@@ -18,6 +18,7 @@ The naming of the classes follows the schema in
18
18
19
19
``` python
20
20
from openapi_pydantic import OpenAPI, Info, PathItem, Operation, Response
21
+ from openapi_pydantic.compat import PYDANTIC_V2
21
22
22
23
# Construct OpenAPI by pydantic objects
23
24
open_api = OpenAPI(
@@ -37,7 +38,10 @@ open_api = OpenAPI(
37
38
)
38
39
},
39
40
)
40
- print (open_api.json(by_alias = True , exclude_none = True , indent = 2 ))
41
+ if PYDANTIC_V2 :
42
+ print (open_api.model_dump_json(by_alias = True , exclude_none = True , indent = 2 ))
43
+ else :
44
+ print (open_api.json(by_alias = True , exclude_none = True , indent = 2 ))
41
45
```
42
46
43
47
Result:
@@ -71,12 +75,13 @@ Result:
71
75
72
76
## Take advantage of Pydantic
73
77
74
- Pydantic is a great tool, allow you to use object / dict / mixed data for for input.
78
+ Pydantic is a great tool. It allows you to use object / dict / mixed data for input.
75
79
76
80
The following examples give the same OpenAPI result as above:
77
81
78
82
``` python
79
83
from openapi_pydantic import parse_obj, OpenAPI, PathItem, Response
84
+ from openapi_pydantic.compat import PYDANTIC_V2
80
85
81
86
# Construct OpenAPI from dict, inferring the correct schema version
82
87
open_api = parse_obj({
@@ -90,7 +95,8 @@ open_api = parse_obj({
90
95
91
96
92
97
# Construct OpenAPI v3.1.0 schema from dict
93
- open_api = OpenAPI.parse_obj({
98
+ openapi_validate = OpenAPI.model_validate if PYDANTIC_V2 else OpenAPI.parse_obj
99
+ open_api = openapi_validate({
94
100
" info" : {" title" : " My own API" , " version" : " v0.0.1" },
95
101
" paths" : {
96
102
" /ping" : {
@@ -100,7 +106,8 @@ open_api = OpenAPI.parse_obj({
100
106
})
101
107
102
108
# Construct OpenAPI with mix of dict/object
103
- open_api = OpenAPI.parse_obj({
109
+ openapi_validate = OpenAPI.model_validate if PYDANTIC_V2 else OpenAPI.parse_obj
110
+ open_api = openapi_validate({
104
111
" info" : {" title" : " My own API" , " version" : " v0.0.1" },
105
112
" paths" : {
106
113
" /ping" : PathItem(
@@ -113,10 +120,10 @@ open_api = OpenAPI.parse_obj({
113
120
## Use Pydantic classes as schema
114
121
115
122
- 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
-
123
+ in OpenAPI has definitions and tweaks in JSON Schema, which are hard to comprehend and define a good data class
124
+ - Pydantic already has a good way to [ create JSON schema] ( https://pydantic-docs.helpmanual.io/usage/schema/ ) .
125
+ Let 's not reinvent the wheel.
126
+
120
127
The approach to deal with this:
121
128
122
129
1 . Use ` PydanticSchema ` objects to represent the ` Schema ` in ` OpenAPI ` object
@@ -126,10 +133,12 @@ The approach to deal with this:
126
133
from pydantic import BaseModel, Field
127
134
128
135
from openapi_pydantic import OpenAPI
136
+ from openapi_pydantic.compat import PYDANTIC_V2
129
137
from openapi_pydantic.util import PydanticSchema, construct_open_api_with_schema_class
130
138
131
139
def construct_base_open_api () -> OpenAPI:
132
- return OpenAPI.parse_obj({
140
+ openapi_validate = OpenAPI.model_validate if PYDANTIC_V2 else OpenAPI.parse_obj
141
+ return openapi_validate({
133
142
" info" : {" title" : " My own API" , " version" : " v0.0.1" },
134
143
" paths" : {
135
144
" /ping" : {
@@ -162,7 +171,10 @@ open_api = construct_base_open_api()
162
171
open_api = construct_open_api_with_schema_class(open_api)
163
172
164
173
# print the result openapi.json
165
- print (open_api.json(by_alias = True , exclude_none = True , indent = 2 ))
174
+ if PYDANTIC_V2 :
175
+ print (open_api.model_dump_json(by_alias = True , exclude_none = True , indent = 2 ))
176
+ else :
177
+ print (open_api.json(by_alias = True , exclude_none = True , indent = 2 ))
166
178
```
167
179
168
180
Result:
@@ -259,21 +271,24 @@ Result:
259
271
260
272
## Notes
261
273
262
- ### Use of OpenAPI.json() / OpenAPI.dict()
274
+ ### Use of OpenAPI.model_dump() / OpenAPI.model_dump_json() / OpenAPI. json() / OpenAPI.dict()
263
275
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.
276
+ When using ` OpenAPI.model_dump() ` / ` OpenAPI.model_dump_json() ` / ` OpenAPI. json()` / ` OpenAPI.dict() ` functions ,
277
+ the arguments ` by_alias=True, exclude_none=True ` have to be in place.
278
+ Otherwise the resulting json will not fit the OpenAPI standard.
267
279
268
280
``` python
269
- # OK
281
+ # OK (Pydantic 2)
282
+ open_api.model_dump_json(by_alias = True , exclude_none = True , indent = 2 )
283
+ # OK (Pydantic 1)
270
284
open_api.json(by_alias = True , exclude_none = True , indent = 2 )
271
285
272
286
# Not good
287
+ open_api.model_dump_json(indent = 2 )
273
288
open_api.json(indent = 2 )
274
289
```
275
290
276
- More info about field alias :
291
+ More info about field aliases :
277
292
278
293
| OpenAPI version | Field alias info |
279
294
| --------------- | ---------------- |
@@ -293,7 +308,7 @@ Please refer to the following for more info:
293
308
### Use OpenAPI 3.0.3 instead of 3.1.0
294
309
295
310
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:
311
+ The old 3.0.3 version is available by importing from different paths:
297
312
298
313
``` python
299
314
from openapi_pydantic.v3.v3_0_3 import OpenAPI, ...
0 commit comments