Skip to content

Commit 96787dd

Browse files
committed
Replace the PYDANTIC_V2 conditions in the README with comments
1 parent 87241e5 commit 96787dd

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

README.md

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ The naming of the classes follows the schema in
1818

1919
```python
2020
from openapi_pydantic import OpenAPI, Info, PathItem, Operation, Response
21-
from openapi_pydantic.compat import PYDANTIC_V2
2221

2322
# Construct OpenAPI by pydantic objects
2423
open_api = OpenAPI(
@@ -38,10 +37,8 @@ open_api = OpenAPI(
3837
)
3938
},
4039
)
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))
4542
```
4643

4744
Result:
@@ -81,7 +78,6 @@ The following examples give the same OpenAPI result as above:
8178

8279
```python
8380
from openapi_pydantic import parse_obj, OpenAPI, PathItem, Response
84-
from openapi_pydantic.compat import PYDANTIC_V2
8581

8682
# Construct OpenAPI from dict, inferring the correct schema version
8783
open_api = parse_obj({
@@ -96,8 +92,8 @@ open_api = parse_obj({
9692

9793

9894
# 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({
10197
"info": {"title": "My own API", "version": "v0.0.1"},
10298
"paths": {
10399
"/ping": {
@@ -107,8 +103,8 @@ open_api = openapi_validate({
107103
})
108104

109105
# 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({
112108
"info": {"title": "My own API", "version": "v0.0.1"},
113109
"paths": {
114110
"/ping": PathItem(
@@ -134,12 +130,11 @@ The approach to deal with this:
134130
from pydantic import BaseModel, Field
135131

136132
from openapi_pydantic import OpenAPI
137-
from openapi_pydantic.compat import PYDANTIC_V2
138133
from openapi_pydantic.util import PydanticSchema, construct_open_api_with_schema_class
139134

140135
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({
143138
"info": {"title": "My own API", "version": "v0.0.1"},
144139
"paths": {
145140
"/ping": {
@@ -172,10 +167,8 @@ open_api = construct_base_open_api()
172167
open_api = construct_open_api_with_schema_class(open_api)
173168

174169
# 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))
179172
```
180173

181174
Result:

0 commit comments

Comments
 (0)