Skip to content

Commit 0590dc7

Browse files
Fix incorrect Swagger return type when using IResult (#34197)
When a POCO is wrapped in an IResult, an incorrect return type is used to determine the default API response format, which results in the response being mapped correctly. This then results in the metadata specified by an [ProducesResponseType] attribute not being used.
1 parent c754959 commit 0590dc7

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/Mvc/Mvc.ApiExplorer/src/EndpointMetadataApiDescriptionProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ private static void AddSupportedResponseTypes(
265265
{
266266
AddResponseContentTypes(apiResponseType.ApiResponseFormats, contentTypes);
267267
}
268-
else if (CreateDefaultApiResponseFormat(responseType) is { } defaultResponseFormat)
268+
else if (CreateDefaultApiResponseFormat(apiResponseType.Type) is { } defaultResponseFormat)
269269
{
270270
apiResponseType.ApiResponseFormats.Add(defaultResponseFormat);
271271
}

src/Mvc/Mvc.ApiExplorer/test/EndpointMetadataApiDescriptionProviderTest.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public void AddsResponseFormatFromMetadata()
179179
}
180180

181181
[Fact]
182-
public void AddsMultipleResponseFormatsFromMetadata()
182+
public void AddsMultipleResponseFormatsFromMetadataWithPoco()
183183
{
184184
var apiDescription = GetApiDescription(
185185
[ProducesResponseType(typeof(TimeSpan), StatusCodes.Status201Created)]
@@ -207,6 +207,34 @@ public void AddsMultipleResponseFormatsFromMetadata()
207207
Assert.Equal("application/json", badRequestResponseFormat.MediaType);
208208
}
209209

210+
[Fact]
211+
public void AddsMultipleResponseFormatsFromMetadataWithIResult()
212+
{
213+
var apiDescription = GetApiDescription(
214+
[ProducesResponseType(typeof(InferredJsonClass), StatusCodes.Status201Created)]
215+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
216+
() => Results.Ok(new InferredJsonClass()));
217+
218+
Assert.Equal(2, apiDescription.SupportedResponseTypes.Count);
219+
220+
var createdResponseType = apiDescription.SupportedResponseTypes[0];
221+
222+
Assert.Equal(201, createdResponseType.StatusCode);
223+
Assert.Equal(typeof(InferredJsonClass), createdResponseType.Type);
224+
Assert.Equal(typeof(InferredJsonClass), createdResponseType.ModelMetadata.ModelType);
225+
226+
var createdResponseFormat = Assert.Single(createdResponseType.ApiResponseFormats);
227+
Assert.Equal("application/json", createdResponseFormat.MediaType);
228+
229+
var badRequestResponseType = apiDescription.SupportedResponseTypes[1];
230+
231+
Assert.Equal(400, badRequestResponseType.StatusCode);
232+
Assert.Equal(typeof(void), badRequestResponseType.Type);
233+
Assert.Equal(typeof(void), badRequestResponseType.ModelMetadata.ModelType);
234+
235+
Assert.Empty(badRequestResponseType.ApiResponseFormats);
236+
}
237+
210238
[Fact]
211239
public void AddsFromRouteParameterAsPath()
212240
{

src/Mvc/Mvc.ApiExplorer/test/Microsoft.AspNetCore.Mvc.ApiExplorer.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<Reference Include="Microsoft.AspNetCore.Http.Results" />
910
<Reference Include="Microsoft.AspNetCore.Mvc.ApiExplorer" />
1011
<Reference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" />
1112
<ProjectReference Include="..\..\shared\Mvc.Core.TestCommon\Microsoft.AspNetCore.Mvc.Core.TestCommon.csproj" />

0 commit comments

Comments
 (0)