1
1
import logging
2
- from typing import Any , Generic , List , Optional , Set , Type , TypeVar , cast
2
+ from typing import TYPE_CHECKING , Any , Generic , List , Optional , Set , Type , TypeVar , cast
3
3
4
4
from pydantic import BaseModel
5
5
@@ -44,6 +44,36 @@ def get_mode(
44
44
return cast (JsonSchemaMode , mode )
45
45
46
46
47
+ if TYPE_CHECKING :
48
+
49
+ class GenerateOpenAPI30Schema :
50
+ ...
51
+
52
+ elif PYDANTIC_V2 :
53
+ from pydantic .json_schema import GenerateJsonSchema , JsonSchemaValue
54
+ from pydantic_core import core_schema
55
+
56
+ class GenerateOpenAPI30Schema (GenerateJsonSchema ):
57
+ """Modify the schema generation for OpenAPI 3.0.
58
+
59
+ In OpenAPI 3.0, types can not be None, but a special "nullable"
60
+ field is available.
61
+ """
62
+
63
+ def nullable_schema (
64
+ self ,
65
+ schema : core_schema .NullableSchema ,
66
+ ) -> JsonSchemaValue :
67
+ inner_json_schema = self .generate_inner (schema ["schema" ])
68
+ inner_json_schema ["nullable" ] = True
69
+ return inner_json_schema
70
+
71
+ else :
72
+
73
+ class GenerateOpenAPI30Schema :
74
+ ...
75
+
76
+
47
77
def construct_open_api_with_schema_class (
48
78
open_api : OpenAPI ,
49
79
schema_classes : Optional [List [Type [BaseModel ]]] = None ,
@@ -89,6 +119,7 @@ def construct_open_api_with_schema_class(
89
119
[(c , get_mode (c )) for c in schema_classes ],
90
120
by_alias = by_alias ,
91
121
ref_template = ref_template ,
122
+ schema_generator = GenerateOpenAPI30Schema ,
92
123
)
93
124
else :
94
125
schema_definitions = v1_schema (
@@ -104,22 +135,25 @@ def construct_open_api_with_schema_class(
104
135
f'"{ existing_key } " already exists in { ref_prefix } . '
105
136
f'The value of "{ ref_prefix } { existing_key } " will be overwritten.'
106
137
)
107
- new_open_api .components .schemas .update (
108
- {
109
- key : schema_validate (schema_dict )
110
- for key , schema_dict in schema_definitions [DEFS_KEY ].items ()
111
- }
112
- )
138
+ new_open_api .components .schemas .update (_validate_schemas (schema_definitions ))
113
139
else :
114
- for key , schema_dict in schema_definitions [DEFS_KEY ].items ():
115
- schema_validate (schema_dict )
116
- new_open_api .components .schemas = {
117
- key : schema_validate (schema_dict )
118
- for key , schema_dict in schema_definitions [DEFS_KEY ].items ()
119
- }
140
+ new_open_api .components .schemas = _validate_schemas (schema_definitions )
120
141
return new_open_api
121
142
122
143
144
+ def _validate_schemas (
145
+ schema_definitions : dict [str , Any ]
146
+ ) -> dict [str , Reference | Schema ]:
147
+ """Convert JSON Schema definitions to parsed OpenAPI objects"""
148
+ # Note: if an error occurs in schema_validate(), it may indicate that
149
+ # the generated JSON schemas are not compatible with the version
150
+ # of OpenAPI this module depends on.
151
+ return {
152
+ key : schema_validate (schema_dict )
153
+ for key , schema_dict in schema_definitions [DEFS_KEY ].items ()
154
+ }
155
+
156
+
123
157
def _handle_pydantic_schema (open_api : OpenAPI ) -> List [Type [BaseModel ]]:
124
158
"""
125
159
This function traverses the `OpenAPI` object and
0 commit comments