@@ -503,6 +503,18 @@ 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
+ operation_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 :
508
520
for status_code in list (self .responses ):
@@ -549,45 +561,34 @@ def _get_openapi_path(
549
561
550
562
response ["content" ][content_type ] = new_payload
551
563
552
- operation ["responses" ] = self .responses
564
+ # Merge the user provided response with the default responses
565
+ operation_responses [status_code ] = response
553
566
else :
554
567
# Set the default 200 response
555
- responses = operation .setdefault ("responses" , {})
556
- success_response = responses .setdefault (200 , {})
557
- success_response ["description" ] = self .response_description or _DEFAULT_OPENAPI_RESPONSE_DESCRIPTION
558
- success_response ["content" ] = {"application/json" : {"schema" : {}}}
559
- json_response = success_response ["content" ].setdefault ("application/json" , {})
560
-
561
- # Add the response schema to the OpenAPI 200 response
562
- json_response .update (
563
- self ._openapi_operation_return (
564
- param = dependant .return_param ,
565
- model_name_map = model_name_map ,
566
- field_mapping = field_mapping ,
567
- ),
568
+ response_schema = self ._openapi_operation_return (
569
+ param = dependant .return_param ,
570
+ model_name_map = model_name_map ,
571
+ field_mapping = field_mapping ,
568
572
)
569
573
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
- },
574
+ # Add the response schema to the OpenAPI 200 response
575
+ operation_responses [200 ] = {
576
+ "description" : self .response_description or _DEFAULT_OPENAPI_RESPONSE_DESCRIPTION ,
577
+ "content" : {"application/json" : response_schema },
578
578
}
579
579
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
- )
588
-
580
+ operation ["responses" ] = operation_responses
589
581
path [self .method .lower ()] = operation
590
582
583
+ # Add the validation error schema to the definitions, but only if it hasn't been added yet
584
+ if "ValidationError" not in definitions :
585
+ definitions .update (
586
+ {
587
+ "ValidationError" : validation_error_definition ,
588
+ "HTTPValidationError" : validation_error_response_definition ,
589
+ },
590
+ )
591
+
591
592
# Generate the response schema
592
593
return path , definitions
593
594
0 commit comments