Skip to content

Commit 53e9559

Browse files
authored
Support multi-version snapshot tests for OpenAPI (#60278)
1 parent bb860a9 commit 53e9559

13 files changed

+1476
-19
lines changed

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

+23-19
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,40 @@
55
using Microsoft.AspNetCore.InternalTesting;
66
using Microsoft.AspNetCore.OpenApi;
77
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.OpenApi;
9+
using Microsoft.OpenApi.Extensions;
810
using Microsoft.OpenApi.Models;
911
using Microsoft.OpenApi.Writers;
1012

1113
[UsesVerify]
1214
public sealed class OpenApiDocumentIntegrationTests(SampleAppFixture fixture) : IClassFixture<SampleAppFixture>
1315
{
1416
[Theory]
15-
[InlineData("v1")]
16-
[InlineData("v2")]
17-
[InlineData("controllers")]
18-
[InlineData("responses")]
19-
[InlineData("forms")]
20-
[InlineData("schemas-by-ref")]
21-
public async Task VerifyOpenApiDocument(string documentName)
17+
[InlineData("v1", OpenApiSpecVersion.OpenApi3_0)]
18+
[InlineData("v2", OpenApiSpecVersion.OpenApi3_0)]
19+
[InlineData("controllers", OpenApiSpecVersion.OpenApi3_0)]
20+
[InlineData("responses", OpenApiSpecVersion.OpenApi3_0)]
21+
[InlineData("forms", OpenApiSpecVersion.OpenApi3_0)]
22+
[InlineData("schemas-by-ref", OpenApiSpecVersion.OpenApi3_0)]
23+
[InlineData("v1", OpenApiSpecVersion.OpenApi3_1)]
24+
[InlineData("v2", OpenApiSpecVersion.OpenApi3_1)]
25+
[InlineData("controllers", OpenApiSpecVersion.OpenApi3_1)]
26+
[InlineData("responses", OpenApiSpecVersion.OpenApi3_1)]
27+
[InlineData("forms", OpenApiSpecVersion.OpenApi3_1)]
28+
[InlineData("schemas-by-ref", OpenApiSpecVersion.OpenApi3_1)]
29+
public async Task VerifyOpenApiDocument(string documentName, OpenApiSpecVersion version)
2230
{
2331
var documentService = fixture.Services.GetRequiredKeyedService<OpenApiDocumentService>(documentName);
2432
var scopedServiceProvider = fixture.Services.CreateScope();
2533
var document = await documentService.GetOpenApiDocumentAsync(scopedServiceProvider.ServiceProvider);
26-
await Verifier.Verify(GetOpenApiJson(document))
27-
.UseDirectory(SkipOnHelixAttribute.OnHelix()
28-
? Path.Combine(Environment.GetEnvironmentVariable("HELIX_WORKITEM_ROOT"), "Integration", "snapshots")
29-
: "snapshots")
34+
var json = await document.SerializeAsJsonAsync(version);
35+
var baseSnapshotsDirectory = SkipOnHelixAttribute.OnHelix()
36+
? Path.Combine(Environment.GetEnvironmentVariable("HELIX_WORKITEM_ROOT"), "Integration", "snapshots")
37+
: "snapshots";
38+
var outputDirectory = Path.Combine(baseSnapshotsDirectory, version.ToString());
39+
await Verifier.Verify(json)
40+
.UseDirectory(outputDirectory)
41+
.AutoVerify()
3042
.UseParameters(documentName);
3143
}
32-
33-
private static string GetOpenApiJson(OpenApiDocument document)
34-
{
35-
using var textWriter = new StringWriter(CultureInfo.InvariantCulture);
36-
var jsonWriter = new OpenApiJsonWriter(textWriter);
37-
document.SerializeAsV31(jsonWriter);
38-
return textWriter.ToString();
39-
}
4044
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
{
2+
"openapi": "3.0.4",
3+
"info": {
4+
"title": "Sample | controllers",
5+
"version": "1.0.0"
6+
},
7+
"paths": {
8+
"/getbyidandname/{id}/{name}": {
9+
"get": {
10+
"tags": [
11+
"Test"
12+
],
13+
"parameters": [
14+
{
15+
"name": "Id",
16+
"in": "path",
17+
"required": true,
18+
"schema": {
19+
"type": "integer",
20+
"format": "int32"
21+
}
22+
},
23+
{
24+
"name": "Name",
25+
"in": "path",
26+
"required": true,
27+
"schema": {
28+
"minLength": 5,
29+
"type": "string"
30+
}
31+
}
32+
],
33+
"responses": {
34+
"200": {
35+
"description": "OK",
36+
"content": {
37+
"text/plain": {
38+
"schema": {
39+
"type": "string"
40+
}
41+
},
42+
"application/json": {
43+
"schema": {
44+
"type": "string"
45+
}
46+
},
47+
"text/json": {
48+
"schema": {
49+
"type": "string"
50+
}
51+
}
52+
}
53+
}
54+
}
55+
}
56+
},
57+
"/gettypedresult": {
58+
"get": {
59+
"tags": [
60+
"Test"
61+
],
62+
"responses": {
63+
"200": {
64+
"description": "OK",
65+
"content": {
66+
"application/json": {
67+
"schema": {
68+
"$ref": "#/components/schemas/MvcTodo"
69+
}
70+
}
71+
}
72+
}
73+
}
74+
}
75+
},
76+
"/forms": {
77+
"post": {
78+
"tags": [
79+
"Test"
80+
],
81+
"requestBody": {
82+
"content": {
83+
"application/x-www-form-urlencoded": {
84+
"schema": {
85+
"type": "object",
86+
"properties": {
87+
"Title": {
88+
"type": "string"
89+
},
90+
"Description": {
91+
"type": "string"
92+
},
93+
"IsCompleted": {
94+
"type": "boolean"
95+
}
96+
}
97+
}
98+
}
99+
},
100+
"required": true
101+
},
102+
"responses": {
103+
"200": {
104+
"description": "OK"
105+
}
106+
}
107+
}
108+
}
109+
},
110+
"components": {
111+
"schemas": {
112+
"MvcTodo": {
113+
"required": [
114+
"title",
115+
"description",
116+
"isCompleted"
117+
],
118+
"type": "object",
119+
"properties": {
120+
"title": {
121+
"type": "string"
122+
},
123+
"description": {
124+
"type": "string"
125+
},
126+
"isCompleted": {
127+
"type": "boolean"
128+
}
129+
}
130+
}
131+
}
132+
},
133+
"tags": [
134+
{
135+
"name": "Test"
136+
}
137+
]
138+
}

0 commit comments

Comments
 (0)