marshmallow-code / marshmallow-code/flask-smorest
How do I provide example values for PaginationParameterSchema?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 717
- Forks
- 77
- Avg merge
- 7h 49m
- Merged PRs (30d)
- 3
Description
I really like adding example data to my OpenAPI spec. It helps some of my users understand my API endpoints more easily, and Rapidoc UI has a convenient "Fill Example" button to autofill example data when playing with the API interactively.
In general, I can add the `example=` or `examples=` keyword arguments to blueprint decorators to provide OpenAPI example data for parameters, return values, etc. I've also noticed that I can put these keywords in the `marshmallow.field()` definition to get the same effect. However, I can't find a way to specify example values for the automatic paging parameters added with the `Blueprint.paginate` decorator. The decorator itself doesn't expose a `**kwargs` parameter, and the schema of the generated object is created on the fly in a private helper function.
So far, the only way I've found to add examples to these values is by subclassing Blueprint, copy/pasting both `paginate` and the helper function `_pagination_parameters_schema_factory`, and adding example values to the anonymous class in that helper, like this:
```python
def _pagination_parameters_schema_factory(def_page, def_page_size, def_max_page_size):
"""Generate a PaginationParametersSchema"""
class PaginationParametersSchema(ma.Schema):
"""Deserializes pagination params into PaginationParameters"""
class Meta:
ordered = True
unknown = ma.EXCLUDE
page = ma.fields.Integer(
load_default=def_page,
validate=ma.validate.Range(min=1),
example=def_page, # Use the default page parameter as an example value
)
page_size = ma.fields.Integer(
load_default=def_page_size,
validate=ma.validate.Range(min=1, max=def_max_page_size),
example=def_page_size, # Again, use the provided default as an example
)
@ma.post_load
def make_paginator(self, data, **kwargs):
return PaginationParameters(**data)
return PaginationParametersSchema
```
I'm opening this as an issue rather than just submitting a PR for that change because I'm new to flask-smorest and Marshmallow, and I could just be missing something obvious. This seems similar to #327, but not identical.
Is there an easier / better way to provide example data for these pagination fields?
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
Start with the Blueprint.paginate decorator and the private _pagination_parameters_schema_factory helper described in the issue. Trace how the generated PaginationParametersSchema reaches the OpenAPI output, then check existing pagination and schema tests. Done means there is a supported way to provide example values for the generated page and page_size parameters without subclassing Blueprint.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- flask, openapi, python
- Domain
- api, documentation
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100