Skip to content

Commit 9fb5ad7

Browse files
committed
Added integration test validating the model building for endpoint with typed result return type
1 parent f5ab78e commit 9fb5ad7

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/Mvc/test/Mvc.FunctionalTests/ApiExplorerTest.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
using Microsoft.Extensions.Logging;
1212
using System.Reflection;
1313
using Xunit.Abstractions;
14+
using Microsoft.AspNetCore.Mvc.ApplicationModels;
15+
using Microsoft.Extensions.DependencyInjection;
16+
using Microsoft.AspNetCore.Mvc.ApiExplorer;
17+
using Microsoft.AspNetCore.Http.HttpResults;
18+
using Microsoft.AspNetCore.Http;
1419

1520
namespace Microsoft.AspNetCore.Mvc.FunctionalTests;
1621

@@ -1565,6 +1570,26 @@ public async Task ApiExplorer_LogsInvokedDescriptionProvidersOnStartup()
15651570
Assert.Contains(TestSink.Writes, w => w.Message.Equals("Executing API description provider 'JsonPatchOperationsArrayProvider' from assembly Microsoft.AspNetCore.Mvc.NewtonsoftJson v42.42.42.42.", StringComparison.Ordinal));
15661571
}
15671572

1573+
[Fact]
1574+
public void ApiExplorer_BuildsMetadataForActionWithTypedResult()
1575+
{
1576+
var apiDescCollectionProvider = Factory.Server.Services.GetService<IApiDescriptionGroupCollectionProvider>();
1577+
var testGroupName = nameof(ApiExplorerWithTypedResultController).Replace("Controller", string.Empty);
1578+
var group = apiDescCollectionProvider.ApiDescriptionGroups.Items.Where(i => i.GroupName == testGroupName).SingleOrDefault();
1579+
Assert.NotNull(group);
1580+
var apiDescription = Assert.Single<ApiDescription>(group.Items);
1581+
1582+
var responseType = Assert.Single(apiDescription.SupportedResponseTypes);
1583+
Assert.Equal(StatusCodes.Status200OK, responseType.StatusCode);
1584+
Assert.Equal(typeof(Product), responseType.Type);
1585+
1586+
Assert.NotNull(apiDescription.ActionDescriptor.EndpointMetadata);
1587+
var producesResponseTypeMetadata = apiDescription.ActionDescriptor.EndpointMetadata.OfType<ProducesResponseTypeMetadata>().SingleOrDefault();
1588+
Assert.NotNull(producesResponseTypeMetadata);
1589+
Assert.Equal(StatusCodes.Status200OK, producesResponseTypeMetadata.StatusCode);
1590+
Assert.Equal(typeof(Product), producesResponseTypeMetadata.Type);
1591+
}
1592+
15681593
private IEnumerable<string> GetSortedMediaTypes(ApiExplorerResponseType apiResponseType)
15691594
{
15701595
return apiResponseType.ResponseFormats

src/Mvc/test/WebSites/ApiExplorerWebSite/ApiExplorerWebSite.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<ItemGroup>
88
<Reference Include="Microsoft.AspNetCore.Mvc" />
99
<Reference Include="Microsoft.AspNetCore.Mvc.Formatters.Xml" />
10+
<Reference Include="Microsoft.AspNetCore.Http.Results" />
1011
<Reference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" />
1112

1213
<Reference Include="Microsoft.AspNetCore.Server.IISIntegration" />
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.AspNetCore.Http.HttpResults;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
namespace ApiExplorerWebSite;
8+
9+
[Route("ApiExplorerWithTypedResult/[Action]")]
10+
public class ApiExplorerWithTypedResultController : Controller
11+
{
12+
[HttpGet]
13+
public Ok<Product> GetProduct() => TypedResults.Ok(new Product { Name = "Test product" });
14+
}

0 commit comments

Comments
 (0)