You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
def_pagination_parameters_schema_factory(def_page, def_page_size, def_max_page_size):
"""Generate a PaginationParametersSchema"""classPaginationParametersSchema(ma.Schema):
"""Deserializes pagination params into PaginationParameters"""classMeta:
ordered=Trueunknown=ma.EXCLUDEpage=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_loaddefmake_paginator(self, data, **kwargs):
returnPaginationParameters(**data)
returnPaginationParametersSchema
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?
The text was updated successfully, but these errors were encountered:
I suppose I could add those two "example" lines to the core, I don't think it would hurt.
This wouldn't address the root issue (#327) which is pagination customization.
Subclassing and overriding _pagination_parameters_schema_factory seem like a fine enough solution to this, at least the best trade-off I can think off. Exposing the schema as class attribute would limit the possibilities to override it in the call to paginate.
Subclassing and overriding _pagination_parameters_schema_factory seem like a fine enough solution to this, at least the best trade-off I can think off. Exposing the schema as class attribute would limit the possibilities to override it in the call to paginate.
If that helper function was converted into an instance method of PaginationMixin, I don't think I'd need to override both, since at that point I could just override the helper function.
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=
orexamples=
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 themarshmallow.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 theBlueprint.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: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?
The text was updated successfully, but these errors were encountered: