For marshamllow, the response data returned by the view function will only be formatting against your
schema, not validating.
For Pydantic, the response data returned by the view function will be validated
against your model schema before formatting. If the data is invalid, a 500 Internal Server Error
will be raised.
You can only declare one output (use one app.output decorator) for the JSON response body.
The error responses of your view can be declared with app.doc(response=...).
The PaginationSchema is a schema that contains the general fields
for pagination information.
The pagination_builder is a helper function to generate pagination
data for the PaginationSchema.
To add pagination support to our pet store example. First, we create a
schema for query strings:
fromapiflaskimportSchemafromapiflask.fieldsimportIntegerfromapiflask.validatorsimportRangeclassPetQuery(Schema):page=Integer(load_default=1)# set default page to 1# set default value to 20, and make sure the value is less than 30per_page=Integer(load_default=20,validate=Range(max=30))
Then we create a pet output schema, and a pets schema that contains
a list of nested PetOut schema, and a nested PaginationSchema
schema.
In the return value of the view function, we use pagination_builder
to build the pagination data and passes the pagination object provided
by Flask-SQLAlchemy.
This function is designed based on Flask-SQLAlchemy's Pagination class.
If you are using a different or custom pagination class, make sure the
passed pagination object has the following attributes:
page
per_page
pages
total
next_num
has_next
prev_num
has_prev
You can also write a custom builder function and pagination schema
to build your custom pagination data.
You can set response examples for OpenAPI spec with the example and examples
parameters, see this section in the
OpenAPI Generating chapter for more details.