@@ -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,142 @@ 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 dataclasses -m Swagger testing_tools/swagger.json
145+ --dict-keys-fields securityDefinitions paths responses definitions properties
146+ --merge percent_50 number
147+ ```
148+
149+ ``` python
150+ from dataclasses import dataclass, field
151+ from json_to_models.dynamic_typing import FloatString
152+ from typing import Any, Dict, List, Optional, Union
153+
154+
155+ @dataclass
156+ class Swagger :
157+ swagger: FloatString
158+ info: ' Info'
159+ host: str
160+ schemes: List[str ]
161+ base_path: str
162+ consumes: List[str ]
163+ produces: List[str ]
164+ security_definitions: Dict[str , ' Parameter_SecurityDefinition' ]
165+ security: List[' Security' ]
166+ paths: Dict[str , ' Path' ]
167+ definitions: Dict[str , ' Definition_Schema' ]
168+
169+
170+ @dataclass
171+ class Info :
172+ title: str
173+ description: str
174+ version: str
175+
176+
177+ @dataclass
178+ class Security :
179+ api_key: Optional[List[Any]] = field(default_factory = list )
180+ basic: Optional[List[Any]] = field(default_factory = list )
181+
182+
183+ @dataclass
184+ class Path :
185+ parameters: List[' Parameter_SecurityDefinition' ]
186+ post: Optional[' Delete_Get_Patch_Post_Put' ] = None
187+ get: Optional[' Delete_Get_Patch_Post_Put' ] = None
188+ put: Optional[' Delete_Get_Patch_Post_Put' ] = None
189+ patch: Optional[' Delete_Get_Patch_Post_Put' ] = None
190+ delete: Optional[' Delete_Get_Patch_Post_Put' ] = None
191+
192+
193+ @dataclass
194+ class Property :
195+ type : str
196+ format : Optional[str ] = None
197+ xnullable: Optional[bool ] = None
198+ items: Optional[' Item_Schema' ] = None
199+
200+
201+ @dataclass
202+ class Property_2E :
203+ type : str
204+ title: Optional[str ] = None
205+ read_only: Optional[bool ] = None
206+ max_length: Optional[int ] = None
207+ min_length: Optional[int ] = None
208+ items: Optional[' Item' ] = None
209+ enum: Optional[List[str ]] = field(default_factory = list )
210+ maximum: Optional[int ] = None
211+ minimum: Optional[int ] = None
212+ format : Optional[str ] = None
213+
214+
215+ @dataclass
216+ class Item :
217+ ref: Optional[str ] = None
218+ title: Optional[str ] = None
219+ type : Optional[str ] = None
220+ max_length: Optional[int ] = None
221+ min_length: Optional[int ] = None
222+
223+
224+ @dataclass
225+ class Parameter_SecurityDefinition :
226+ name: str
227+ in_: str
228+ required: Optional[bool ] = None
229+ schema: Optional[' Item_Schema' ] = None
230+ type : Optional[str ] = None
231+ description: Optional[str ] = None
232+
233+
234+ @dataclass
235+ class Delete_Get_Patch_Post_Put :
236+ operation_id: str
237+ description: str
238+ parameters: List[' Parameter_SecurityDefinition' ]
239+ responses: Dict[str , ' Response' ]
240+ tags: List[str ]
241+
242+
243+ @dataclass
244+ class Item_Schema :
245+ ref: str
246+
247+
248+ @dataclass
249+ class Response :
250+ description: str
251+ schema: Optional[Union[' Item_Schema' , ' Definition_Schema' ]] = None
252+
253+
254+ @dataclass
255+ class Definition_Schema :
256+ ref: Optional[str ] = None
257+ required: Optional[List[str ]] = field(default_factory = list )
258+ type : Optional[str ] = None
259+ properties: Optional[Dict[str , Union[' Property_2E' , ' Property' ]]] = field(default_factory = dict )
260+ ```
261+
262+ </p >
263+ </details >
264+
124265## Installation
125266
126267| ** Be ware** : this project supports only ` python3.7 ` and higher. |
0 commit comments