@@ -18,7 +18,6 @@ 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
22
21
23
22
# Construct OpenAPI by pydantic objects
24
23
open_api = OpenAPI(
@@ -38,10 +37,8 @@ open_api = OpenAPI(
38
37
)
39
38
},
40
39
)
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 ))
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 ))
45
42
```
46
43
47
44
Result:
@@ -81,7 +78,6 @@ The following examples give the same OpenAPI result as above:
81
78
82
79
``` python
83
80
from openapi_pydantic import parse_obj, OpenAPI, PathItem, Response
84
- from openapi_pydantic.compat import PYDANTIC_V2
85
81
86
82
# Construct OpenAPI from dict, inferring the correct schema version
87
83
open_api = parse_obj({
@@ -96,8 +92,8 @@ open_api = parse_obj({
96
92
97
93
98
94
# Construct OpenAPI v3.1.0 schema from dict
99
- openapi_validate = OpenAPI.model_validate if PYDANTIC_V2 else OpenAPI. parse_obj
100
- open_api = openapi_validate ({
95
+ # Note: for Pydantic 1.x, replace `model_validate` with ` parse_obj`
96
+ open_api = OpenAPI.model_validate ({
101
97
" info" : {" title" : " My own API" , " version" : " v0.0.1" },
102
98
" paths" : {
103
99
" /ping" : {
@@ -107,8 +103,8 @@ open_api = openapi_validate({
107
103
})
108
104
109
105
# Construct OpenAPI with mix of dict/object
110
- openapi_validate = OpenAPI.model_validate if PYDANTIC_V2 else OpenAPI. parse_obj
111
- open_api = openapi_validate ({
106
+ # Note: for Pydantic 1.x, replace `model_validate` with ` parse_obj`
107
+ open_api = OpenAPI.model_validate ({
112
108
" info" : {" title" : " My own API" , " version" : " v0.0.1" },
113
109
" paths" : {
114
110
" /ping" : PathItem(
@@ -134,12 +130,11 @@ The approach to deal with this:
134
130
from pydantic import BaseModel, Field
135
131
136
132
from openapi_pydantic import OpenAPI
137
- from openapi_pydantic.compat import PYDANTIC_V2
138
133
from openapi_pydantic.util import PydanticSchema, construct_open_api_with_schema_class
139
134
140
135
def construct_base_open_api () -> OpenAPI:
141
- openapi_validate = OpenAPI.model_validate if PYDANTIC_V2 else OpenAPI. parse_obj
142
- return openapi_validate ({
136
+ # Note: for Pydantic 1.x, replace `model_validate` with ` parse_obj`
137
+ return OpenAPI.model_validate ({
143
138
" info" : {" title" : " My own API" , " version" : " v0.0.1" },
144
139
" paths" : {
145
140
" /ping" : {
@@ -172,10 +167,8 @@ open_api = construct_base_open_api()
172
167
open_api = construct_open_api_with_schema_class(open_api)
173
168
174
169
# print the result openapi.json
175
- if PYDANTIC_V2 :
176
- print (open_api.model_dump_json(by_alias = True , exclude_none = True , indent = 2 ))
177
- else :
178
- 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 ))
179
172
```
180
173
181
174
Result:
0 commit comments