13
13
"""
14
14
15
15
16
+ class OpenapiExtensions (BaseModel ):
17
+ openapi_extensions : Optional [Dict [str , Any ]] = None
18
+
19
+ # This rule is valid for Pydantic v1 and v2
20
+ # If the 'openapi_extensions' field is present in the 'values' dictionary,
21
+ # update the 'values' dictionary with the contents of 'openapi_extensions',
22
+ # and then remove the 'openapi_extensions' field from the 'values' dictionary
23
+
24
+ if PYDANTIC_V2 :
25
+ model_config = {"extra" : "allow" }
26
+
27
+ @parser_openapi_extension (mode = "before" )
28
+ def serialize_openapi_extension (self ):
29
+ if isinstance (self , dict ) and self .get ("openapi_extensions" ):
30
+ self .update (self .get ("openapi_extensions" ))
31
+ self .pop ("openapi_extensions" , None )
32
+
33
+ return self
34
+
35
+ else :
36
+
37
+ @parser_openapi_extension (pre = False , allow_reuse = True )
38
+ def serialize_openapi_extension (cls , values ):
39
+ if values .get ("openapi_extensions" ):
40
+ values .update (values ["openapi_extensions" ])
41
+ del values ["openapi_extensions" ]
42
+ return values
43
+
44
+ class Config :
45
+ extra = "allow"
46
+
47
+
16
48
# https://swagger.io/specification/#contact-object
17
49
class Contact (BaseModel ):
18
50
name : Optional [str ] = None
@@ -77,33 +109,16 @@ class Config:
77
109
78
110
79
111
# https://swagger.io/specification/#server-object
80
- class Server (BaseModel ):
112
+ class Server (OpenapiExtensions ):
81
113
url : Union [AnyUrl , str ]
82
114
description : Optional [str ] = None
83
115
variables : Optional [Dict [str , ServerVariable ]] = None
84
- openapi_extensions : Optional [Dict [str , Any ]] = None
85
116
86
117
if PYDANTIC_V2 :
87
118
model_config = {"extra" : "allow" }
88
119
89
- @parser_openapi_extension ()
90
- def serialize (self ):
91
- # If the 'openapi_extensions' field is not None, return it
92
- if self .openapi_extensions :
93
- return self .openapi_extensions
94
-
95
120
else :
96
121
97
- # If the 'openapi_extensions' field is present in the 'values' dictionary,
98
- # update the 'values' dictionary with the contents of 'openapi_extensions',
99
- # and then remove the 'openapi_extensions' field from the 'values' dictionary
100
- @parser_openapi_extension (pre = False , allow_reuse = True )
101
- def check_json (cls , values ):
102
- if values .get ("openapi_extensions" ):
103
- values .update (values ["openapi_extensions" ])
104
- del values ["openapi_extensions" ]
105
- return values
106
-
107
122
class Config :
108
123
extra = "allow"
109
124
@@ -396,7 +411,7 @@ class Config:
396
411
397
412
398
413
# https://swagger.io/specification/#operation-object
399
- class Operation (BaseModel ):
414
+ class Operation (OpenapiExtensions ):
400
415
tags : Optional [List [str ]] = None
401
416
summary : Optional [str ] = None
402
417
description : Optional [str ] = None
@@ -410,23 +425,12 @@ class Operation(BaseModel):
410
425
deprecated : Optional [bool ] = None
411
426
security : Optional [List [Dict [str , List [str ]]]] = None
412
427
servers : Optional [List [Server ]] = None
413
- openapi_extensions : Optional [Dict [str , Any ]] = None
414
428
415
429
if PYDANTIC_V2 :
416
430
model_config = {"extra" : "allow" }
417
431
418
432
else :
419
433
420
- # If the 'openapi_extensions' field is present in the 'values' dictionary,
421
- # update the 'values' dictionary with the contents of 'openapi_extensions',
422
- # and then remove the 'openapi_extensions' field from the 'values' dictionary
423
- @parser_openapi_extension (pre = False , allow_reuse = True )
424
- def check_json (cls , values ):
425
- if values .get ("openapi_extensions" ):
426
- values .update (values ["openapi_extensions" ])
427
- del values ["openapi_extensions" ]
428
- return values
429
-
430
434
class Config :
431
435
extra = "allow"
432
436
@@ -464,32 +468,15 @@ class SecuritySchemeType(Enum):
464
468
openIdConnect = "openIdConnect"
465
469
466
470
467
- class SecurityBase (BaseModel ):
471
+ class SecurityBase (OpenapiExtensions ):
468
472
type_ : SecuritySchemeType = Field (alias = "type" )
469
473
description : Optional [str ] = None
470
- openapi_extensions : Optional [Dict [str , Any ]] = None
471
474
472
475
if PYDANTIC_V2 :
473
476
model_config = {"extra" : "allow" , "populate_by_name" : True }
474
477
475
- @parser_openapi_extension ()
476
- def serialize (self ):
477
- # If the 'openapi_extensions' field is not None, return it
478
- if self .openapi_extensions :
479
- return self .openapi_extensions
480
-
481
478
else :
482
479
483
- # If the 'openapi_extensions' field is present in the 'values' dictionary,
484
- # update the 'values' dictionary with the contents of 'openapi_extensions',
485
- # and then remove the 'openapi_extensions' field from the 'values' dictionary
486
- @parser_openapi_extension (pre = False , allow_reuse = True )
487
- def check_json (cls , values ):
488
- if values .get ("openapi_extensions" ):
489
- values .update (values ["openapi_extensions" ])
490
- del values ["openapi_extensions" ]
491
- return values
492
-
493
480
class Config :
494
481
extra = "allow"
495
482
allow_population_by_field_name = True
@@ -602,7 +589,7 @@ class Config:
602
589
603
590
604
591
# https://swagger.io/specification/#openapi-object
605
- class OpenAPI (BaseModel ):
592
+ class OpenAPI (OpenapiExtensions ):
606
593
openapi : str
607
594
info : Info
608
595
jsonSchemaDialect : Optional [str ] = None
@@ -614,23 +601,12 @@ class OpenAPI(BaseModel):
614
601
security : Optional [List [Dict [str , List [str ]]]] = None
615
602
tags : Optional [List [Tag ]] = None
616
603
externalDocs : Optional [ExternalDocumentation ] = None
617
- openapi_extensions : Optional [Dict [str , Any ]] = None
618
604
619
605
if PYDANTIC_V2 :
620
606
model_config = {"extra" : "allow" }
621
607
622
608
else :
623
609
624
- # If the 'openapi_extensions' field is present in the 'values' dictionary,
625
- # update the 'values' dictionary with the contents of 'openapi_extensions',
626
- # and then remove the 'openapi_extensions' field from the 'values' dictionary
627
- @parser_openapi_extension (pre = False , allow_reuse = True )
628
- def check_json (cls , values ):
629
- if values .get ("openapi_extensions" ):
630
- values .update (values ["openapi_extensions" ])
631
- del values ["openapi_extensions" ]
632
- return values
633
-
634
610
class Config :
635
611
extra = "allow"
636
612
0 commit comments