Schema Adapters¶
The schema adapter system provides a pluggable architecture for supporting different schema libraries in APIFlask.
Base Classes¶
SchemaAdapter
¶
Bases: ABC
Base class for schema adapters.
Schema adapters provide a common interface for different schema libraries (marshmallow, Pydantic, etc.) to integrate with APIFlask.
Version added: 3.0.0
Source code in apiflask/schema_adapters/base.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
schema_type
abstractmethod
property
¶
Get the type of schema adapter.
Returns:
| Type | Description |
|---|---|
str
|
Schema type identifier ('marshmallow', 'pydantic', etc.) |
__init__(schema, schema_name=None, many=False)
¶
Initialize the adapter with a schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Any
|
The schema object (marshmallow Schema, Pydantic model, etc.) |
required |
schema_name
|
str | None
|
Optional schema name (used by some adapters like marshmallow) |
None
|
many
|
bool
|
Whether this schema represents a list/array of items |
False
|
Source code in apiflask/schema_adapters/base.py
20 21 22 23 24 25 26 27 28 29 |
|
get_openapi_schema(**kwargs)
abstractmethod
¶
Get OpenAPI schema definition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Additional arguments for schema generation |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
OpenAPI schema dict |
Source code in apiflask/schema_adapters/base.py
61 62 63 64 65 66 67 68 69 70 71 |
|
get_schema_name()
abstractmethod
¶
Get the name of the schema for OpenAPI documentation.
Returns:
| Type | Description |
|---|---|
str
|
Schema name string |
Source code in apiflask/schema_adapters/base.py
73 74 75 76 77 78 79 80 |
|
serialize_output(data, many=False)
abstractmethod
¶
Serialize output data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Data to serialize |
required |
many
|
bool
|
Whether to serialize many objects |
False
|
Returns:
| Type | Description |
|---|---|
Any
|
Serialized data ready for JSON response |
Source code in apiflask/schema_adapters/base.py
48 49 50 51 52 53 54 55 56 57 58 59 |
|
validate_input(request, location, **kwargs)
abstractmethod
¶
Validate and parse input data from request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
Flask request object |
required |
location
|
str
|
Location of data ('json', 'query', 'form', etc.) |
required |
**kwargs
|
Any
|
Additional arguments passed from decorator |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Validated and parsed data |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If validation fails |
Source code in apiflask/schema_adapters/base.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
Registry¶
SchemaRegistry
¶
Registry for managing schema adapters and automatic type detection.
Version added: 3.0.0
Source code in apiflask/schema_adapters/registry.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
create_adapter(schema, schema_type=None, schema_name=None)
¶
Create a schema adapter for the given schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Any
|
Schema object (can be a model or list[Model]/List[Model]) |
required |
schema_type
|
str | None
|
Optional schema type override |
None
|
schema_name
|
str | None
|
Optional schema name for dict schemas |
None
|
Returns:
| Type | Description |
|---|---|
SchemaAdapter
|
Schema adapter instance |
Raises:
| Type | Description |
|---|---|
ValueError
|
If schema type is not supported |
Source code in apiflask/schema_adapters/registry.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
detect_schema_type(schema)
¶
Detect the type of schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Any
|
Schema object to detect type for |
required |
Returns:
| Type | Description |
|---|---|
str
|
Schema type name |
Raises:
| Type | Description |
|---|---|
ValueError
|
If schema type cannot be detected |
Source code in apiflask/schema_adapters/registry.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
|
get_available_types()
¶
Get list of available schema types.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of available schema type names |
Source code in apiflask/schema_adapters/registry.py
147 148 149 150 151 152 153 |
|
register(name, adapter_class)
¶
Register a schema adapter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the adapter |
required |
adapter_class
|
type[SchemaAdapter]
|
Schema adapter class |
required |
Source code in apiflask/schema_adapters/registry.py
49 50 51 52 53 54 55 56 |
|
Adapters¶
marshmallow Adapter¶
FlaskParser
¶
Bases: FlaskParser
Overwrite the default webargs.FlaskParser.handle_error.
Update the default status code and the error description from related configuration variables.
Version added: 3.0.0
Source code in apiflask/schema_adapters/marshmallow.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
MarshmallowAdapter
¶
Bases: SchemaAdapter
Schema adapter for marshmallow schemas.
Source code in apiflask/schema_adapters/marshmallow.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
|
__init__(schema, schema_name=None, many=False)
¶
Initialize the marshmallow adapter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Schema | dict | type[Schema]
|
Marshmallow schema instance, dict, or schema class |
required |
schema_name
|
str | None
|
Optional schema name for dict schemas |
None
|
many
|
bool
|
Whether this schema represents a list/array of items |
False
|
Source code in apiflask/schema_adapters/marshmallow.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
get_openapi_schema(**kwargs)
¶
Get OpenAPI schema from marshmallow schema.
Source code in apiflask/schema_adapters/marshmallow.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|
get_schema_name()
¶
Get schema name for OpenAPI documentation.
Version changed: 3.0.0
- Remove "Schema" suffix stripping from schema names.
Source code in apiflask/schema_adapters/marshmallow.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
|
serialize_output(data, many=False)
¶
Serialize output using marshmallow.
Source code in apiflask/schema_adapters/marshmallow.py
136 137 138 139 140 141 142 |
|
validate_input(request, location, **kwargs)
¶
Validate input using marshmallow/webargs.
Source code in apiflask/schema_adapters/marshmallow.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
Pydantic Adapter¶
PydanticAdapter
¶
Bases: SchemaAdapter
Schema adapter for Pydantic models.
Source code in apiflask/schema_adapters/pydantic.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
|
__init__(schema, schema_name=None, many=False)
¶
Initialize the Pydantic adapter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
type[BaseModel] | BaseModel
|
Pydantic model class or instance |
required |
schema_name
|
str | None
|
Optional schema name (not used for Pydantic) |
None
|
many
|
bool
|
Whether this schema represents a list/array of items |
False
|
Source code in apiflask/schema_adapters/pydantic.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
get_openapi_schema(**kwargs)
¶
Get OpenAPI schema from Pydantic model.
Uses Pydantic's ref_template to generate OpenAPI-compliant $ref paths. Nested models in $defs should be extracted and registered separately at the components/schemas level.
Source code in apiflask/schema_adapters/pydantic.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
|
get_schema_name()
¶
Get schema name for OpenAPI documentation.
Source code in apiflask/schema_adapters/pydantic.py
230 231 232 |
|
serialize_output(data, many=False)
¶
Serialize output using Pydantic with validation.
Source code in apiflask/schema_adapters/pydantic.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
validate_input(request, location, **kwargs)
¶
Validate input using Pydantic.
Source code in apiflask/schema_adapters/pydantic.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|