2
2
3
3
from dataclasses import dataclass , field
4
4
from enum import Enum
5
- from typing import Dict , List , Optional , Set , Iterable , Generator
5
+ from typing import Any , Dict , Generator , Iterable , List , Optional , Set
6
6
7
- from .properties import Property , property_from_dict , ListProperty , RefProperty , EnumProperty
7
+ from .properties import EnumProperty , ListProperty , Property , RefProperty , property_from_dict
8
8
from .reference import Reference
9
- from .responses import Response , response_from_dict
9
+ from .responses import ListRefResponse , RefResponse , Response , response_from_dict
10
10
11
11
12
12
class ParameterLocation (str , Enum ):
@@ -30,7 +30,7 @@ class EndpointCollection:
30
30
relative_imports : Set [str ] = field (default_factory = set )
31
31
32
32
@staticmethod
33
- def from_dict (d : Dict [str , Dict [str , Dict ]], / ) -> Dict [str , EndpointCollection ]:
33
+ def from_dict (d : Dict [str , Dict [str , Dict [ str , Any ] ]], / ) -> Dict [str , EndpointCollection ]:
34
34
""" Parse the openapi paths data to get EndpointCollections by tag """
35
35
endpoints_by_tag : Dict [str , EndpointCollection ] = {}
36
36
for path , path_data in d .items ():
@@ -55,6 +55,10 @@ def from_dict(d: Dict[str, Dict[str, Dict]], /) -> Dict[str, EndpointCollection]
55
55
56
56
for code , response_dict in method_data ["responses" ].items ():
57
57
response = response_from_dict (status_code = int (code ), data = response_dict )
58
+ if isinstance (response , (RefResponse , ListRefResponse )):
59
+ collection .relative_imports .add (
60
+ import_string_from_reference (response .reference , prefix = "..models" )
61
+ )
58
62
responses .append (response )
59
63
form_body_reference = None
60
64
if "requestBody" in method_data :
@@ -69,7 +73,7 @@ def from_dict(d: Dict[str, Dict[str, Dict]], /) -> Dict[str, EndpointCollection]
69
73
path_parameters = path_parameters ,
70
74
responses = responses ,
71
75
form_body_reference = form_body_reference ,
72
- requires_security = method_data .get ("security" ),
76
+ requires_security = bool ( method_data .get ("security" ) ),
73
77
)
74
78
75
79
collection .endpoints .append (endpoint )
@@ -97,7 +101,7 @@ class Endpoint:
97
101
form_body_reference : Optional [Reference ]
98
102
99
103
@staticmethod
100
- def parse_request_body (body : Dict , / ) -> Optional [Reference ]:
104
+ def parse_request_body (body : Dict [ str , Any ] , / ) -> Optional [Reference ]:
101
105
""" Return form_body_ref """
102
106
form_body_reference = None
103
107
body_content = body ["content" ]
@@ -122,7 +126,7 @@ class Schema:
122
126
relative_imports : Set [str ]
123
127
124
128
@staticmethod
125
- def from_dict (d : Dict , / ) -> Schema :
129
+ def from_dict (d : Dict [ str , Any ] , / ) -> Schema :
126
130
""" A single Schema from its dict representation """
127
131
required_set = set (d .get ("required" , []))
128
132
required_properties : List [Property ] = []
@@ -148,7 +152,7 @@ def from_dict(d: Dict, /) -> Schema:
148
152
return schema
149
153
150
154
@staticmethod
151
- def dict (d : Dict , / ) -> Dict [str , Schema ]:
155
+ def dict (d : Dict [ str , Dict [ str , Any ]] , / ) -> Dict [str , Schema ]:
152
156
""" Get a list of Schemas from an OpenAPI dict """
153
157
result = {}
154
158
for data in d .values ():
@@ -164,7 +168,7 @@ class OpenAPI:
164
168
title : str
165
169
description : str
166
170
version : str
167
- security_schemes : Dict
171
+ # security_schemes: Dict
168
172
schemas : Dict [str , Schema ]
169
173
endpoint_collections_by_tag : Dict [str , EndpointCollection ]
170
174
enums : Dict [str , EnumProperty ]
@@ -173,7 +177,7 @@ class OpenAPI:
173
177
def check_enums (schemas : Iterable [Schema ], collections : Iterable [EndpointCollection ]) -> Dict [str , EnumProperty ]:
174
178
enums : Dict [str , EnumProperty ] = {}
175
179
176
- def _iterate_properties () -> Generator [Property ]:
180
+ def _iterate_properties () -> Generator [Property , None , None ]:
177
181
for schema in schemas :
178
182
yield from schema .required_properties
179
183
yield from schema .optional_properties
@@ -196,7 +200,7 @@ def _iterate_properties() -> Generator[Property]:
196
200
return enums
197
201
198
202
@staticmethod
199
- def from_dict (d : Dict , / ) -> OpenAPI :
203
+ def from_dict (d : Dict [ str , Dict [ str , Any ]] , / ) -> OpenAPI :
200
204
""" Create an OpenAPI from dict """
201
205
schemas = Schema .dict (d ["components" ]["schemas" ])
202
206
endpoint_collections_by_tag = EndpointCollection .from_dict (d ["paths" ])
@@ -208,6 +212,6 @@ def from_dict(d: Dict, /) -> OpenAPI:
208
212
version = d ["info" ]["version" ],
209
213
endpoint_collections_by_tag = endpoint_collections_by_tag ,
210
214
schemas = schemas ,
211
- security_schemes = d ["components" ]["securitySchemes" ],
215
+ # security_schemes=d["components"]["securitySchemes"],
212
216
enums = enums ,
213
217
)
0 commit comments