Comments (5)
I figured it out, if i replace the decorator with this:
@_bp.arguments(AccountGroupModelSchema(partial=True))
the validate method ignores missing fields!
from flask-smorest.
Whilst partial=True does permit PATCH to work with validation, it creates another problem within apispec causing errors like this to appear:
apispec/ext/marshmallow/openapi.py:135: UserWarning: Multiple schemas resolved to the name Account. The name has been modified. Either manually add each of the schemas with a different name or provide a custom schema_name_resolver.
I assume this is because apispec considers the partial Schema to be different to the non-partial one.
Therefore I'm back in the situation where just sending a single field in a PATCH doesn't work perfectly.
from flask-smorest.
It is fine. Just craft a custom name resolver to provide a different name for partial schemas.
def resolver(schema):
# This is the default name resolver from apispec
schema_cls = resolve_schema_cls(schema)
name = schema_cls.__name__
if name.endswith("Schema"):
name = name[:-6] or name
# Except it appends Partial to partial schemas.
if isinstance(schema, ma.Schema) and schema.partial:
name = f"{name}Partial"
return name
class Api(flask_smorest.Api):
"""Api class"""
def __init__(self, app=None, *, spec_kwargs=None):
spec_kwargs = spec_kwargs or {}
spec_kwargs["marshmallow_plugin"] = MarshmallowPlugin(
schema_name_resolver=resolver
)
super().__init__(app=app, spec_kwargs=spec_kwargs)
from flask-smorest.
Shouldn't flask-smorest "just work" for the (very common) RESTful use of PATCH whereby only modified attributes are submitted?
I don't really understand the above code enough to know quite how it works, but would (in my example above) I also need a AccountGroupModelSchemaPartial schema, thus doubling the number of schemas I have ??
from flask-smorest.
I didn't test that code but I think it should work with the partial=True
change you proposed above. It will create two schemas in the doc but you only use one in your code (with or without partial
).
I'm not sure patch is so commonly used. I've been investigating about it a while back and doing it this way doesn't allow to remove a field, for instance, hence the use of complicated things like json-patch. In my APIs, I don't provide PATCH, just PUT. The client GETs an item, modifies it, then PUTs. But there's no point arguing about whether or not it is common. I'd be happy if it could be supported out-of-the-box even if it uncommon, except in this case it seems kind of complicated to do and requires assumptions about how the PATCH is performed that I don't want to make.
I think the solution proposed above is the way to go. Admittedly, it could be documented better.
from flask-smorest.
Related Issues (20)
- Inconsistent order of parameters when using rule.arguments from werkzeug inside FlaskPlugin HOT 10
- Pagination documentation example is incorrect HOT 3
- Are query based blueprint arguments after path arguments formally unsupported? HOT 5
- Support for mixed multipart forms HOT 5
- ImportError: cannot import name '_app_ctx_stack' from 'flask' HOT 4
- flask smorest import error HOT 1
- Swagger submitting incorrect format for Model-based form inputs HOT 4
- Proposal: Allow register_blueprint option to name the Blueprint outside of Flask HOT 1
- Werkzeug Security Issue
- How can i create pagination for method POST HOT 2
- Missing response header in OpenAPI documentation HOT 3
- How to show docs with nginx url prefix set? HOT 1
- Different `unknown` arguments behaviour locally vs on production HOT 2
- What's the best way to handle multiple routes per function? HOT 1
- Swagger isn't obeying required=True for IPv4 field HOT 6
- TypeError: argument of type 'SchemaMeta' is not iterable when running flask --app '...' openapi print HOT 1
- Improving error handlers to satisfy both template and API server use cases HOT 6
- Endpoint isn't getting data from json_or_form arguments when using the Swagger UI HOT 8
- flask-smorest with subdomains and redoc HOT 2
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-smorest.