diff --git a/src/AspNetCore/WebApi/test/Asp.Versioning.OpenApi.Tests/Content/v1-minimal.json b/src/AspNetCore/WebApi/test/Asp.Versioning.OpenApi.Tests/Content/v1-minimal.json new file mode 100644 index 00000000..5e9491ce --- /dev/null +++ b/src/AspNetCore/WebApi/test/Asp.Versioning.OpenApi.Tests/Content/v1-minimal.json @@ -0,0 +1,86 @@ +{ + "openapi": "3.1.1", + "info": { + "title": "Test API | v1", + "description": "The API was deprecated on 01/01/2026. The API was sunset on 02/10/2026.\n\n### Links\n\n- [Version Deprecation Policy](http://my.api.com/policies/versions/deprecated.html)\n- [Version Sunset Policy](http://my.api.com/policies/versions/sunset.html)", + "version": "1.0" + }, + "servers": [ + { + "url": "http://localhost/" + } + ], + "paths": { + "/test/{id}": { + "get": { + "tags": [ + "Test" + ], + "summary": "", + "description": "", + "parameters": [ + { + "name": "id", + "in": "path", + "description": "", + "required": true, + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": "integer", + "format": "int32" + } + }, + { + "name": "api-version", + "in": "query", + "description": "The requested API version", + "required": true, + "schema": { + "type": "string", + "default": "1.0" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "format": "int32" + } + } + } + }, + "400": { + "description": "Bad Request" + } + } + } + } + }, + "tags": [ + { + "name": "Test" + } + ], + "x-api-versioning": [ + { + "title": "Version Deprecation Policy", + "type": "text/html", + "rel": "deprecation", + "url": "http://my.api.com/policies/versions/deprecated.html" + }, + { + "title": "Version Sunset Policy", + "type": "text/html", + "rel": "sunset", + "url": "http://my.api.com/policies/versions/sunset.html" + } + ] +} \ No newline at end of file diff --git a/src/AspNetCore/WebApi/test/Asp.Versioning.OpenApi.Tests/Content/v1-mixed.json b/src/AspNetCore/WebApi/test/Asp.Versioning.OpenApi.Tests/Content/v1-mixed.json new file mode 100644 index 00000000..f6ec6c15 --- /dev/null +++ b/src/AspNetCore/WebApi/test/Asp.Versioning.OpenApi.Tests/Content/v1-mixed.json @@ -0,0 +1,157 @@ +{ + "openapi": "3.1.1", + "info": { + "title": "Test API | v1", + "description": "The API was deprecated on 01/01/2026. The API was sunset on 02/10/2026.\n\n### Links\n\n- [Version Deprecation Policy](http://my.api.com/policies/versions/deprecated.html)\n- [Version Sunset Policy](http://my.api.com/policies/versions/sunset.html)", + "version": "1.0" + }, + "servers": [ + { + "url": "http://localhost/" + } + ], + "paths": { + "/minimal/{id}": { + "get": { + "tags": [ + "Test" + ], + "summary": "", + "description": "", + "parameters": [ + { + "name": "id", + "in": "path", + "description": "", + "required": true, + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": "integer", + "format": "int32" + } + }, + { + "name": "api-version", + "in": "query", + "description": "The requested API version", + "required": true, + "schema": { + "type": "string", + "default": "1.0" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "format": "int32" + } + } + } + }, + "400": { + "description": "Bad Request" + } + } + } + }, + "/Test": { + "get": { + "tags": [ + "Test" + ], + "summary": "", + "description": "", + "parameters": [ + { + "name": "id", + "in": "query", + "description": "", + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "format": "int32" + } + }, + { + "name": "api-version", + "in": "query", + "description": "The requested API version", + "required": true, + "schema": { + "type": "string", + "default": "1.0" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "text/plain": { + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "format": "int32" + } + }, + "application/json": { + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "format": "int32" + } + }, + "text/json": { + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "format": "int32" + } + } + } + } + } + } + } + }, + "tags": [ + { + "name": "Test" + } + ], + "x-api-versioning": [ + { + "title": "Version Deprecation Policy", + "type": "text/html", + "rel": "deprecation", + "url": "http://my.api.com/policies/versions/deprecated.html" + }, + { + "title": "Version Sunset Policy", + "type": "text/html", + "rel": "sunset", + "url": "http://my.api.com/policies/versions/sunset.html" + } + ] +} \ No newline at end of file diff --git a/src/AspNetCore/WebApi/test/Asp.Versioning.OpenApi.Tests/Transformers/AcceptanceTest.cs b/src/AspNetCore/WebApi/test/Asp.Versioning.OpenApi.Tests/Transformers/AcceptanceTest.cs index 9d780bbf..54d9822e 100644 --- a/src/AspNetCore/WebApi/test/Asp.Versioning.OpenApi.Tests/Transformers/AcceptanceTest.cs +++ b/src/AspNetCore/WebApi/test/Asp.Versioning.OpenApi.Tests/Transformers/AcceptanceTest.cs @@ -26,7 +26,6 @@ public async Task minimal_api_should_generate_expected_open_api_document() var builder = WebApplication.CreateBuilder(); builder.WebHost.UseTestServer(); - builder.Services.AddProblemDetails(); builder.Services.AddApiVersioning( options => AddPolicies( options ) ) .AddApiExplorer( options => options.GroupNameFormat = "'v'VVV" ) .AddOpenApi(); @@ -43,6 +42,9 @@ public async Task minimal_api_should_generate_expected_open_api_document() app.MapOpenApi().WithDocumentPerVersion(); var cancellationToken = TestContext.Current.CancellationToken; + using var stream = File.OpenRead( Path.Combine( AppContext.BaseDirectory, "Content", "v1-minimal.json" ) ); + var expected = await JsonNode.ParseAsync( stream, default, default, cancellationToken ); + await app.StartAsync( cancellationToken ); using var client = app.GetTestClient(); @@ -51,18 +53,7 @@ public async Task minimal_api_should_generate_expected_open_api_document() var actual = await client.GetFromJsonAsync( "/openapi/v1.json", cancellationToken ); // assert - actual!["info"]!["version"]!.GetValue().Should().Be( "1.0" ); - - var paths = actual["paths"]!.AsObject(); - - paths.Select( p => p.Key ).Should().Contain( "/test/{id}" ); - paths.Select( p => p.Key ).Should().NotContain( "/Test" ); - - var operation = paths["/test/{id}"]!["get"]!; - var parameters = operation["parameters"]!.AsArray(); - - parameters.Should().Contain( p => p!["name"]!.GetValue() == "id" ); - parameters.Should().Contain( p => p!["name"]!.GetValue() == "api-version" ); + JsonNode.DeepEquals( actual, expected ).Should().BeTrue(); } [Fact] @@ -124,6 +115,9 @@ public async Task mixed_api_should_generate_expected_open_api_document() app.MapOpenApi().WithDocumentPerVersion(); var cancellationToken = TestContext.Current.CancellationToken; + using var stream = File.OpenRead( Path.Combine( AppContext.BaseDirectory, "Content", "v1-mixed.json" ) ); + var expected = await JsonNode.ParseAsync( stream, default, default, cancellationToken ); + await app.StartAsync( cancellationToken ); using var client = app.GetTestClient(); @@ -132,12 +126,7 @@ public async Task mixed_api_should_generate_expected_open_api_document() var actual = await client.GetFromJsonAsync( "/openapi/v1.json", cancellationToken ); // assert - actual!["info"]!["version"]!.GetValue().Should().Be( "1.0" ); - - var paths = actual["paths"]!.AsObject(); - - paths.Select( p => p.Key ).Should().Contain( "/minimal/{id}" ); - paths.Select( p => p.Key ).Should().Contain( "/Test" ); + JsonNode.DeepEquals( actual, expected ).Should().BeTrue(); } private static ApiVersioningOptions AddPolicies( ApiVersioningOptions options )