@@ -73,19 +73,25 @@ def get_operation(
73
73
doc = inspect .getdoc (func ) or ""
74
74
doc = doc .strip ()
75
75
lines = doc .split ("\n " )
76
- doc_summary = lines [0 ] or None
76
+ doc_summary = lines [0 ]
77
77
78
78
# Determine the summary and description based on provided arguments or docstring
79
79
if summary is None :
80
- doc_description = lines [0 ] if len (lines ) == 0 else "</br>" .join (lines [1 :]) or None
80
+ doc_description = lines [0 ] if len (lines ) == 0 else "</br>" .join (lines [1 :])
81
81
else :
82
- doc_description = "</br>" .join (lines ) or None
82
+ doc_description = "</br>" .join (lines )
83
+
84
+ summary = summary or doc_summary
85
+ description = description or doc_description
83
86
84
87
# Create the operation dictionary with summary and description
85
- operation_dict = dict (
86
- summary = summary or doc_summary ,
87
- description = description or doc_description
88
- )
88
+ operation_dict = {}
89
+
90
+ if summary :
91
+ operation_dict ["summary" ] = summary # type: ignore
92
+
93
+ if description :
94
+ operation_dict ["description" ] = description # type: ignore
89
95
90
96
# Add any additional openapi_extensions to the operation dictionary
91
97
operation_dict .update (openapi_extensions or {})
@@ -136,16 +142,18 @@ def parse_header(header: Type[BaseModel]) -> Tuple[List[Parameter], dict]:
136
142
data = {
137
143
"name" : name ,
138
144
"in" : ParameterInType .HEADER ,
139
- "description" : value .get ("description" ),
140
145
"required" : name in schema .get ("required" , []),
141
146
"schema" : Schema (** value )
142
147
}
143
148
# Parse extra values
144
- data .update ({
145
- "deprecated" : value .get ("deprecated" ),
146
- "example" : value .get ("example" ),
147
- "examples" : value .get ("examples" ),
148
- })
149
+ if "description" in value .keys ():
150
+ data ["description" ] = value .get ("description" )
151
+ if "deprecated" in value .keys ():
152
+ data ["deprecated" ] = value .get ("deprecated" )
153
+ if "example" in value .keys ():
154
+ data ["example" ] = value .get ("example" )
155
+ if "examples" in value .keys ():
156
+ data ["examples" ] = value .get ("examples" )
149
157
parameters .append (Parameter (** data ))
150
158
151
159
# Parse definitions
@@ -167,16 +175,18 @@ def parse_cookie(cookie: Type[BaseModel]) -> Tuple[List[Parameter], dict]:
167
175
data = {
168
176
"name" : name ,
169
177
"in" : ParameterInType .COOKIE ,
170
- "description" : value .get ("description" ),
171
178
"required" : name in schema .get ("required" , []),
172
179
"schema" : Schema (** value )
173
180
}
174
181
# Parse extra values
175
- data .update ({
176
- "deprecated" : value .get ("deprecated" ),
177
- "example" : value .get ("example" ),
178
- "examples" : value .get ("examples" ),
179
- })
182
+ if "description" in value .keys ():
183
+ data ["description" ] = value .get ("description" )
184
+ if "deprecated" in value .keys ():
185
+ data ["deprecated" ] = value .get ("deprecated" )
186
+ if "example" in value .keys ():
187
+ data ["example" ] = value .get ("example" )
188
+ if "examples" in value .keys ():
189
+ data ["examples" ] = value .get ("examples" )
180
190
parameters .append (Parameter (** data ))
181
191
182
192
# Parse definitions
@@ -198,16 +208,18 @@ def parse_path(path: Type[BaseModel]) -> Tuple[List[Parameter], dict]:
198
208
data = {
199
209
"name" : name ,
200
210
"in" : ParameterInType .PATH ,
201
- "description" : value .get ("description" ),
202
211
"required" : True ,
203
212
"schema" : Schema (** value )
204
213
}
205
214
# Parse extra values
206
- data .update ({
207
- "deprecated" : value .get ("deprecated" ),
208
- "example" : value .get ("example" ),
209
- "examples" : value .get ("examples" ),
210
- })
215
+ if "description" in value .keys ():
216
+ data ["description" ] = value .get ("description" )
217
+ if "deprecated" in value .keys ():
218
+ data ["deprecated" ] = value .get ("deprecated" )
219
+ if "example" in value .keys ():
220
+ data ["example" ] = value .get ("example" )
221
+ if "examples" in value .keys ():
222
+ data ["examples" ] = value .get ("examples" )
211
223
parameters .append (Parameter (** data ))
212
224
213
225
# Parse definitions
@@ -229,16 +241,18 @@ def parse_query(query: Type[BaseModel]) -> Tuple[List[Parameter], dict]:
229
241
data = {
230
242
"name" : name ,
231
243
"in" : ParameterInType .QUERY ,
232
- "description" : value .get ("description" ),
233
244
"required" : name in schema .get ("required" , []),
234
245
"schema" : Schema (** value )
235
246
}
236
247
# Parse extra values
237
- data .update ({
238
- "deprecated" : value .get ("deprecated" ),
239
- "example" : value .get ("example" ),
240
- "examples" : value .get ("examples" ),
241
- })
248
+ if "description" in value .keys ():
249
+ data ["description" ] = value .get ("description" )
250
+ if "deprecated" in value .keys ():
251
+ data ["deprecated" ] = value .get ("deprecated" )
252
+ if "example" in value .keys ():
253
+ data ["example" ] = value .get ("example" )
254
+ if "examples" in value .keys ():
255
+ data ["examples" ] = value .get ("examples" )
242
256
parameters .append (Parameter (** data ))
243
257
244
258
# Parse definitions
@@ -269,9 +283,10 @@ def parse_form(
269
283
content = {
270
284
"multipart/form-data" : MediaType (
271
285
schema = Schema (** {"$ref" : f"{ OPENAPI3_REF_PREFIX } /{ title } " }),
272
- encoding = encoding or None
273
286
)
274
287
}
288
+ if encoding :
289
+ content ["multipart/form-data" ].encoding = encoding
275
290
276
291
# Parse definitions
277
292
definitions = schema .get ("$defs" , {})
@@ -333,18 +348,24 @@ def get_responses(
333
348
)})
334
349
335
350
model_config : DefaultDict [str , Any ] = response .model_config # type: ignore
336
- openapi_extra = model_config .get ("openapi_extra" )
351
+ openapi_extra = model_config .get ("openapi_extra" , {} )
337
352
if openapi_extra :
353
+ openapi_extra_keys = openapi_extra .keys ()
338
354
# Add additional information from model_config to the response
339
- _responses [key ].description = openapi_extra .get ("description" )
340
- _responses [key ].headers = openapi_extra .get ("headers" )
341
- _responses [key ].links = openapi_extra .get ("links" )
355
+ if "description" in openapi_extra_keys :
356
+ _responses [key ].description = openapi_extra .get ("description" )
357
+ if "headers" in openapi_extra_keys :
358
+ _responses [key ].headers = openapi_extra .get ("headers" )
359
+ if "links" in openapi_extra_keys :
360
+ _responses [key ].links = openapi_extra .get ("links" )
342
361
_content = _responses [key ].content
343
- if _content is not None :
344
- _content ["application/json" ].example = openapi_extra .get ("example" )
345
- _content ["application/json" ].examples = openapi_extra .get ("examples" )
346
- _content ["application/json" ].encoding = openapi_extra .get ("encoding" )
347
- _content .update (openapi_extra .get ("content" , {}))
362
+ if "example" in openapi_extra_keys :
363
+ _content ["application/json" ].example = openapi_extra .get ("example" ) # type: ignore
364
+ if "examples" in openapi_extra_keys :
365
+ _content ["application/json" ].examples = openapi_extra .get ("examples" ) # type: ignore
366
+ if "encoding" in openapi_extra_keys :
367
+ _content ["application/json" ].encoding = openapi_extra .get ("encoding" ) # type: ignore
368
+ _content .update (openapi_extra .get ("content" , {})) # type: ignore
348
369
349
370
_schemas [name ] = Schema (** schema )
350
371
definitions = schema .get ("$defs" )
@@ -383,8 +404,8 @@ def parse_and_store_tags(
383
404
old_tags .append (tag )
384
405
385
406
# Set the tags attribute of the operation object to a list of unique tag names from new_tags
386
- # If the resulting list is empty, set it to None
387
- operation .tags = list (set ([tag .name for tag in new_tags ])) or None
407
+ # If the resulting list is empty, set it to ["default"]
408
+ operation .tags = list (set ([tag .name for tag in new_tags ])) or [ "default" ]
388
409
389
410
390
411
def parse_parameters (
@@ -459,29 +480,38 @@ def parse_parameters(
459
480
if form :
460
481
_content , _components_schemas = parse_form (form )
461
482
components_schemas .update (** _components_schemas )
462
- request_body = RequestBody (content = _content )
483
+ request_body = RequestBody (content = _content , required = True )
463
484
model_config : DefaultDict [str , Any ] = form .model_config # type: ignore
464
- openapi_extra = model_config .get ("openapi_extra" )
485
+ openapi_extra = model_config .get ("openapi_extra" , {} )
465
486
if openapi_extra :
466
- request_body .description = openapi_extra .get ("description" )
467
- request_body .content ["multipart/form-data" ].example = openapi_extra .get ("example" )
468
- request_body .content ["multipart/form-data" ].examples = openapi_extra .get ("examples" )
469
- if openapi_extra .get ("encoding" ):
487
+ openapi_extra_keys = openapi_extra .keys ()
488
+ if "description" in openapi_extra_keys :
489
+ request_body .description = openapi_extra .get ("description" )
490
+ if "example" in openapi_extra_keys :
491
+ request_body .content ["multipart/form-data" ].example = openapi_extra .get ("example" )
492
+ if "examples" in openapi_extra_keys :
493
+ request_body .content ["multipart/form-data" ].examples = openapi_extra .get ("examples" )
494
+ if "encoding" in openapi_extra_keys :
470
495
request_body .content ["multipart/form-data" ].encoding = openapi_extra .get ("encoding" )
471
496
operation .requestBody = request_body
472
497
473
498
if body :
474
499
_content , _components_schemas = parse_body (body )
475
500
components_schemas .update (** _components_schemas )
476
- request_body = RequestBody (content = _content )
501
+ request_body = RequestBody (content = _content , required = True )
477
502
model_config : DefaultDict [str , Any ] = body .model_config # type: ignore
478
- openapi_extra = model_config .get ("openapi_extra" )
503
+ openapi_extra = model_config .get ("openapi_extra" , {} )
479
504
if openapi_extra :
480
- request_body .description = openapi_extra .get ("description" )
505
+ openapi_extra_keys = openapi_extra .keys ()
506
+ if "description" in openapi_extra_keys :
507
+ request_body .description = openapi_extra .get ("description" )
481
508
request_body .required = openapi_extra .get ("required" , True )
482
- request_body .content ["application/json" ].example = openapi_extra .get ("example" )
483
- request_body .content ["application/json" ].examples = openapi_extra .get ("examples" )
484
- request_body .content ["application/json" ].encoding = openapi_extra .get ("encoding" )
509
+ if "example" in openapi_extra_keys :
510
+ request_body .content ["application/json" ].example = openapi_extra .get ("example" )
511
+ if "examples" in openapi_extra_keys :
512
+ request_body .content ["application/json" ].examples = openapi_extra .get ("examples" )
513
+ if "encoding" in openapi_extra_keys :
514
+ request_body .content ["application/json" ].encoding = openapi_extra .get ("encoding" )
485
515
operation .requestBody = request_body
486
516
487
517
if raw :
@@ -498,8 +528,9 @@ def parse_parameters(
498
528
request_body = RequestBody (content = _content )
499
529
operation .requestBody = request_body
500
530
501
- # Set the parsed parameters in the operation object
502
- operation .parameters = parameters if parameters else None
531
+ if parameters :
532
+ # Set the parsed parameters in the operation object
533
+ operation .parameters = parameters
503
534
504
535
return header , cookie , path , query , form , body , raw
505
536
0 commit comments