@@ -40,6 +40,11 @@ json2python-models is a [Python](https://www.python.org/) tool that can generate
4040
4141## Example
4242
43+ ### F1 Season Results
44+
45+ <details ><summary >Show (long code)</summary >
46+ <p >
47+
4348```
4449driver_standings.json
4550[
@@ -121,6 +126,140 @@ class DriverStandings:
121126 driver_standings: List[' DriverStanding' ] = attr.ib()
122127```
123128
129+ </p >
130+ </details >
131+
132+ ### Swagger
133+
134+ <details ><summary >Show (long code)</summary >
135+ <p >
136+
137+ ` swagger.json ` from any online API (I tested file generated by drf-yasg and another one for Spotify API)
138+
139+ It requires a lit bit of tweaking:
140+ * Some fields store routes/models specs as dicts
141+ * There is a lot of optinal fields so we reduce merging threshold
142+
143+ ```
144+ json_to_models -s flat -f attrs -m Swagger testing_tools/swagger.json --dict-keys-fields securityDefinitions paths responses definitions properties --merge percent_50 number
145+ ```
146+
147+ ``` python
148+ import attr
149+ from json_to_models.dynamic_typing import FloatString
150+ from typing import Any, Dict, List, Optional, Union
151+
152+
153+ @attr.s
154+ class Swagger :
155+ swagger: FloatString = attr.ib(converter = FloatString)
156+ info: ' Info' = attr.ib()
157+ host: str = attr.ib()
158+ schemes: List[str ] = attr.ib()
159+ base_path: str = attr.ib()
160+ consumes: List[str ] = attr.ib()
161+ produces: List[str ] = attr.ib()
162+ security_definitions: Dict[str , ' Parameter_SecurityDefinition' ] = attr.ib()
163+ security: List[' Security' ] = attr.ib()
164+ paths: Dict[str , ' Path' ] = attr.ib()
165+ definitions: Dict[str , ' Definition_Schema' ] = attr.ib()
166+
167+
168+ @attr.s
169+ class Info :
170+ title: str = attr.ib()
171+ description: str = attr.ib()
172+ version: str = attr.ib()
173+
174+
175+ @attr.s
176+ class Security :
177+ api_key: Optional[List[Any]] = attr.ib(factory = list )
178+ basic: Optional[List[Any]] = attr.ib(factory = list )
179+
180+
181+ @attr.s
182+ class Path :
183+ parameters: List[' Parameter_SecurityDefinition' ] = attr.ib()
184+ post: Optional[' Delete_Get_Patch_Post_Put' ] = attr.ib(default = None )
185+ get: Optional[' Delete_Get_Patch_Post_Put' ] = attr.ib(default = None )
186+ put: Optional[' Delete_Get_Patch_Post_Put' ] = attr.ib(default = None )
187+ patch: Optional[' Delete_Get_Patch_Post_Put' ] = attr.ib(default = None )
188+ delete: Optional[' Delete_Get_Patch_Post_Put' ] = attr.ib(default = None )
189+
190+
191+ @attr.s
192+ class Property :
193+ type : str = attr.ib()
194+ format : Optional[str ] = attr.ib(default = None )
195+ xnullable: Optional[bool ] = attr.ib(default = None )
196+ items: Optional[' Item_Schema' ] = attr.ib(default = None )
197+
198+
199+ @attr.s
200+ class Property_2E :
201+ type : str = attr.ib()
202+ title: Optional[str ] = attr.ib(default = None )
203+ read_only: Optional[bool ] = attr.ib(default = None )
204+ max_length: Optional[int ] = attr.ib(default = None )
205+ min_length: Optional[int ] = attr.ib(default = None )
206+ items: Optional[' Item' ] = attr.ib(default = None )
207+ enum: Optional[List[str ]] = attr.ib(factory = list )
208+ maximum: Optional[int ] = attr.ib(default = None )
209+ minimum: Optional[int ] = attr.ib(default = None )
210+ format : Optional[str ] = attr.ib(default = None )
211+
212+
213+ @attr.s
214+ class Item :
215+ ref: Optional[str ] = attr.ib(default = None )
216+ title: Optional[str ] = attr.ib(default = None )
217+ type : Optional[str ] = attr.ib(default = None )
218+ max_length: Optional[int ] = attr.ib(default = None )
219+ min_length: Optional[int ] = attr.ib(default = None )
220+
221+
222+ @attr.s
223+ class Parameter_SecurityDefinition :
224+ name: str = attr.ib()
225+ in_: str = attr.ib()
226+ description: Optional[str ] = attr.ib(default = None )
227+ required: Optional[bool ] = attr.ib(default = None )
228+ type : Optional[str ] = attr.ib(default = None )
229+ schema: Optional[' Item_Schema' ] = attr.ib(default = None )
230+
231+
232+ @attr.s
233+ class Delete_Get_Patch_Post_Put :
234+ operation_id: str = attr.ib()
235+ description: str = attr.ib()
236+ parameters: List[' Parameter_SecurityDefinition' ] = attr.ib()
237+ responses: Dict[str , ' Response' ] = attr.ib()
238+ tags: List[str ] = attr.ib()
239+
240+
241+ @attr.s
242+ class Item_Schema :
243+ ref: str = attr.ib()
244+
245+
246+ @attr.s
247+ class Response :
248+ description: str = attr.ib()
249+ schema: Optional[Union[' Definition_Schema' , ' Item_Schema' ]] = attr.ib(default = None )
250+
251+
252+ @attr.s
253+ class Definition_Schema :
254+ ref: Optional[str ] = attr.ib(default = None )
255+ required: Optional[List[str ]] = attr.ib(factory = list )
256+ type : Optional[str ] = attr.ib(default = None )
257+ properties: Optional[Dict[str , Union[' Property_2E' , ' Property' ]]] = attr.ib(factory = dict )
258+ ```
259+
260+ </p >
261+ </details >
262+
124263## Installation
125264
126265| ** Be ware** : this project supports only ` python3.7 ` and higher. |
0 commit comments