@@ -26,21 +26,27 @@ def construct_open_api_with_schema_class(
26
26
by_alias : bool = True ,
27
27
) -> OpenAPI :
28
28
"""
29
- Construct a new OpenAPI object, with the use of pydantic classes to produce JSON schemas
29
+ Construct a new OpenAPI object, utilising pydantic classes to produce JSON schemas
30
30
31
31
:param open_api: the base `OpenAPI` object
32
- :param schema_classes: pydanitic classes that their schema will be used "#/components/schemas" values
33
- :param scan_for_pydantic_schema_reference: flag to indicate if scanning for `PydanticSchemaReference` class
34
- is needed for "#/components/schemas" value updates
32
+ :param schema_classes: pydanitic classes that their schema will be used
33
+ "#/components/schemas" values
34
+ :param scan_for_pydantic_schema_reference: flag to indicate if scanning for
35
+ `PydanticSchemaReference` class is
36
+ needed for "#/components/schemas" value
37
+ updates
35
38
:param by_alias: construct schema by alias (default is True)
36
39
:return: new OpenAPI object with "#/components/schemas" values updated.
37
- If there is no update in "#/components/schemas" values, the original `open_api` will be returned.
40
+ If there is no update in "#/components/schemas" values, the original
41
+ `open_api` will be returned.
38
42
"""
39
43
new_open_api : OpenAPI = open_api .copy (deep = True )
40
44
if scan_for_pydantic_schema_reference :
41
45
extracted_schema_classes = _handle_pydantic_schema (new_open_api )
42
46
if schema_classes :
43
- schema_classes = list ({* schema_classes , * _handle_pydantic_schema (new_open_api )})
47
+ schema_classes = list (
48
+ {* schema_classes , * _handle_pydantic_schema (new_open_api )}
49
+ )
44
50
else :
45
51
schema_classes = extracted_schema_classes
46
52
@@ -51,7 +57,9 @@ def construct_open_api_with_schema_class(
51
57
logger .debug (f"schema_classes{ schema_classes } " )
52
58
53
59
# update new_open_api with new #/components/schemas
54
- schema_definitions = schema (schema_classes , by_alias = by_alias , ref_prefix = ref_prefix )
60
+ schema_definitions = schema (
61
+ schema_classes , by_alias = by_alias , ref_prefix = ref_prefix
62
+ )
55
63
if not new_open_api .components :
56
64
new_open_api .components = Components ()
57
65
if new_open_api .components .schemas :
@@ -62,11 +70,15 @@ def construct_open_api_with_schema_class(
62
70
f'The value of "{ ref_prefix } { existing_key } " will be overwritten.'
63
71
)
64
72
new_open_api .components .schemas .update (
65
- {key : Schema .parse_obj (schema_dict ) for key , schema_dict in schema_definitions ["definitions" ].items ()}
73
+ {
74
+ key : Schema .parse_obj (schema_dict )
75
+ for key , schema_dict in schema_definitions ["definitions" ].items ()
76
+ }
66
77
)
67
78
else :
68
79
new_open_api .components .schemas = {
69
- key : Schema .parse_obj (schema_dict ) for key , schema_dict in schema_definitions ["definitions" ].items ()
80
+ key : Schema .parse_obj (schema_dict )
81
+ for key , schema_dict in schema_definitions ["definitions" ].items ()
70
82
}
71
83
return new_open_api
72
84
@@ -75,7 +87,8 @@ def _handle_pydantic_schema(open_api: OpenAPI) -> List[Type[BaseModel]]:
75
87
"""
76
88
This function traverses the `OpenAPI` object and
77
89
78
- 1. Replaces the `PydanticSchema` object with `Reference` object, with correct ref value;
90
+ 1. Replaces the `PydanticSchema` object with `Reference` object, with correct ref
91
+ value;
79
92
2. Extracts the involved schema class from `PydanticSchema` object.
80
93
81
94
**This function will mutate the input `OpenAPI` object.**
@@ -92,7 +105,9 @@ def _traverse(obj: Any) -> None:
92
105
for field in fields :
93
106
child_obj = obj .__getattribute__ (field )
94
107
if isinstance (child_obj , PydanticSchema ):
95
- logger .debug (f"PydanticSchema found in { obj .__repr_name__ ()} : { child_obj } " )
108
+ logger .debug (
109
+ f"PydanticSchema found in { obj .__repr_name__ ()} : { child_obj } "
110
+ )
96
111
obj .__setattr__ (field , _construct_ref_obj (child_obj ))
97
112
pydantic_types .add (child_obj .schema_class )
98
113
else :
0 commit comments