Skip to content

Commit 38c10bc

Browse files
Fix snapshot tests
Make OpenAPI sample endpoints version-specific.
1 parent 9d9671d commit 38c10bc

16 files changed

+77
-30
lines changed

src/OpenApi/sample/Program.cs

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Globalization;
55
using System.Text.Json.Serialization;
6+
using Microsoft.OpenApi;
67
using Microsoft.OpenApi.Models;
78
using Sample.Transformers;
89

@@ -25,20 +26,65 @@
2526
options.AddHeader("X-Version", "1.0");
2627
options.AddDocumentTransformer<BearerSecuritySchemeTransformer>();
2728
});
28-
builder.Services.AddOpenApi("v2", options => {
29+
builder.Services.AddOpenApi("v2", options =>
30+
{
2931
options.AddSchemaTransformer<AddExternalDocsTransformer>();
3032
options.AddOperationTransformer<AddExternalDocsTransformer>();
3133
options.AddDocumentTransformer(new AddContactTransformer());
32-
options.AddDocumentTransformer((document, context, token) => {
34+
options.AddDocumentTransformer((document, context, token) =>
35+
{
3336
document.Info.License = new OpenApiLicense { Name = "MIT" };
3437
return Task.CompletedTask;
3538
});
3639
});
37-
builder.Services.AddOpenApi("controllers");
38-
builder.Services.AddOpenApi("responses");
39-
builder.Services.AddOpenApi("forms");
40-
builder.Services.AddOpenApi("schemas-by-ref");
41-
builder.Services.AddOpenApi("xml");
40+
41+
var versions = new[]
42+
{
43+
OpenApiSpecVersion.OpenApi3_0,
44+
OpenApiSpecVersion.OpenApi3_1,
45+
};
46+
47+
var documentNames = new[]
48+
{
49+
"controllers",
50+
"responses",
51+
"forms",
52+
"schemas-by-ref",
53+
"xml",
54+
};
55+
56+
foreach (var version in versions)
57+
{
58+
builder.Services.AddOpenApi($"v1-{version}", options =>
59+
{
60+
options.OpenApiVersion = version;
61+
options.ShouldInclude = (description) => description.GroupName == null || description.GroupName == "v1";
62+
options.AddHeader("X-Version", "1.0");
63+
options.AddDocumentTransformer<BearerSecuritySchemeTransformer>();
64+
});
65+
builder.Services.AddOpenApi($"v2-{version}", options =>
66+
{
67+
options.OpenApiVersion = version;
68+
options.ShouldInclude = (description) => description.GroupName == null || description.GroupName == "v2";
69+
options.AddSchemaTransformer<AddExternalDocsTransformer>();
70+
options.AddOperationTransformer<AddExternalDocsTransformer>();
71+
options.AddDocumentTransformer(new AddContactTransformer());
72+
options.AddDocumentTransformer((document, context, token) =>
73+
{
74+
document.Info.License = new OpenApiLicense { Name = "MIT" };
75+
return Task.CompletedTask;
76+
});
77+
});
78+
79+
foreach (var name in documentNames)
80+
{
81+
builder.Services.AddOpenApi($"{name}-{version}", options =>
82+
{
83+
options.OpenApiVersion = version;
84+
options.ShouldInclude = (description) => description.GroupName == null || description.GroupName == name;
85+
});
86+
}
87+
}
4288

4389
var app = builder.Build();
4490

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/OpenApiDocumentIntegrationTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ public static TheoryData<string, OpenApiSpecVersion> OpenApiDocuments()
3535
[MemberData(nameof(OpenApiDocuments))]
3636
public async Task VerifyOpenApiDocument(string documentName, OpenApiSpecVersion version)
3737
{
38+
var versionString = version.ToString();
3839
using var client = fixture.CreateClient();
39-
var json = await client.GetStringAsync($"/openapi/{documentName}.json");
40+
var json = await client.GetStringAsync($"/openapi/{documentName}-{versionString}.json");
4041
var baseSnapshotsDirectory = SkipOnHelixAttribute.OnHelix()
4142
? Path.Combine(Environment.GetEnvironmentVariable("HELIX_WORKITEM_ROOT"), "Integration", "snapshots")
4243
: "snapshots";
43-
var outputDirectory = Path.Combine(baseSnapshotsDirectory, version.ToString());
44+
var outputDirectory = Path.Combine(baseSnapshotsDirectory, versionString);
4445
await Verify(json)
4546
.UseDirectory(outputDirectory)
4647
.UseParameters(documentName);

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApi3_0/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=controllers.verified.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"openapi": "3.1.1",
2+
"openapi": "3.0.4",
33
"info": {
4-
"title": "Sample | controllers",
4+
"title": "Sample | controllers-openapi3_0",
55
"version": "1.0.0"
66
},
77
"servers": [

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApi3_0/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=forms.verified.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"openapi": "3.1.1",
2+
"openapi": "3.0.4",
33
"info": {
4-
"title": "Sample | forms",
4+
"title": "Sample | forms-openapi3_0",
55
"version": "1.0.0"
66
},
77
"servers": [

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApi3_0/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=responses.verified.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"openapi": "3.1.1",
2+
"openapi": "3.0.4",
33
"info": {
4-
"title": "Sample | responses",
4+
"title": "Sample | responses-openapi3_0",
55
"version": "1.0.0"
66
},
77
"servers": [

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApi3_0/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=schemas-by-ref.verified.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"openapi": "3.1.1",
2+
"openapi": "3.0.4",
33
"info": {
4-
"title": "Sample | schemas-by-ref",
4+
"title": "Sample | schemas-by-ref-openapi3_0",
55
"version": "1.0.0"
66
},
77
"servers": [

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApi3_0/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=v1.verified.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"openapi": "3.1.1",
2+
"openapi": "3.0.4",
33
"info": {
4-
"title": "Sample | v1",
4+
"title": "Sample | v1-openapi3_0",
55
"version": "1.0.0"
66
},
77
"servers": [

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApi3_0/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=v2.verified.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"openapi": "3.1.1",
2+
"openapi": "3.0.4",
33
"info": {
4-
"title": "Sample | v2",
4+
"title": "Sample | v2-openapi3_0",
55
"contact": {
66
"name": "OpenAPI Enthusiast",
77
"email": "[email protected]"

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApi3_0/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=xml.verified.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"openapi": "3.1.1",
2+
"openapi": "3.0.4",
33
"info": {
4-
"title": "Sample | xml",
4+
"title": "Sample | xml-openapi3_0",
55
"version": "1.0.0"
66
},
77
"servers": [

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApi3_1/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=controllers.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"openapi": "3.1.1",
33
"info": {
4-
"title": "Sample | controllers",
4+
"title": "Sample | controllers-openapi3_1",
55
"version": "1.0.0"
66
},
77
"servers": [

0 commit comments

Comments
 (0)