Comments (11)
On the latest (0.14.2) version I observe that the Swagger documentation does not correctly capture many=True
, but the validation does work. Can you confirm this is what you experience?
Here is the script I used:
from dataclasses import dataclass
from marshmallow import fields, Schema, post_load
from flask import Flask, jsonify, request
from flask_accepts import accepts, responds
@dataclass
class Widget:
foo: str
baz: int
class WidgetSchema(Schema):
foo = fields.String(default="test value")
baz = fields.Integer(default=422)
@post_load
def make(self, data, **kwargs):
return Widget(**data)
def create_app(env=None):
from flask_restplus import Api, Namespace, Resource
app = Flask(__name__)
api = Api(app)
@api.route("/restplus/make_a_widget")
class WidgetResource(Resource):
@accepts(
dict(name="some_arg", type=str), schema=WidgetSchema(many=True), api=api
)
@responds(schema=WidgetSchema(many=True), api=api)
def post(self):
from flask import jsonify
return request.parsed_obj
return app
app = create_app()
if __name__ == "__main__":
app.run(debug=True)
and then the curl request:
curl -X POST "http://127.0.0.1:5000/restplus/make_a_widget" -H "accept: application/json" -H "Content-Type: application/json" -d "[{ \"baz\": 422, \"foo\": \"test value\"},{ \"baz\": 1, \"foo\": \"another value\"}]"
yields a successful response:
[
{
"baz": 422,
"foo": "test value"
},
{
"baz": 1,
"foo": "another value"
}
]
Assuming this is what you observe as well, the issue is limited to the Swagger documentation itself, and I suspect the fix is simply to make sure if many=True
that a List
is wrapped around the converted schema. Should be straightforward to fix.
from flask_accepts.
I agree, your example functions correctly and only the swagger documentation is incorrect.
In a more complex app, I still have a problem with the many=True
input. It is likely an interaction with another library and not a problem with this one.
Thanks for publishing this library, it is quite helpful and easy to use!
from flask_accepts.
I just tried a couple of things and wasn't quite able to get it working the way I'd like. I'll need to spend some more time on it later.
Basically it appears there is no issue with nested objects/lists -- the issue is isolated to documentation of top-level many=True
.
I would point out that perhaps a more specific solution would be to declare a separate schema that explicitly wraps a list of your objects. This has the benefit of allowing you the control the model name and add additional metadata if necessary; however, it is more verbose and may not do what you want. I only bring it up as an alternative for the time being:
from dataclasses import dataclass
from marshmallow import fields, Schema, post_load
from flask import Flask, jsonify, request
from flask_accepts import accepts, responds
@dataclass
class Widget:
foo: str
baz: int
class WidgetSchema(Schema):
foo = fields.String(default="test value")
baz = fields.Integer(default=422)
@post_load
def make(self, data, **kwargs):
return Widget(**data)
class WidgetList(Schema):
widgets = fields.List(fields.Nested(WidgetSchema))
def create_app(env=None):
from flask_restplus import Api, Namespace, Resource
app = Flask(__name__)
api = Api(app)
@api.route("/restplus/make_a_widget")
class WidgetResource(Resource):
@accepts(dict(name="some_arg", type=str), schema=WidgetList, api=api)
@responds(schema=WidgetList, api=api)
def post(self):
from flask import jsonify
return request.parsed_obj
return app
app = create_app()
if __name__ == "__main__":
app.run(debug=True)
from flask_accepts.
I am generally opposed to top-level lists, but in this case I have no choice but to keep to retain compatibility.
I was able to determine the cause of my list validation failure. It was due to having RESTPLUS_VALIDATE
set to True
in the app configuration. Once I set it to False
, then the issue is reduced to just swagger being incorrect.
I do not recall why RESTPLUS_VALIDATE
was set and in my case disabling it seems fine. Should it be allowed while using flask_accepts
, though? Everything seems to work okay with it set to True
except for top-level list validation.
With it True
, the restplus Resource's validate_payload
fails while trying to validate the list as a model.
from flask_accepts.
For posterity, this example exhibits the validation issue I saw:
from dataclasses import dataclass
from marshmallow import fields, Schema, post_load
from marshmallow.validate import Range
from flask import Flask, request
from flask_accepts import accepts, responds
@dataclass
class Widget:
foo: str
baz: int
class WidgetSchema(Schema):
foo = fields.String(default="test value")
baz = fields.Integer(default=422)
@post_load
def make(self, data, **kwargs):
return Widget(**data)
def create_app(env=None):
from flask_restplus import Api, Resource
app = Flask(__name__)
app.config["RESTPLUS_VALIDATE"] = True
api = Api(app)
@api.route("/restplus/make_a_widget")
class WidgetResource(Resource):
@accepts(schema=WidgetSchema(many=True), api=api)
@responds(schema=WidgetSchema(many=True), api=api)
def put(self):
return request.parsed_obj
return app
app = create_app()
if __name__ == "__main__":
app.run(debug=True)
from flask_accepts.
from flask_accepts.
@apryor6 so you think it is a marshmallow issue or swagger, not flask_accepts, correct?
Looks similar to
https://stackoverflow.com/questions/58484417/flask-marshmallow-nested-schema-with-many-true-not-working
from flask_accepts.
This issue can be closed as resolved.
The fix - #51
from flask_accepts.
@apryor6
Can you bump up version of the package and release new version on PyPI?
from flask_accepts.
Yes, apologies for the delay!
https://pypi.org/project/flask-accepts/0.15.4/
from flask_accepts.
Thank you.
from flask_accepts.
Related Issues (20)
- When using the @responds decorator, X-Fields headers are automatically applied to the swagger request HOT 2
- Swagger fails to load when a schema does not have a default set in one of the fields. HOT 4
- Marshmallow schema for form data HOT 1
- Query parameter and header schemas do not respect the data key attribute HOT 1
- Passing a schema type to the `schema` argument of @accepts ignores the type's Meta attributes
- Marshmallow 4 breaking changes
- Overriding flask_restx field
- Nested Marshmallow Schemas throwing error on Swagger HOT 5
- Partial param for Schema loading HOT 4
- [Q] Customize schema_errors? HOT 2
- Only swagger generation for end-point HOT 10
- What is the best practice for file uploads? HOT 2
- Working with UUIDs, Marshmallow & 'missing' fields
- Nested schemas with many=true do not generate a list of schemas in the swagger docs HOT 6
- The new partial keyword of accepts() breaks the marshmallow schema
- Using multiple different schemas in accepts decorator raises KeyError if there is a ValidationError
- Cookie header validation works on gunicorn, does not on NGINX
- Static typing on request.parsed_obj
- More forgiving loading of the JSON schema payload ignoring the mimetype
- Skip response fields where value is None
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flask_accepts.