diff --git a/Directory.Packages.props b/Directory.Packages.props index 431e84c03d..29848393ea 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -8,12 +8,12 @@ - - + + - - + + diff --git a/src/Swashbuckle.AspNetCore.Cli/Program.cs b/src/Swashbuckle.AspNetCore.Cli/Program.cs index da548a470f..ceea11ceeb 100644 --- a/src/Swashbuckle.AspNetCore.Cli/Program.cs +++ b/src/Swashbuckle.AspNetCore.Cli/Program.cs @@ -255,10 +255,12 @@ private static IServiceProvider GetServiceProvider(Assembly startupAssembly) return host.Services; } +#pragma warning disable ASPDEPR008 if (TryGetCustomHost(startupAssembly, "SwaggerWebHostFactory", "CreateWebHost", out IWebHost webHost)) { return webHost.Services; } +#pragma warning restore ASPDEPR008 try { diff --git a/test/Swashbuckle.AspNetCore.Annotations.Test/AnnotationsDocumentFilterTests.cs b/test/Swashbuckle.AspNetCore.Annotations.Test/AnnotationsDocumentFilterTests.cs index 9349fe26eb..41369ca8cf 100644 --- a/test/Swashbuckle.AspNetCore.Annotations.Test/AnnotationsDocumentFilterTests.cs +++ b/test/Swashbuckle.AspNetCore.Annotations.Test/AnnotationsDocumentFilterTests.cs @@ -1,7 +1,7 @@ using Microsoft.OpenApi.Models; -using Xunit; using Swashbuckle.AspNetCore.SwaggerGen; using Swashbuckle.AspNetCore.TestSupport; +using Xunit; namespace Swashbuckle.AspNetCore.Annotations.Test; @@ -13,7 +13,7 @@ public void Apply_CreatesMetadataForControllerNameTag_FromSwaggerTagAttribute() var document = new OpenApiDocument(); var apiDescription = ApiDescriptionFactory.Create(c => nameof(c.ActionWithNoAttributes)); var filterContext = new DocumentFilterContext( - apiDescriptions: new[] { apiDescription }, + apiDescriptions: [apiDescription], schemaGenerator: null, schemaRepository: null); @@ -24,8 +24,6 @@ public void Apply_CreatesMetadataForControllerNameTag_FromSwaggerTagAttribute() Assert.Equal("http://tempuri.org/", tag.ExternalDocs.Url.ToString()); } - private AnnotationsDocumentFilter Subject() - { - return new AnnotationsDocumentFilter(); - } + private static AnnotationsDocumentFilter Subject() + => new(); } diff --git a/test/Swashbuckle.AspNetCore.Annotations.Test/AnnotationsParameterFilterTests.cs b/test/Swashbuckle.AspNetCore.Annotations.Test/AnnotationsParameterFilterTests.cs index 48af7a7665..6fa6a8ae4f 100644 --- a/test/Swashbuckle.AspNetCore.Annotations.Test/AnnotationsParameterFilterTests.cs +++ b/test/Swashbuckle.AspNetCore.Annotations.Test/AnnotationsParameterFilterTests.cs @@ -61,8 +61,5 @@ public void Apply_DoesNotModifyTheRequiredFlag_IfNotSpecifiedWithSwaggerParamete Assert.True(parameter.Required); } - private AnnotationsParameterFilter Subject() - { - return new AnnotationsParameterFilter(); - } + private static AnnotationsParameterFilter Subject() => new(); } diff --git a/test/Swashbuckle.AspNetCore.Annotations.Test/AnnotationsRequestBodyFilterTests.cs b/test/Swashbuckle.AspNetCore.Annotations.Test/AnnotationsRequestBodyFilterTests.cs index fc2a93f672..374f380391 100644 --- a/test/Swashbuckle.AspNetCore.Annotations.Test/AnnotationsRequestBodyFilterTests.cs +++ b/test/Swashbuckle.AspNetCore.Annotations.Test/AnnotationsRequestBodyFilterTests.cs @@ -61,7 +61,7 @@ public void Apply_EnrichesParameterMetadata_IfPropertyDecoratedWithSwaggerReques var context = new RequestBodyFilterContext(bodyParameterDescription, null, null, null); Subject().Apply(requestBody, context); - + Assert.Equal("Description for StringWithSwaggerRequestBodyAttribute", requestBody.Description); Assert.True(requestBody.Required); } diff --git a/test/Swashbuckle.AspNetCore.ApiTesting.Test/ApiTestRunnerBaseTests.cs b/test/Swashbuckle.AspNetCore.ApiTesting.Test/ApiTestRunnerBaseTests.cs index e85c83e622..85c3a812d5 100644 --- a/test/Swashbuckle.AspNetCore.ApiTesting.Test/ApiTestRunnerBaseTests.cs +++ b/test/Swashbuckle.AspNetCore.ApiTesting.Test/ApiTestRunnerBaseTests.cs @@ -26,7 +26,7 @@ public async Task TestAsync_ThrowsException_IfOperationNotFound() { c.OpenApiDocs.Add("v1", new OpenApiDocument()); }); - + var exception = await Assert.ThrowsAsync(() => subject.TestAsync( "v1", "GetProducts", @@ -70,7 +70,7 @@ public async Task TestAsync_ThrowsException_IfExpectedStatusCodeIs2xxAndRequestD ], Responses = new OpenApiResponses { - [ "200" ] = new OpenApiResponse() + [ "200" ] = new OpenApiResponse() } } } @@ -118,7 +118,7 @@ public async Task TestAsync_ThrowsException_IfResponseDoesNotMatchSpec( Responses = new OpenApiResponses { ["400"] = new OpenApiResponse(), - ["200"] = new OpenApiResponse() + ["200"] = new OpenApiResponse() } } } diff --git a/test/Swashbuckle.AspNetCore.ApiTesting.Test/JsonValidatorTests.cs b/test/Swashbuckle.AspNetCore.ApiTesting.Test/JsonValidatorTests.cs index 0154284568..2eed054370 100644 --- a/test/Swashbuckle.AspNetCore.ApiTesting.Test/JsonValidatorTests.cs +++ b/test/Swashbuckle.AspNetCore.ApiTesting.Test/JsonValidatorTests.cs @@ -418,7 +418,7 @@ public void Validate_ReturnsError_IfRequiredPropertyNotPresent( var openApiSchema = new OpenApiSchema { Type = JsonSchemaTypes.Object, - Required = new SortedSet(schemaRequired) + Required = new SortedSet(schemaRequired), }; var instance = JToken.Parse(instanceText); @@ -541,7 +541,7 @@ public void Validate_ReturnsError_IfInstanceDoesNotMatchAllSchemasSpecifiedByAll [ new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet { "p1" } }, new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet { "p2" } }, - new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet { "p3" } } + new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet { "p3" } }, ] }; var instance = JToken.Parse(instanceText); @@ -570,7 +570,7 @@ public void Validate_ReturnsError_IfInstanceDoesNotMatchAnySchemaSpecifiedByAnyO [ new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet { "p1" } }, new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet { "p2" } }, - new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet { "p3" } } + new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet { "p3" } }, ] }; var instance = JToken.Parse(instanceText); @@ -600,7 +600,7 @@ public void Validate_ReturnsError_IfInstanceDoesNotMatchExactlyOneSchemaSpecifie [ new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet { "p1" } }, new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet { "p2" } }, - new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet { "p3" } } + new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet { "p3" } }, ] }; var instance = JToken.Parse(instanceText); diff --git a/test/Swashbuckle.AspNetCore.ApiTesting.Test/RequestValidatorTests.cs b/test/Swashbuckle.AspNetCore.ApiTesting.Test/RequestValidatorTests.cs index 38090f27ff..dc0bc1d1ae 100644 --- a/test/Swashbuckle.AspNetCore.ApiTesting.Test/RequestValidatorTests.cs +++ b/test/Swashbuckle.AspNetCore.ApiTesting.Test/RequestValidatorTests.cs @@ -392,6 +392,6 @@ private static OpenApiDocument DocumentWithOperation(string pathTemplate, Operat private static RequestValidator Subject(IEnumerable contentValidators = null) { - return new RequestValidator(contentValidators ?? []); + return new(contentValidators ?? []); } } diff --git a/test/Swashbuckle.AspNetCore.ApiTesting.Test/ResponseValidatorTests.cs b/test/Swashbuckle.AspNetCore.ApiTesting.Test/ResponseValidatorTests.cs index e6cb61db64..bb5eafe01e 100644 --- a/test/Swashbuckle.AspNetCore.ApiTesting.Test/ResponseValidatorTests.cs +++ b/test/Swashbuckle.AspNetCore.ApiTesting.Test/ResponseValidatorTests.cs @@ -215,7 +215,7 @@ public void Validate_DelegatesContentValidationToInjectedContentValidators( Schema = new OpenApiSchema { Type = JsonSchemaTypes.Object, - Required = new SortedSet { "prop1", "prop2" } + Required = new SortedSet { "prop1", "prop2" }, } } } @@ -259,6 +259,6 @@ private static OpenApiDocument DocumentWithOperation(string pathTemplate, Operat private static ResponseValidator Subject(IEnumerable contentValidators = null) { - return new ResponseValidator(contentValidators ?? []); + return new(contentValidators ?? []); } } diff --git a/test/Swashbuckle.AspNetCore.IntegrationTests/CustomDocumentSerializerTests.cs b/test/Swashbuckle.AspNetCore.IntegrationTests/CustomDocumentSerializerTests.cs index 19dc653086..cdf8f9c6b4 100644 --- a/test/Swashbuckle.AspNetCore.IntegrationTests/CustomDocumentSerializerTests.cs +++ b/test/Swashbuckle.AspNetCore.IntegrationTests/CustomDocumentSerializerTests.cs @@ -49,7 +49,7 @@ public async Task DocumentProvider_Writes_Custom_V3_Document() { var testSite = new TestSite(typeof(CustomDocumentSerializer.Startup), outputHelper); var server = testSite.BuildServer(); - var services = server.Host.Services; + var services = server.Services; var documentProvider = services.GetService(); using var stream = new MemoryStream(); @@ -80,7 +80,7 @@ private async Task DocumentProviderWritesCustomV2Document(Action { var testSite = new TestSite(typeof(CustomDocumentSerializer.Startup), outputHelper); var server = testSite.BuildServer(); - var services = server.Host.Services; + var services = server.Services; var documentProvider = services.GetService(); var options = services.GetService>(); diff --git a/test/Swashbuckle.AspNetCore.IntegrationTests/DocumentProviderTests.cs b/test/Swashbuckle.AspNetCore.IntegrationTests/DocumentProviderTests.cs index 8bf64cf30b..60c8f31701 100644 --- a/test/Swashbuckle.AspNetCore.IntegrationTests/DocumentProviderTests.cs +++ b/test/Swashbuckle.AspNetCore.IntegrationTests/DocumentProviderTests.cs @@ -18,7 +18,7 @@ public void DocumentProvider_ExposesAllDocumentNames(Type startupType, string[] { var testSite = new TestSite(startupType, outputHelper); var server = testSite.BuildServer(); - var services = server.Host.Services; + var services = server.Services; var documentProvider = (IDocumentProvider)services.GetService(typeof(IDocumentProvider)); var documentNames = documentProvider.GetDocumentNames(); @@ -37,7 +37,7 @@ public async Task DocumentProvider_ExposesGeneratedSwagger(Type startupType, str { var testSite = new TestSite(startupType, outputHelper); var server = testSite.BuildServer(); - var services = server.Host.Services; + var services = server.Services; var documentProvider = (IDocumentProvider)services.GetService(typeof(IDocumentProvider)); using var stream = new MemoryStream(); @@ -51,6 +51,7 @@ public async Task DocumentProvider_ExposesGeneratedSwagger(Type startupType, str var (_, diagnostic) = await OpenApiDocumentLoader.LoadWithDiagnosticsAsync(stream); Assert.NotNull(diagnostic); Assert.Empty(diagnostic.Errors); + Assert.Empty(diagnostic.Warnings); } [Fact] @@ -58,7 +59,7 @@ public async Task DocumentProvider_ThrowsUnknownDocument_IfUnknownDocumentName() { var testSite = new TestSite(typeof(Basic.Startup), outputHelper); var server = testSite.BuildServer(); - var services = server.Host.Services; + var services = server.Services; var documentProvider = (IDocumentProvider)services.GetService(typeof(IDocumentProvider)); using var writer = new StringWriter(); diff --git a/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerIntegrationTests.cs b/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerIntegrationTests.cs index 98a8c8bca2..55ee5c5f94 100644 --- a/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerIntegrationTests.cs +++ b/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerIntegrationTests.cs @@ -36,7 +36,7 @@ public async Task SwaggerEndpoint_ReturnsValidSwaggerJson( [Fact] public async Task SwaggerEndpoint_ReturnsValidSwaggerJson_ForAutofaq() { - var testSite = new TestSiteAutofaq(typeof(CliExampleWithFactory.Startup)); + var testSite = new TestSiteAutofaq(typeof(CliExampleWithFactory.Startup), outputHelper); using var client = testSite.BuildClient(); await AssertValidSwaggerJson(client, "/swagger/v1/swagger_net8.0.json"); @@ -205,5 +205,6 @@ private static async Task AssertValidSwaggerJson(HttpClient client, string swagg var (_, diagnostic) = await OpenApiDocumentLoader.LoadWithDiagnosticsAsync(contentStream); Assert.NotNull(diagnostic); Assert.Empty(diagnostic.Errors); + Assert.Empty(diagnostic.Warnings); } } diff --git a/test/Swashbuckle.AspNetCore.IntegrationTests/Swashbuckle.AspNetCore.IntegrationTests.csproj b/test/Swashbuckle.AspNetCore.IntegrationTests/Swashbuckle.AspNetCore.IntegrationTests.csproj index 050373b86a..3d3a84a83e 100644 --- a/test/Swashbuckle.AspNetCore.IntegrationTests/Swashbuckle.AspNetCore.IntegrationTests.csproj +++ b/test/Swashbuckle.AspNetCore.IntegrationTests/Swashbuckle.AspNetCore.IntegrationTests.csproj @@ -30,13 +30,13 @@ - - + + - - + + diff --git a/test/Swashbuckle.AspNetCore.IntegrationTests/TestSite.cs b/test/Swashbuckle.AspNetCore.IntegrationTests/TestSite.cs index b6fdbab42c..9af7057754 100644 --- a/test/Swashbuckle.AspNetCore.IntegrationTests/TestSite.cs +++ b/test/Swashbuckle.AspNetCore.IntegrationTests/TestSite.cs @@ -4,29 +4,33 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace Swashbuckle.AspNetCore.IntegrationTests; public class TestSite(Type startupType, ITestOutputHelper outputHelper) { - public TestServer BuildServer() + private IHost _host; + private TestServer _server; + + public virtual TestServer BuildServer() { - var startupAssembly = startupType.Assembly; - var applicationName = startupAssembly.GetName().Name; + if (_server is null) + { + var builder = new HostBuilder(); - var builder = new WebHostBuilder() - .UseEnvironment("Development") - .UseSolutionRelativeContentRoot(Path.Combine("test", "WebSites", applicationName), "*.slnx") - .UseStartup(startupType); + Configure(builder); - builder.ConfigureTestServices((services) => - { - services.AddLogging((logging) => logging.ClearProviders().AddXUnit(outputHelper)); - services.AddTransient(); - }); + builder.ConfigureWebHost(Configure); + + _host = builder.Build(); + _host.Start(); - return new(builder); + _server = _host.GetTestServer(); + } + + return _server; } public HttpClient BuildClient() @@ -35,6 +39,25 @@ public HttpClient BuildClient() return server.CreateClient(); } + protected virtual void Configure(IHostBuilder builder) + { + builder.ConfigureServices((services) => + { + services.AddLogging((logging) => logging.ClearProviders().AddXUnit(outputHelper)); + services.AddTransient(); + }); + } + + protected virtual void Configure(IWebHostBuilder builder) + { + var applicationName = startupType.Assembly.GetName().Name; + + builder.UseEnvironment("Development") + .UseSolutionRelativeContentRoot(Path.Combine("test", "WebSites", applicationName), "*.slnx") + .UseStartup(startupType) + .UseTestServer(); + } + private sealed class LocalizationStartupFilter : IStartupFilter { public Action Configure(Action next) diff --git a/test/Swashbuckle.AspNetCore.IntegrationTests/TestSiteAutofaq.cs b/test/Swashbuckle.AspNetCore.IntegrationTests/TestSiteAutofaq.cs index 2ce4e835e7..43efd9d779 100644 --- a/test/Swashbuckle.AspNetCore.IntegrationTests/TestSiteAutofaq.cs +++ b/test/Swashbuckle.AspNetCore.IntegrationTests/TestSiteAutofaq.cs @@ -1,38 +1,21 @@ -using System.Reflection; -using Autofac.Extensions.DependencyInjection; +using Autofac.Extensions.DependencyInjection; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.Hosting; namespace Swashbuckle.AspNetCore.IntegrationTests; -public class TestSiteAutofaq +public class TestSiteAutofaq(Type startupType, ITestOutputHelper outputHelper) + : TestSite(startupType, outputHelper) { - private readonly Type _startupType; - - public TestSiteAutofaq(Type startupType) + protected override void Configure(IHostBuilder builder) { - _startupType = startupType; + base.Configure(builder); + builder.UseServiceProviderFactory(new AutofacServiceProviderFactory()); } - public TestServer BuildServer() + protected override void Configure(IWebHostBuilder builder) { - var startupAssembly = _startupType.Assembly; - var applicationName = startupAssembly.GetName().Name; - - var hostBuilder = new WebHostBuilder() - .UseEnvironment("Development") - .ConfigureServices(services => services.AddAutofac()) - .UseSolutionRelativeContentRoot(Path.Combine("test", "WebSites", applicationName), "*.slnx") - .UseStartup(_startupType); - - return new TestServer(hostBuilder); - } - - public HttpClient BuildClient() - { - var server = BuildServer(); - var client = server.CreateClient(); - - return client; + builder.ConfigureServices((services) => services.AddAutofac()); + base.Configure(builder); } } diff --git a/test/Swashbuckle.AspNetCore.IntegrationTests/VerifyTests.cs b/test/Swashbuckle.AspNetCore.IntegrationTests/VerifyTests.cs index 847824a73e..ffe2aec60d 100644 --- a/test/Swashbuckle.AspNetCore.IntegrationTests/VerifyTests.cs +++ b/test/Swashbuckle.AspNetCore.IntegrationTests/VerifyTests.cs @@ -42,7 +42,7 @@ public async Task SwaggerEndpoint_ReturnsValidSwaggerJson_ForAutofaq() var startupType = typeof(CliExampleWithFactory.Startup); const string swaggerRequestUri = "/swagger/v1/swagger_net8.0.json"; - var testSite = new TestSiteAutofaq(startupType); + var testSite = new TestSiteAutofaq(startupType, outputHelper); using var client = testSite.BuildClient(); using var swaggerResponse = await client.GetAsync(swaggerRequestUri, TestContext.Current.CancellationToken); diff --git a/test/WebSites/MvcWithNullable/MvcWithNullable.csproj b/test/WebSites/MvcWithNullable/MvcWithNullable.csproj index 1a95be247f..f4465a728b 100644 --- a/test/WebSites/MvcWithNullable/MvcWithNullable.csproj +++ b/test/WebSites/MvcWithNullable/MvcWithNullable.csproj @@ -18,7 +18,7 @@ - + diff --git a/test/WebSites/OAuth2Integration/OAuth2Integration.csproj b/test/WebSites/OAuth2Integration/OAuth2Integration.csproj index 56cc1d3b57..5bbc961286 100644 --- a/test/WebSites/OAuth2Integration/OAuth2Integration.csproj +++ b/test/WebSites/OAuth2Integration/OAuth2Integration.csproj @@ -11,12 +11,12 @@ - + - + diff --git a/test/WebSites/ReDoc/Startup.cs b/test/WebSites/ReDoc/Startup.cs index a41f56fe1d..e2a96e9732 100644 --- a/test/WebSites/ReDoc/Startup.cs +++ b/test/WebSites/ReDoc/Startup.cs @@ -28,6 +28,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseEndpoints(endpoints => { endpoints.MapControllers(); + endpoints.MapGet("/", () => Results.Redirect("api-docs")); }); app.UseSwagger(c => diff --git a/test/WebSites/WebApi/EndPoints/OpenApiEndpoints.cs b/test/WebSites/WebApi/EndPoints/OpenApiEndpoints.cs index cd48ac6313..04d3a629d5 100644 --- a/test/WebSites/WebApi/EndPoints/OpenApiEndpoints.cs +++ b/test/WebSites/WebApi/EndPoints/OpenApiEndpoints.cs @@ -98,6 +98,7 @@ public static IEndpointRouteBuilder MapWithOpenApiEndpoints(this IEndpointRouteB return app; } } + record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) { public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); diff --git a/test/WebSites/WebApi/WebApi.csproj b/test/WebSites/WebApi/WebApi.csproj index 503306e6ff..c509238d96 100644 --- a/test/WebSites/WebApi/WebApi.csproj +++ b/test/WebSites/WebApi/WebApi.csproj @@ -19,7 +19,7 @@ - +