Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flask-RESTX does not natively support Marshmallow #398

Closed
GvandeSteeg opened this issue Dec 13, 2021 · 2 comments
Closed

Flask-RESTX does not natively support Marshmallow #398

GvandeSteeg opened this issue Dec 13, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@GvandeSteeg
Copy link

The docs have stated for some time that flask-restx should be compatible with Marshmallow. I'm trying to use marshmallow's field.UUID field that flask-restx keeps interpreting as a string, but couldn't get the api schema to work.

I've gone back to a smaller example to showcase the issue:

Code

Using the Quickstart from the README

ns = api.namespace('todos', description='TODO operations')

class todo(Schema):
    id = fields.Integer(readonly=True, description='The task unique identifier'),
    task = fields.String(required=True, description='The task details')

# todo = api.model('Todo', {
#     'id': fields.Integer(readonly=True, description='The task unique identifier'),
#     'task': fields.String(required=True, description='The task details')
# })

class TodoDAO(object):

throws the following error:

127.0.0.1 - - [10/Dec/2021 15:52:08] "GET / HTTP/1.1" 200 -
Unable to render schema
Traceback (most recent call last):
  File "/Users/gv4/PycharmProjects/pipeline_api/venv/lib/python3.8/site-packages/flask_restx/api.py", line 565, in __schema__
    self._schema = Swagger(self).as_dict()
  File "/Users/gv4/PycharmProjects/pipeline_api/venv/lib/python3.8/site-packages/flask_restx/swagger.py", line 239, in as_dict
    serialized = self.serialize_resource(
  File "/Users/gv4/PycharmProjects/pipeline_api/venv/lib/python3.8/site-packages/flask_restx/swagger.py", line 446, in serialize_resource
    path[method] = self.serialize_operation(doc, method)
  File "/Users/gv4/PycharmProjects/pipeline_api/venv/lib/python3.8/site-packages/flask_restx/swagger.py", line 452, in serialize_operation
    "responses": self.responses_for(doc, method) or None,
  File "/Users/gv4/PycharmProjects/pipeline_api/venv/lib/python3.8/site-packages/flask_restx/swagger.py", line 572, in responses_for
    schema = self.serialize_schema(model)
  File "/Users/gv4/PycharmProjects/pipeline_api/venv/lib/python3.8/site-packages/flask_restx/swagger.py", line 633, in serialize_schema
    "items": self.serialize_schema(model),
  File "/Users/gv4/PycharmProjects/pipeline_api/venv/lib/python3.8/site-packages/flask_restx/swagger.py", line 653, in serialize_schema
    raise ValueError("Model {0} not registered".format(model))
ValueError: Model <class 'app.todo'> not registered

or

ns = api.namespace('todos', description='TODO operations')

class TODO(Schema):
    id = fields.Integer(readonly=True, description='The task unique identifier'),
    task = fields.String(required=True, description='The task details')

# todo = api.model('Todo', {
#     'id': fields.Integer(readonly=True, description='The task unique identifier'),
#     'task': fields.String(required=True, description='The task details')
# })

todo = TODO()

class TodoDAO(object):

Throws the following error:

127.0.0.1 - - [10/Dec/2021 15:52:40] "GET / HTTP/1.1" 200 -
Unable to render schema
Traceback (most recent call last):
  File "/Users/gv4/PycharmProjects/pipeline_api/venv/lib/python3.8/site-packages/flask_restx/api.py", line 565, in __schema__
    self._schema = Swagger(self).as_dict()
  File "/Users/gv4/PycharmProjects/pipeline_api/venv/lib/python3.8/site-packages/flask_restx/swagger.py", line 239, in as_dict
    serialized = self.serialize_resource(
  File "/Users/gv4/PycharmProjects/pipeline_api/venv/lib/python3.8/site-packages/flask_restx/swagger.py", line 446, in serialize_resource
    path[method] = self.serialize_operation(doc, method)
  File "/Users/gv4/PycharmProjects/pipeline_api/venv/lib/python3.8/site-packages/flask_restx/swagger.py", line 452, in serialize_operation
    "responses": self.responses_for(doc, method) or None,
  File "/Users/gv4/PycharmProjects/pipeline_api/venv/lib/python3.8/site-packages/flask_restx/swagger.py", line 572, in responses_for
    schema = self.serialize_schema(model)
  File "/Users/gv4/PycharmProjects/pipeline_api/venv/lib/python3.8/site-packages/flask_restx/swagger.py", line 653, in serialize_schema
    raise ValueError("Model {0} not registered".format(model))
ValueError: Model <TODO(many=False)> not registered

Expected Behavior

Marshmallow Schemas to be accepted alongside flask-restx models

Actual Behavior

flask-restx throws a Model not registered error.

Error Messages/Stack Trace

If applicable, add the stack trace produced by the error

Environment

  • Python 3.8.2
  • Flask 1.1.4
  • Werkzeug 1.0.1
  • Flask-RESTX 0.4.0

Additional Context

This is your last chance to provide any pertinent details, don't let this opportunity pass you by!

@GvandeSteeg GvandeSteeg added the bug Something isn't working label Dec 13, 2021
@j5awry
Copy link
Contributor

j5awry commented Dec 14, 2021

You've misread the documentation. From the link

The whole request parser part of Flask-RESTX is slated for removal and will be replaced by documentation on how to integrate with other packages that do the input/output stuff better (such as marshmallow).

Key being will be replaced. At this time, the request parser does not integrate.

Furthermore you can see an open ticket for possible API model redesign: #59

At this time, flask-restx only supports it's own models for API and the built in request parser.

@j5awry j5awry closed this as completed Dec 14, 2021
@GvandeSteeg
Copy link
Author

Hi @j5awry, thanks for getting back to me so quickly. Do you have any suggestions on how I can use Flask-Restx to take in a UUID in my requests parser?

blorb from my swagger.json

"parameters": [
{
"name": "file",
"in": "formData",
"type": "file",
"required": true
},
{
"name": "uuid",
"in": "query",
"type": "string",
"required": true,
"default": "37b4e109-8f5b-4e04-9261-41056be9671d"
},

I'd like that to become:

"parameters": [
{
"name": "file",
"in": "formData",
"type": "file",
"required": true
},
{
"name": "uuid",
"in": "query",
"type": "uuid",
"required": true,
"default": "37b4e109-8f5b-4e04-9261-41056be9671d"
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants