3
3
[ ![ PyPI] ( https://img.shields.io/pypi/v/openapi-pydantic )] ( https://pypi.org/project/openapi-pydantic/ )
4
4
[ ![ PyPI - License] ( https://img.shields.io/pypi/l/openapi-pydantic )] ( https://github.com/mike-oakley/openapi-pydantic/blob/main/LICENSE )
5
5
6
- OpenAPI schema implemented in [ Pydantic] ( https://github.com/samuelcolvin/pydantic ) .
6
+ OpenAPI schema implemented in [ Pydantic] ( https://github.com/samuelcolvin/pydantic ) . Both Pydantic 1.8+ and 2.x are supported.
7
7
8
8
The naming of the classes follows the schema in
9
9
[ OpenAPI specification] ( https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#schema ) .
@@ -37,7 +37,8 @@ open_api = OpenAPI(
37
37
)
38
38
},
39
39
)
40
- print (open_api.json(by_alias = True , exclude_none = True , indent = 2 ))
40
+ # Note: for Pydantic 1.x, replace `model_dump_json` with `json`
41
+ print (open_api.model_dump_json(by_alias = True , exclude_none = True , indent = 2 ))
41
42
```
42
43
43
44
Result:
@@ -71,7 +72,7 @@ Result:
71
72
72
73
## Take advantage of Pydantic
73
74
74
- Pydantic is a great tool, allow you to use object / dict / mixed data for for input.
75
+ Pydantic is a great tool. It allows you to use object / dict / mixed data for input.
75
76
76
77
The following examples give the same OpenAPI result as above:
77
78
@@ -80,6 +81,7 @@ from openapi_pydantic import parse_obj, OpenAPI, PathItem, Response
80
81
81
82
# Construct OpenAPI from dict, inferring the correct schema version
82
83
open_api = parse_obj({
84
+ " openapi" : " 3.1.0" ,
83
85
" info" : {" title" : " My own API" , " version" : " v0.0.1" },
84
86
" paths" : {
85
87
" /ping" : {
@@ -90,7 +92,8 @@ open_api = parse_obj({
90
92
91
93
92
94
# Construct OpenAPI v3.1.0 schema from dict
93
- open_api = OpenAPI.parse_obj({
95
+ # Note: for Pydantic 1.x, replace `model_validate` with `parse_obj`
96
+ open_api = OpenAPI.model_validate({
94
97
" info" : {" title" : " My own API" , " version" : " v0.0.1" },
95
98
" paths" : {
96
99
" /ping" : {
@@ -100,7 +103,8 @@ open_api = OpenAPI.parse_obj({
100
103
})
101
104
102
105
# Construct OpenAPI with mix of dict/object
103
- open_api = OpenAPI.parse_obj({
106
+ # Note: for Pydantic 1.x, replace `model_validate` with `parse_obj`
107
+ open_api = OpenAPI.model_validate({
104
108
" info" : {" title" : " My own API" , " version" : " v0.0.1" },
105
109
" paths" : {
106
110
" /ping" : PathItem(
@@ -113,10 +117,10 @@ open_api = OpenAPI.parse_obj({
113
117
## Use Pydantic classes as schema
114
118
115
119
- The [ Schema Object] ( https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#schemaObject )
116
- in OpenAPI has definitions and tweaks in JSON Schema, which is hard to comprehend and define a good data class
117
- - Pydantic already has a good way to [ create JSON schema] ( https://pydantic-docs.helpmanual.io/usage/schema/ ) ,
118
- let 's not re-invent the wheel
119
-
120
+ in OpenAPI has definitions and tweaks in JSON Schema, which are hard to comprehend and define a good data class
121
+ - Pydantic already has a good way to [ create JSON schema] ( https://pydantic-docs.helpmanual.io/usage/schema/ ) .
122
+ Let 's not reinvent the wheel.
123
+
120
124
The approach to deal with this:
121
125
122
126
1 . Use ` PydanticSchema ` objects to represent the ` Schema ` in ` OpenAPI ` object
@@ -129,7 +133,8 @@ from openapi_pydantic import OpenAPI
129
133
from openapi_pydantic.util import PydanticSchema, construct_open_api_with_schema_class
130
134
131
135
def construct_base_open_api () -> OpenAPI:
132
- return OpenAPI.parse_obj({
136
+ # Note: for Pydantic 1.x, replace `model_validate` with `parse_obj`
137
+ return OpenAPI.model_validate({
133
138
" info" : {" title" : " My own API" , " version" : " v0.0.1" },
134
139
" paths" : {
135
140
" /ping" : {
@@ -162,7 +167,8 @@ open_api = construct_base_open_api()
162
167
open_api = construct_open_api_with_schema_class(open_api)
163
168
164
169
# print the result openapi.json
165
- print (open_api.json(by_alias = True , exclude_none = True , indent = 2 ))
170
+ # Note: for Pydantic 1.x, replace `model_dump_json` with `json`
171
+ print (open_api.model_dump_json(by_alias = True , exclude_none = True , indent = 2 ))
166
172
```
167
173
168
174
Result:
@@ -259,21 +265,24 @@ Result:
259
265
260
266
## Notes
261
267
262
- ### Use of OpenAPI.json() / OpenAPI.dict()
268
+ ### Use of OpenAPI.model_dump() / OpenAPI.model_dump_json() / OpenAPI. json() / OpenAPI.dict()
263
269
264
- When using ` OpenAPI.json() ` / ` OpenAPI.dict() ` function ,
265
- arguments ` by_alias=True, exclude_none=True ` has to be in place.
266
- Otherwise the result json will not fit the OpenAPI standard.
270
+ When using ` OpenAPI.model_dump() ` / ` OpenAPI.model_dump_json() ` / ` OpenAPI. json()` / ` OpenAPI.dict() ` functions ,
271
+ the arguments ` by_alias=True, exclude_none=True ` have to be in place.
272
+ Otherwise the resulting json will not fit the OpenAPI standard.
267
273
268
274
``` python
269
- # OK
275
+ # OK (Pydantic 2)
276
+ open_api.model_dump_json(by_alias = True , exclude_none = True , indent = 2 )
277
+ # OK (Pydantic 1)
270
278
open_api.json(by_alias = True , exclude_none = True , indent = 2 )
271
279
272
280
# Not good
281
+ open_api.model_dump_json(indent = 2 )
273
282
open_api.json(indent = 2 )
274
283
```
275
284
276
- More info about field alias :
285
+ More info about field aliases :
277
286
278
287
| OpenAPI version | Field alias info |
279
288
| --------------- | ---------------- |
@@ -293,13 +302,17 @@ Please refer to the following for more info:
293
302
### Use OpenAPI 3.0.3 instead of 3.1.0
294
303
295
304
Some UI renderings (e.g. Swagger) still do not support OpenAPI 3.1.0.
296
- It is allowed to use the old 3.0.3 version by importing from different paths:
305
+ The old 3.0.3 version is available by importing from different paths:
297
306
298
307
``` python
299
308
from openapi_pydantic.v3.v3_0_3 import OpenAPI, ...
300
309
from openapi_pydantic.v3.v3_0_3.util import PydanticSchema, construct_open_api_with_schema_class
301
310
```
302
311
312
+ ### Pydantic version compatibility
313
+
314
+ Compatibility with both major versions of Pydantic (1.8+ and 2.* ) is mostly achieved using a module called ` compat.py ` . It detects the installed version of Pydantic and exports version-specific symbols for use by the rest of the package. It also provides all symbols necessary for type checking. The ` compat.py ` module is not intended to be imported by other packages, but other packages may find it helpful as an example of how to span major versions of Pydantic.
315
+
303
316
## Credits
304
317
305
318
This library is based from the original implementation by Kuimono of [ OpenAPI Schema Pydantic] ( https://github.com/kuimono/openapi-schema-pydantic ) which is no longer actively maintained.
0 commit comments