Skip to content

Support resolving OpenAPI server URLs from HttpRequest #60617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static IEndpointConventionBuilder MapOpenApi(this IEndpointRouteBuilder e
}
else
{
var document = await documentService.GetOpenApiDocumentAsync(context.RequestServices, context.RequestAborted);
var document = await documentService.GetOpenApiDocumentAsync(context.RequestServices, context.Request, context.RequestAborted);
var documentOptions = options.Get(lowercasedDocumentName);
using var output = MemoryBufferWriter.Get();
using var writer = Utf8BufferTextWriter.Get(output);
Expand Down
23 changes: 19 additions & 4 deletions src/OpenApi/src/Services/OpenApiDocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Http.Metadata;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
Expand Down Expand Up @@ -56,7 +57,7 @@ internal sealed class OpenApiDocumentService(
internal bool TryGetCachedOperationTransformerContext(string descriptionId, [NotNullWhen(true)] out OpenApiOperationTransformerContext? context)
=> _operationTransformerContextCache.TryGetValue(descriptionId, out context);

public async Task<OpenApiDocument> GetOpenApiDocumentAsync(IServiceProvider scopedServiceProvider, CancellationToken cancellationToken = default)
public async Task<OpenApiDocument> GetOpenApiDocumentAsync(IServiceProvider scopedServiceProvider, HttpRequest? httpRequest = null, CancellationToken cancellationToken = default)
{
// Schema and operation transformers are scoped per-request and can be
// pre-allocated to hold the same number of transformers as the associated
Expand All @@ -71,7 +72,7 @@ public async Task<OpenApiDocument> GetOpenApiDocumentAsync(IServiceProvider scop
var document = new OpenApiDocument
{
Info = GetOpenApiInfo(),
Servers = GetOpenApiServers()
Servers = GetOpenApiServers(httpRequest)
};
document.Paths = await GetOpenApiPathsAsync(document, scopedServiceProvider, operationTransformers, schemaTransformers, cancellationToken);
document.Tags = document.Tags?.Distinct(OpenApiTagComparer.Instance).ToList();
Expand Down Expand Up @@ -198,12 +199,26 @@ internal OpenApiInfo GetOpenApiInfo()
};
}

internal List<OpenApiServer> GetOpenApiServers()
// Resolve server URL from the request to handle reverse proxies.
// If there is active request object, assume a development environment and use the server addresses.
internal List<OpenApiServer> GetOpenApiServers(HttpRequest? httpRequest = null)
{
if (httpRequest is not null)
{
var serverUrl = UriHelper.BuildAbsolute(httpRequest.Scheme, httpRequest.Host, httpRequest.PathBase);
return [new OpenApiServer { Url = serverUrl }];
}
else
{
return GetDevelopmentOpenApiServers();
}
}
private List<OpenApiServer> GetDevelopmentOpenApiServers()
{
if (hostEnvironment.IsDevelopment() &&
server?.Features.Get<IServerAddressesFeature>()?.Addresses is { Count: > 0 } addresses)
{
return addresses.Select(address => new OpenApiServer { Url = address }).ToList();
return [.. addresses.Select(address => new OpenApiServer { Url = address })];
}
return [];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -10,6 +11,45 @@

public partial class OpenApiDocumentServiceTests
{
[Theory]
[InlineData("Development", "localhost:5001", "", "http", "http://localhost:5001/")]
[InlineData("Development", "example.com", "/api", "https", "https://example.com/api")]
[InlineData("Staging", "localhost:5002", "/v1", "http", "http://localhost:5002/v1")]
[InlineData("Staging", "api.example.com", "/base/path", "https", "https://api.example.com/base/path")]
[InlineData("Development", "localhost", "/", "http", "http://localhost/")]
public void GetOpenApiServers_FavorsHttpContextRequestOverServerAddress(string environment, string host, string pathBase, string scheme, string expectedUri)
{
// Arrange
var hostEnvironment = new HostingEnvironment
{
ApplicationName = "TestApplication",
EnvironmentName = environment
};
var docService = new OpenApiDocumentService(
"v1",
new Mock<IApiDescriptionGroupCollectionProvider>().Object,
hostEnvironment,
GetMockOptionsMonitor(),
new Mock<IKeyedServiceProvider>().Object,
new OpenApiTestServer(["http://localhost:5000"]));
var httpContext = new DefaultHttpContext()
{
Request =
{
Host = new HostString(host),
PathBase = pathBase,
Scheme = scheme

}
};

// Act
var servers = docService.GetOpenApiServers(httpContext.Request);

// Assert
Assert.Contains(expectedUri, servers.Select(s => s.Url));
}

[Fact]
public void GetOpenApiServers_HandlesServerAddressFeatureWithValues()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static async Task VerifyOpenApiDocument(IEndpointRouteBuilder builder, Op
{
var documentService = CreateDocumentService(builder, openApiOptions);
var scopedService = ((TestServiceProvider)builder.ServiceProvider).CreateScope();
var document = await documentService.GetOpenApiDocumentAsync(scopedService.ServiceProvider, cancellationToken);
var document = await documentService.GetOpenApiDocumentAsync(scopedService.ServiceProvider, null, cancellationToken);
verifyOpenApiDocument(document);
}

Expand All @@ -43,7 +43,7 @@ public static async Task VerifyOpenApiDocument(ActionDescriptor action, Action<O
var builder = CreateBuilder();
var documentService = CreateDocumentService(builder, action);
var scopedService = ((TestServiceProvider)builder.ServiceProvider).CreateScope();
var document = await documentService.GetOpenApiDocumentAsync(scopedService.ServiceProvider, cancellationToken);
var document = await documentService.GetOpenApiDocumentAsync(scopedService.ServiceProvider, null, cancellationToken);
verifyOpenApiDocument(document);
}

Expand Down
Loading