flask-restful-swagger-2.0
flask-restful-swagger-2.0 copied to clipboard
Custom URL Map Converter
I am having an issue with getting this to work with a custom url map converter.
Here is a snippet of what I'm trying to do:
import os
import flask
from flask_restful_swagger_2 import Api
from device_key import DeviceKeyConverter
from resources.device import Device, DeviceList
app = flask.Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
api = Api(app, api_version='0.0',
api_spec_url='/api',
produces=['application/json', 'text/html']
)
app.url_map.converters['device_key'] = DeviceKeyConverter
api.add_resource(Device, '/devices/<device_key:device_key>')
api.add_resource(DeviceList, '/devices')
With the coverer looking like this:
from werkzeug.routing import BaseConverter
from special_serial_number import SpecialSerialNumber
class DeviceKeyConverter(BaseConverter):
def to_python(self, value):
if (isinstance(value, int)) or (':' not in value):
return int(value)
return SpecialSerialNumber(value)
def to_url(self, value):
if isinstance(value, int):
return int(value)
return str(value)
When I run my app I get an error on the line:
api.add_resource(Device, '/devices/<device_key:device_key>')
Here is the error:
Traceback (most recent call last):
File "/home/tim/work/backend/venv/lib/python3.5/site-packages/flask_restful_swagger_2/swagger.py", line 268, in validate_operation_object
validate_reference_object(parameter)
File "/home/tim/work/backend/venv/lib/python3.5/site-packages/flask_restful_swagger_2/swagger.py", line 324, in validate_reference_object
raise ValidationError('Invalid reference object. It may only contain key "$ref"')
flask_restful_swagger_2.swagger.ValidationError: Invalid reference object. It may only contain key "$ref"
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "src/app.py", line 47, in <module>
api.add_resource(Device, '/devices/<device_key:device_key>')
File "/home/tim/work/backend/venv/lib/python3.5/site-packages/flask_restful_swagger_2/__init__.py", line 119, in add_resource
validate_path_item_object(path_item)
File "/home/tim/work/backend/venv/lib/python3.5/site-packages/flask_restful_swagger_2/swagger.py", line 232, in validate_path_item_object
validate_operation_object(v)
File "/home/tim/work/backend/venv/lib/python3.5/site-packages/flask_restful_swagger_2/swagger.py", line 270, in validate_operation_object
validate_parameter_object(parameter)
File "/home/tim/work/backend/venv/lib/python3.5/site-packages/flask_restful_swagger_2/swagger.py", line 295, in validate_parameter_object
url='http://swagger.io/specification/#parameterObject'))
flask_restful_swagger_2.swagger.ValidationError: Invalid parameter object. Unknown field "allowMultiple". See http://swagger.io/specification/#parameterObject
Does this package not support custom URL Map Converters?