Skip to content
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
8 changes: 4 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
<PackageVersion Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
<PackageVersion Include="JunitXml.TestLogger" Version="6.1.0" />
<PackageVersion Include="MartinCostello.Logging.XUnit.v3" Version="0.6.0" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.19" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.19" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.20" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.20" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.1.0" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.19" />
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="8.0.19" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.20" />
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="8.0.20" />
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="4.14.0" />
<PackageVersion Include="Microsoft.OpenApi" Version="1.6.25" />
<PackageVersion Include="Microsoft.OpenApi.Readers" Version="1.6.25" />
Expand Down
2 changes: 2 additions & 0 deletions src/Swashbuckle.AspNetCore.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,7 +13,7 @@ public void Apply_CreatesMetadataForControllerNameTag_FromSwaggerTagAttribute()
var document = new OpenApiDocument();
var apiDescription = ApiDescriptionFactory.Create<FakeControllerWithSwaggerAnnotations>(c => nameof(c.ActionWithNoAttributes));
var filterContext = new DocumentFilterContext(
apiDescriptions: new[] { apiDescription },
apiDescriptions: [apiDescription],
schemaGenerator: null,
schemaRepository: null);

Expand All @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,5 @@ public void Apply_DoesNotModifyTheRequiredFlag_IfNotSpecifiedWithSwaggerParamete
Assert.True(parameter.Required);
}

private AnnotationsParameterFilter Subject()
{
return new AnnotationsParameterFilter();
}
private static AnnotationsParameterFilter Subject() => new();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task TestAsync_ThrowsException_IfOperationNotFound()
{
c.OpenApiDocs.Add("v1", new OpenApiDocument());
});

var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => subject.TestAsync(
"v1",
"GetProducts",
Expand Down Expand Up @@ -70,7 +70,7 @@ public async Task TestAsync_ThrowsException_IfExpectedStatusCodeIs2xxAndRequestD
],
Responses = new OpenApiResponses
{
[ "200" ] = new OpenApiResponse()
[ "200" ] = new OpenApiResponse()
}
}
}
Expand Down Expand Up @@ -118,7 +118,7 @@ public async Task TestAsync_ThrowsException_IfResponseDoesNotMatchSpec(
Responses = new OpenApiResponses
{
["400"] = new OpenApiResponse(),
["200"] = new OpenApiResponse()
["200"] = new OpenApiResponse()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public void Validate_ReturnsError_IfRequiredPropertyNotPresent(
var openApiSchema = new OpenApiSchema
{
Type = JsonSchemaTypes.Object,
Required = new SortedSet<string>(schemaRequired)
Required = new SortedSet<string>(schemaRequired),
};
var instance = JToken.Parse(instanceText);

Expand Down Expand Up @@ -541,7 +541,7 @@ public void Validate_ReturnsError_IfInstanceDoesNotMatchAllSchemasSpecifiedByAll
[
new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet<string> { "p1" } },
new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet<string> { "p2" } },
new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet<string> { "p3" } }
new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet<string> { "p3" } },
]
};
var instance = JToken.Parse(instanceText);
Expand Down Expand Up @@ -570,7 +570,7 @@ public void Validate_ReturnsError_IfInstanceDoesNotMatchAnySchemaSpecifiedByAnyO
[
new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet<string> { "p1" } },
new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet<string> { "p2" } },
new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet<string> { "p3" } }
new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet<string> { "p3" } },
]
};
var instance = JToken.Parse(instanceText);
Expand Down Expand Up @@ -600,7 +600,7 @@ public void Validate_ReturnsError_IfInstanceDoesNotMatchExactlyOneSchemaSpecifie
[
new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet<string> { "p1" } },
new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet<string> { "p2" } },
new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet<string> { "p3" } }
new OpenApiSchema { Type = JsonSchemaTypes.Object, Required = new SortedSet<string> { "p3" } },
]
};
var instance = JToken.Parse(instanceText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,6 @@ private static OpenApiDocument DocumentWithOperation(string pathTemplate, Operat

private static RequestValidator Subject(IEnumerable<IContentValidator> contentValidators = null)
{
return new RequestValidator(contentValidators ?? []);
return new(contentValidators ?? []);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public void Validate_DelegatesContentValidationToInjectedContentValidators(
Schema = new OpenApiSchema
{
Type = JsonSchemaTypes.Object,
Required = new SortedSet<string> { "prop1", "prop2" }
Required = new SortedSet<string> { "prop1", "prop2" },
}
}
}
Expand Down Expand Up @@ -259,6 +259,6 @@ private static OpenApiDocument DocumentWithOperation(string pathTemplate, Operat

private static ResponseValidator Subject(IEnumerable<IContentValidator> contentValidators = null)
{
return new ResponseValidator(contentValidators ?? []);
return new(contentValidators ?? []);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<IDocumentProvider>();
using var stream = new MemoryStream();
Expand Down Expand Up @@ -80,7 +80,7 @@ private async Task DocumentProviderWritesCustomV2Document(Action<SwaggerOptions>
{
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<IDocumentProvider>();
var options = services.GetService<IOptions<SwaggerOptions>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -51,14 +51,15 @@ 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]
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
<PackageReference Update="Microsoft.AspNetCore.Mvc.Testing" VersionOverride="8.0.19" />
<PackageReference Update="Microsoft.AspNetCore.TestHost" VersionOverride="8.0.19" />
<PackageReference Update="Microsoft.AspNetCore.Mvc.Testing" VersionOverride="8.0.20" />
<PackageReference Update="Microsoft.AspNetCore.TestHost" VersionOverride="8.0.20" />
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))">
<PackageReference Update="Microsoft.AspNetCore.Mvc.Testing" VersionOverride="9.0.8" />
<PackageReference Update="Microsoft.AspNetCore.TestHost" VersionOverride="9.0.8" />
<PackageReference Update="Microsoft.AspNetCore.Mvc.Testing" VersionOverride="9.0.9" />
<PackageReference Update="Microsoft.AspNetCore.TestHost" VersionOverride="9.0.9" />
</ItemGroup>

</Project>
49 changes: 36 additions & 13 deletions test/Swashbuckle.AspNetCore.IntegrationTests/TestSite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IStartupFilter, LocalizationStartupFilter>();
});
builder.ConfigureWebHost(Configure);

_host = builder.Build();
_host.Start();

return new(builder);
_server = _host.GetTestServer();
}

return _server;
}

public HttpClient BuildClient()
Expand All @@ -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<IStartupFilter, LocalizationStartupFilter>();
});
}

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<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
Expand Down
37 changes: 10 additions & 27 deletions test/Swashbuckle.AspNetCore.IntegrationTests/TestSiteAutofaq.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/WebSites/MvcWithNullable/MvcWithNullable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))">
<PackageReference Update="Microsoft.AspNetCore.OpenApi" VersionOverride="9.0.8" />
<PackageReference Update="Microsoft.AspNetCore.OpenApi" VersionOverride="9.0.9" />
</ItemGroup>

</Project>
Loading
Loading