Skip to content

Commit 53f59de

Browse files
authored
Handle multiple response annotations for the same status code in OpenApi (#41699)
1 parent 74b9063 commit 53f59de

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/OpenApi/src/OpenApiGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private static OpenApiResponses GetOpenApiResponses(MethodInfo method, EndpointM
142142
if (discoveredTypeAnnotation is not null)
143143
{
144144
GenerateDefaultContent(discoveredContentTypeAnnotation, discoveredTypeAnnotation);
145-
eligibileAnnotations.Add(statusCode, (discoveredTypeAnnotation, discoveredContentTypeAnnotation));
145+
eligibileAnnotations[statusCode] = (discoveredTypeAnnotation, discoveredContentTypeAnnotation);
146146
}
147147
}
148148

@@ -182,7 +182,7 @@ private static OpenApiResponses GetOpenApiResponses(MethodInfo method, EndpointM
182182
: discoveredTypeAnnotation;
183183

184184
GenerateDefaultContent(discoveredContentTypeAnnotation, discoveredTypeAnnotation);
185-
eligibileAnnotations.Add(statusCode, (discoveredTypeAnnotation, discoveredContentTypeAnnotation));
185+
eligibileAnnotations[statusCode] = (discoveredTypeAnnotation, discoveredContentTypeAnnotation);
186186
}
187187

188188
if (eligibileAnnotations.Count == 0)

src/OpenApi/test/OpenApiGeneratorTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,26 @@ public void HandlesEndpointWithDescriptionAndSummary_WithAttributes()
769769
Assert.Equal("A summary", operation.Summary);
770770
}
771771

772+
// Test case for https://github.com/dotnet/aspnetcore/issues/41622
773+
[Fact]
774+
public void HandlesEndpointWithMultipleResponses()
775+
{
776+
var operation = GetOpenApiOperation(() => TypedResults.Ok(new InferredJsonClass()),
777+
additionalMetadata: new[]
778+
{
779+
// Metadata added by the `IEndpointMetadataProvider` on `TypedResults.Ok`
780+
new ProducesResponseTypeMetadata(StatusCodes.Status200OK),
781+
// Metadata added by the `Produces<Type>` extension method
782+
new ProducesResponseTypeMetadata(typeof(InferredJsonClass), StatusCodes.Status200OK, "application/json"),
783+
});
784+
785+
var response = Assert.Single(operation.Responses);
786+
var content = Assert.Single(response.Value.Content);
787+
Assert.Equal("object", content.Value.Schema.Type);
788+
Assert.Equal("200", response.Key);
789+
Assert.Equal("application/json", content.Key);
790+
}
791+
772792
private static OpenApiOperation GetOpenApiOperation(
773793
Delegate action,
774794
string pattern = null,

0 commit comments

Comments
 (0)