Comments (5)
In order to help, you can find attached the basic configuration I am using :
case-accepts.tar.gz
thank you,
from flask_accepts.
@froggylab Were you able to find a solution/workaround for this?
from flask_accepts.
@v1shwa The only two possibilities I found out were :
- Transmit only the ID of the item you want to send in order for the frontend to do a second request
- import one of the schema directly in your schema class definition (but it's ugly)
from flask_accepts.
Thanks for quick response @froggylab . I don't think asking frontend to make another call is an option. I am planning to look into the source & see if we can tweak something. Hopefully, I will raise a PR for this soon.
from flask_accepts.
Just ran into this myself, for anyone interested here's my workaround, exploiting the fact that marshmallow calculates these string-name-defined schema lazily and stores the result on the schema
attribute of the field:
def unpack_nested(val, api, model_name: str = None, operation: str = "dump"):
if val.nested == "self":
return unpack_nested_self(val, api, model_name, operation)
+ if isinstance(val.nested, str):
+ parent = val.parent.parent if is_list_field(val.parent) else val.parent
+ parent_name = get_default_model_name(parent)
+ model_name = val.nested + '-in-' + parent_name
+ nested_schema = val.schema
+ else:
model_name = get_default_model_name(val.nested)
+ nested_schema = val.nested
if val.many:
return fr.List(
fr.Nested(
- map_type(val.nested, api, model_name, operation), **_ma_field_to_fr_field(val)
+ map_type(nested_schema, api, model_name, operation), **_ma_field_to_fr_field(val)
)
)
return fr.Nested(
- map_type(val.nested, api, model_name, operation), **_ma_field_to_fr_field(val)
+ map_type(nested_schema, api, model_name, operation), **_ma_field_to_fr_field(val)
)
I additionally change the model name to <child>-in-<parent>(-load/dump)
to prevent conflicts in case your nested fields use only/include/exclude
and thus have different fields in their schema. (for_swagger
also has to be modified to deal with this, simply add conditions in here: https://github.com/apryor6/flask_accepts/blob/master/flask_accepts/utils.py#L74)
This method can be monkey-patched in via utils.type_map.update({fr.Nested: unpack_nested})
if you want to define it yourself instead of editing the library. (for_swagger
can be likewise monkey-patched by updating Schema/SchemaMeta
in type_map
)
This probably needs some cleanup before being PR-ready material (not even sure if this is a good approach), but it might be a usable workaround for anyone who needs it in the interim.
Bonus: hacky workaround for pluck fields by replacing the first half of that update_nested with
if isinstance(val.nested, str):
nested_schema = val.schema
else:
nested_schema = val.nested()
plucked_field = nested_schema.fields[val.field_name]
(and pass plucked_field
into map_type
instead of nested_schema
)
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.