Skip to content

Commit 811b234

Browse files
committed
Added tests for the issue
1 parent 8491a27 commit 811b234

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,82 @@ public void AddsResponseDescription()
331331
Assert.Equal(expectedBadRequestDescription, badRequestResponseType.Description);
332332
}
333333

334+
[Fact]
335+
public void AddsResponseDescription_WorksWithGenerics()
336+
{
337+
const string expectedOkDescription = "The weather forecast for the next 5 days.";
338+
339+
var apiDescription = GetApiDescription([ProducesResponseType<GenericClass<TimeSpan>>(StatusCodes.Status200OK, Description = "The weather forecast for the next 5 days.")]
340+
() => new GenericClass<TimeSpan> { Value = new TimeSpan() });
341+
342+
var okResponseType = Assert.Single(apiDescription.SupportedResponseTypes);
343+
344+
Assert.Equal(200, okResponseType.StatusCode);
345+
Assert.Equal(typeof(GenericClass<TimeSpan>), okResponseType.Type);
346+
Assert.Equal(typeof(GenericClass<TimeSpan>), okResponseType.ModelMetadata?.ModelType);
347+
Assert.Equal(expectedOkDescription, okResponseType.Description);
348+
349+
var createdOkFormat = Assert.Single(okResponseType.ApiResponseFormats);
350+
Assert.Equal("application/json", createdOkFormat.MediaType);
351+
}
352+
353+
[Fact]
354+
public void AddsResponseDescription_WorksWithGenericsAndTypedResults()
355+
{
356+
const string expectedOkDescription = "The weather forecast for the next 5 days.";
357+
358+
var apiDescription = GetApiDescription([ProducesResponseType<GenericClass<TimeSpan>>(StatusCodes.Status200OK, Description = "The weather forecast for the next 5 days.")]
359+
() => TypedResults.Ok(new GenericClass<TimeSpan> { Value = new TimeSpan() }));
360+
361+
var okResponseType = Assert.Single(apiDescription.SupportedResponseTypes);
362+
363+
Assert.Equal(200, okResponseType.StatusCode);
364+
Assert.Equal(typeof(GenericClass<TimeSpan>), okResponseType.Type);
365+
Assert.Equal(typeof(GenericClass<TimeSpan>), okResponseType.ModelMetadata?.ModelType);
366+
Assert.Equal(expectedOkDescription, okResponseType.Description);
367+
368+
var createdOkFormat = Assert.Single(okResponseType.ApiResponseFormats);
369+
Assert.Equal("application/json", createdOkFormat.MediaType);
370+
}
371+
372+
[Fact]
373+
public void AddsResponseDescription_WorksWithCollections()
374+
{
375+
const string expectedOkDescription = "The weather forecast for the next 5 days.";
376+
377+
var apiDescription = GetApiDescription([ProducesResponseType<IEnumerable<TimeSpan>>(StatusCodes.Status200OK, Description = "The weather forecast for the next 5 days.")]
378+
() => new List<TimeSpan> { new() });
379+
380+
var okResponseType = Assert.Single(apiDescription.SupportedResponseTypes);
381+
382+
Assert.Equal(200, okResponseType.StatusCode);
383+
Assert.Equal(typeof(List<TimeSpan>), okResponseType.Type); // We use List as the inferred type has higher priority than those set by metadata (attributes)
384+
Assert.Equal(typeof(List<TimeSpan>), okResponseType.ModelMetadata?.ModelType);
385+
Assert.Equal(expectedOkDescription, okResponseType.Description);
386+
387+
var createdOkFormat = Assert.Single(okResponseType.ApiResponseFormats);
388+
Assert.Equal("application/json", createdOkFormat.MediaType);
389+
}
390+
391+
[Fact]
392+
public void AddsResponseDescription_WorksWithCollectionsAndTypedResults()
393+
{
394+
const string expectedOkDescription = "The weather forecast for the next 5 days.";
395+
396+
var apiDescription = GetApiDescription([ProducesResponseType<IEnumerable<TimeSpan>>(StatusCodes.Status200OK, Description = "The weather forecast for the next 5 days.")]
397+
() => TypedResults.Ok(new List<TimeSpan> { new() }));
398+
399+
var okResponseType = Assert.Single(apiDescription.SupportedResponseTypes);
400+
401+
Assert.Equal(200, okResponseType.StatusCode);
402+
Assert.Equal(typeof(List<TimeSpan>), okResponseType.Type); // We use List as the inferred type has higher priority than those set by metadata (attributes)
403+
Assert.Equal(typeof(List<TimeSpan>), okResponseType.ModelMetadata?.ModelType);
404+
Assert.Equal(expectedOkDescription, okResponseType.Description);
405+
406+
var createdOkFormat = Assert.Single(okResponseType.ApiResponseFormats);
407+
Assert.Equal("application/json", createdOkFormat.MediaType);
408+
}
409+
334410
[Fact]
335411
public void WithEmptyMethodBody_AddsResponseDescription()
336412
{
@@ -1813,5 +1889,7 @@ private class TestServiceProvider : IServiceProvider
18131889

18141890
return null;
18151891
}
1892+
18161893
}
1894+
private class GenericClass<TType> { public required TType Value { get; set; } }
18171895
}

0 commit comments

Comments
 (0)