marshmallow-code / marshmallow-code/apispec
What is the best way to handle post_dump hooks and "tweak" the generated spec?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.2k
- Forks
- 202
- Avg merge
- 3h 38m
- Merged PRs (30d)
- 3
Description
I have some schemas which handle some messy/complicated cases by means of (sometimes elaborate) pre_dump and post_dump hooks.
To give a (contrived) example, a schema might look like so:
```python
class MySchemaEncodingAPeculiarList(Schema):
data = fields.Nested(OtherSchema(many=True))
@pre_dump
def filter_data(self, data, **kwargs):
contents = [x for x in data["foo"] if data["bar"][x] % 2 == 0]
return {"data": contents}
@post_dump
def unwrap_data(self, obj, **kwargs):
return obj["data"]
class MySchema(Schema):
foos = fields.Nested(MySchemaEncodingAPeculiarList)
```
When my output schemas are passed into apispec, `MySchema` will show up something like
```
{
"foos": {
"data": [
...
]
}
}
```
But the field `data` shouldn't really be there -- it's just an artifact of the way that these `pre_dump` and `post_dump` hooks are manipulating the data.
apispec cannot reasonably be asked to know what to do with this out of the box. post_dump and pre_dump hooks could do anything.
But I would like to know how I can or should customize the marshmallow plugin in order to handle `MySchemaEncodingAPeculiarList`, or maybe to handle it at the level of `MySchema`. I couldn't figure this out from the documentation, and would appreciate any help or guidance.
(I am also looking at restructuring the schemas, but the real use-case is a lot more complex than this toy example.)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue mentions marshmallow hooks, apispec, and the marshmallow plugin, but names no source files or tests. Start by reading the plugin customization documentation and locating the schema-to-OpenAPI conversion entry point. Done means documenting a supported way to represent the post_dump result without the internal data field, or clearly stating that this transformation is unsupported.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, python
- Domain
- api, documentation
- Issue type
- Documentation
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100