APIFlask 3.x introduces a new schema adapter system that supports both marshmallow schemas and Pydantic models. This change is fully backward compatible.
You can now optionally use Pydantic models for type-hint based validation:
frompydanticimportBaseModel,FieldclassPetIn(BaseModel):name:str=Field(...,min_length=1,max_length=50)age:int=Field(...,ge=0,le=30)@app.input(PetIn)# Works with the same decoratorsdefcreate_pet(json_data):return{'message':'created'}
See the Data Schema documentation for comprehensive examples or check out the Pydantic example for a complete working application.
Output Validation Behavior
Unlike marshmallow, Pydantic validates output data before sending responses. This means that if your view function returns data that does not conform to the output model schema, a 500 Internal Server Error will be raised.
Use security.APIKey*Auth for API Key Authentication¶
In APIFlask 3.x, the API key authentication classes have been refactored for better clarity. Instead of using HTTPTokenAuth for API key authentication, you should now use one of the following classes based on where the API key is expected:
In APIFlask 2.x, the data passed with the input decorator changed
to keyword argument named {location}_data. For example, the name
for the JSON input will be json_data:
@app.post('/pets')@app.input(PetQuery,location='query')@app.input(PetIn)# equals to app.input(PetIn, location='json')defcreate_pet(query_data,json_data):pass
From APIFlask 2.x, all the OpenAPI docs are available at /docs. You can change
the docs UI to ReDoc with the docs_ui parameter, and change the docs path
with the docs_path parameter: