Skip to content

Set OpenApiServers object in document #56470

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 6 commits into from
Jul 10, 2024
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
16 changes: 15 additions & 1 deletion src/OpenApi/src/Services/OpenApiDocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.IO.Pipelines;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Metadata;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -29,7 +31,8 @@ internal sealed class OpenApiDocumentService(
IApiDescriptionGroupCollectionProvider apiDescriptionGroupCollectionProvider,
IHostEnvironment hostEnvironment,
IOptionsMonitor<OpenApiOptions> optionsMonitor,
IServiceProvider serviceProvider)
IServiceProvider serviceProvider,
IServer? server = null)
{
private readonly OpenApiOptions _options = optionsMonitor.Get(documentName);
private readonly OpenApiSchemaService _componentService = serviceProvider.GetRequiredKeyedService<OpenApiSchemaService>(documentName);
Expand Down Expand Up @@ -58,6 +61,7 @@ public async Task<OpenApiDocument> GetOpenApiDocumentAsync(CancellationToken can
{
Info = GetOpenApiInfo(),
Paths = await GetOpenApiPathsAsync(capturedTags, cancellationToken),
Servers = GetOpenApiServers(),
Tags = [.. capturedTags]
};
await ApplyTransformersAsync(document, cancellationToken);
Expand Down Expand Up @@ -92,6 +96,16 @@ internal OpenApiInfo GetOpenApiInfo()
};
}

internal List<OpenApiServer> GetOpenApiServers()
{
if (hostEnvironment.IsDevelopment() &&
server?.Features.Get<IServerAddressesFeature>()?.Addresses is { Count: > 0 } addresses)
{
return addresses.Select(address => new OpenApiServer { Url = address }).ToList();
}
return [];
}

/// <summary>
/// Gets the OpenApiPaths for the document based on the ApiDescriptions.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public void GetOpenApiInfo_RespectsHostEnvironmentName()
new Mock<IApiDescriptionGroupCollectionProvider>().Object,
hostEnvironment,
new Mock<IOptionsMonitor<OpenApiOptions>>().Object,
new Mock<IKeyedServiceProvider>().Object);
new Mock<IKeyedServiceProvider>().Object,
new OpenApiTestServer());

// Act
var info = docService.GetOpenApiInfo();
Expand All @@ -45,7 +46,8 @@ public void GetOpenApiInfo_RespectsDocumentName()
new Mock<IApiDescriptionGroupCollectionProvider>().Object,
hostEnvironment,
new Mock<IOptionsMonitor<OpenApiOptions>>().Object,
new Mock<IKeyedServiceProvider>().Object);
new Mock<IKeyedServiceProvider>().Object,
new OpenApiTestServer());

// Act
var info = docService.GetOpenApiInfo();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// 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.Mvc.ApiExplorer;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting.Internal;
using Microsoft.Extensions.Options;
using Moq;

public partial class OpenApiDocumentServiceTests
{
[Fact]
public void GetOpenApiServers_HandlesServerAddressFeatureWithValues()
{
// Arrange
var hostEnvironment = new HostingEnvironment
{
ApplicationName = "TestApplication",
EnvironmentName = "Development"
};
var docService = new OpenApiDocumentService(
"v1",
new Mock<IApiDescriptionGroupCollectionProvider>().Object,
hostEnvironment,
new Mock<IOptionsMonitor<OpenApiOptions>>().Object,
new Mock<IKeyedServiceProvider>().Object,
new OpenApiTestServer(["http://localhost:5000"]));

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

// Assert
Assert.Contains("http://localhost:5000", servers.Select(s => s.Url));
}

[Fact]
public void GetOpenApiServers_HandlesServerAddressFeatureWithMultipleValues()
{
// Arrange
var hostEnvironment = new HostingEnvironment
{
ApplicationName = "TestApplication",
EnvironmentName = "Development"
};
var docService = new OpenApiDocumentService(
"v1",
new Mock<IApiDescriptionGroupCollectionProvider>().Object,
hostEnvironment,
new Mock<IOptionsMonitor<OpenApiOptions>>().Object,
new Mock<IKeyedServiceProvider>().Object,
new OpenApiTestServer(["http://localhost:5000", "http://localhost:5002"]));

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

// Assert
Assert.Contains("http://localhost:5000", servers.Select(s => s.Url));
Assert.Contains("http://localhost:5002", servers.Select(s => s.Url));
}

[Fact]
public void GetOpenApiServers_HandlesNonDevelopmentEnvironment()
{
// Arrange
var hostEnvironment = new HostingEnvironment
{
ApplicationName = "TestApplication",
EnvironmentName = "Production"
};
var docService = new OpenApiDocumentService(
"v1",
new Mock<IApiDescriptionGroupCollectionProvider>().Object,
hostEnvironment,
new Mock<IOptionsMonitor<OpenApiOptions>>().Object,
new Mock<IKeyedServiceProvider>().Object,
new OpenApiTestServer(["http://localhost:5000"]));

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

// Assert
Assert.Empty(servers);
}

[Fact]
public void GetOpenApiServers_HandlesServerAddressFeatureWithNoValues()
{
// Arrange
var hostEnvironment = new HostingEnvironment
{
ApplicationName = "TestApplication",
EnvironmentName = "Development"
};
var docService = new OpenApiDocumentService(
"v2",
new Mock<IApiDescriptionGroupCollectionProvider>().Object,
hostEnvironment,
new Mock<IOptionsMonitor<OpenApiOptions>>().Object,
new Mock<IKeyedServiceProvider>().Object,
new OpenApiTestServer());

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

// Assert
Assert.Empty(servers);
}
}
34 changes: 32 additions & 2 deletions src/OpenApi/test/Services/OpenApiDocumentServiceTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
Expand Down Expand Up @@ -75,7 +78,7 @@ internal static OpenApiDocumentService CreateDocumentService(ActionDescriptor ac

var schemaService = new OpenApiSchemaService("Test", Options.Create(new Microsoft.AspNetCore.Http.Json.JsonOptions()), builder.ServiceProvider, openApiOptions.Object);
((TestServiceProvider)builder.ServiceProvider).TestSchemaService = schemaService;
var documentService = new OpenApiDocumentService("Test", apiDescriptionGroupCollectionProvider, hostEnvironment, openApiOptions.Object, builder.ServiceProvider);
var documentService = new OpenApiDocumentService("Test", apiDescriptionGroupCollectionProvider, hostEnvironment, openApiOptions.Object, builder.ServiceProvider, new OpenApiTestServer());
((TestServiceProvider)builder.ServiceProvider).TestDocumentService = documentService;

return documentService;
Expand All @@ -101,7 +104,7 @@ internal static OpenApiDocumentService CreateDocumentService(IEndpointRouteBuild

var schemaService = new OpenApiSchemaService("Test", Options.Create(new Microsoft.AspNetCore.Http.Json.JsonOptions()), builder.ServiceProvider, options.Object);
((TestServiceProvider)builder.ServiceProvider).TestSchemaService = schemaService;
var documentService = new OpenApiDocumentService("Test", apiDescriptionGroupCollectionProvider, hostEnvironment, options.Object, builder.ServiceProvider);
var documentService = new OpenApiDocumentService("Test", apiDescriptionGroupCollectionProvider, hostEnvironment, options.Object, builder.ServiceProvider, new OpenApiTestServer());
((TestServiceProvider)builder.ServiceProvider).TestDocumentService = documentService;

return documentService;
Expand Down Expand Up @@ -276,4 +279,31 @@ public object GetService(Type serviceType)
return _serviceProvider.GetService(serviceType);
}
}

internal class OpenApiTestServer(string[] addresses = null) : IServer
{
public IFeatureCollection Features => GenerateFeatures();

public void Dispose()
{
return;
}

internal virtual IFeatureCollection GenerateFeatures()
{
var features = new FeatureCollection();
features.Set<IServerAddressesFeature>(new TestServerAddressesFeature { Addresses = addresses });
return features;
}

public Task StartAsync<TContext>(IHttpApplication<TContext> application, CancellationToken cancellationToken) where TContext : notnull => Task.CompletedTask;

public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}

private class TestServerAddressesFeature : IServerAddressesFeature
{
public ICollection<string> Addresses { get; set; }
public bool PreferHostingUrls { get; set; }
}
}
Loading