python-restx / flask-restx

Fork of Flask-RESTPlus: Fully featured framework for fast, easy and documented API development with Flask
https://flask-restx.readthedocs.io/en/latest/
Other
2.15k stars 332 forks source link

Using SchemaModel with marshal_with not supported #435

Open busoff opened 2 years ago

busoff commented 2 years ago

I followed the documentation to use JSON schema as model, but I encountered the error described as below.

Code

from flask import Flask
from flask_restx import Api, Resource, fields

app = Flask(__name__)
api = Api(app)

person = api.schema_model('Person', {
    'required': ['name'],
    'properties': {
        'name': {
            'type': 'string'
        },
        'age': {
            'type': 'integer'
        }
    },
    'type': 'object'
})

@api.route('/person')
class Person(Resource):
    @api.marshal_with(person)
    def get(self, **kwargs):
        return {'name': 'foo', 'age': 33}

if __name__ == '__main__':
    app.run(debug=True)

Repro Steps

  1. run the flask application
  2. open a URL http://localhost:5000/person in a browser

Expected Behavior

The flask application shall return a JSON response {'name': 'foo', 'age': 33}

Actual Behavior

The flask application returns 500error code

Error Messages/Stack Trace

(venv) ➜ webconfig git:(master) ✗ python app.py

Environment

ErikBjare commented 2 years ago

flask-restx does not support marshalling using JSON schemas afaik.

Here's an example where the JSON schemas are used for documentation, and normal api.model for marshalling: https://github.com/ActivityWatch/aw-server/blob/master/aw_server/rest.py#L74

I suggest you change the issue title to: "Using SchemaModel with marshal_with not supported"

ErikBjare commented 2 years ago

Actually, this is a duplicate of: https://github.com/python-restx/flask-restx/issues/142