@@ -503,8 +503,23 @@ def _get_openapi_path(
503
503
if request_body_oai :
504
504
operation ["requestBody" ] = request_body_oai
505
505
506
+ # Validation failure response (422) will always be part of the schema
507
+ default_responses : Dict [int , OpenAPIResponse ] = {
508
+ 422 : {
509
+ "description" : "Validation Error" ,
510
+ "content" : {
511
+ "application/json" : {
512
+ "schema" : {"$ref" : COMPONENT_REF_PREFIX + "HTTPValidationError" },
513
+ },
514
+ },
515
+ },
516
+ }
517
+
506
518
# Add the response to the OpenAPI operation
507
519
if self .responses :
520
+ # Merge default responses with user responses
521
+ self .responses = {** default_responses , ** self .responses }
522
+
508
523
for status_code in list (self .responses ):
509
524
response = self .responses [status_code ]
510
525
@@ -552,8 +567,7 @@ def _get_openapi_path(
552
567
operation ["responses" ] = self .responses
553
568
else :
554
569
# Set the default 200 response
555
- responses = operation .setdefault ("responses" , {})
556
- success_response = responses .setdefault (200 , {})
570
+ success_response = default_responses .setdefault (200 , {})
557
571
success_response ["description" ] = self .response_description or _DEFAULT_OPENAPI_RESPONSE_DESCRIPTION
558
572
success_response ["content" ] = {"application/json" : {"schema" : {}}}
559
573
json_response = success_response ["content" ].setdefault ("application/json" , {})
@@ -567,24 +581,16 @@ def _get_openapi_path(
567
581
),
568
582
)
569
583
570
- # Add validation failure response (422)
571
- operation ["responses" ][422 ] = {
572
- "description" : "Validation Error" ,
573
- "content" : {
574
- "application/json" : {
575
- "schema" : {"$ref" : COMPONENT_REF_PREFIX + "HTTPValidationError" },
576
- },
577
- },
578
- }
584
+ operation ["responses" ] = default_responses
579
585
580
- # Add the validation error schema to the definitions, but only if it hasn't been added yet
581
- if "ValidationError" not in definitions :
582
- definitions .update (
583
- {
584
- "ValidationError" : validation_error_definition ,
585
- "HTTPValidationError" : validation_error_response_definition ,
586
- },
587
- )
586
+ # Add the validation error schema to the definitions, but only if it hasn't been added yet
587
+ if "ValidationError" not in definitions :
588
+ definitions .update (
589
+ {
590
+ "ValidationError" : validation_error_definition ,
591
+ "HTTPValidationError" : validation_error_response_definition ,
592
+ },
593
+ )
588
594
589
595
path [self .method .lower ()] = operation
590
596
0 commit comments