Skip to content

Commit 0560067

Browse files
committed
Fix integration tests for OpenAPI document
1 parent 320c49f commit 0560067

File tree

17 files changed

+488
-1563
lines changed

17 files changed

+488
-1563
lines changed
Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Globalization;
54
using Microsoft.AspNetCore.InternalTesting;
65
using Microsoft.AspNetCore.OpenApi;
76
using Microsoft.Extensions.DependencyInjection;
8-
using Microsoft.OpenApi.Models;
9-
using Microsoft.OpenApi.Writers;
7+
using Microsoft.OpenApi;
8+
using Microsoft.OpenApi.Extensions;
109

1110
[UsesVerify]
1211
public sealed class OpenApiDocumentIntegrationTests(SampleAppFixture fixture) : IClassFixture<SampleAppFixture>
1312
{
1413
[Theory]
15-
[InlineData("v1")]
16-
[InlineData("v2")]
17-
[InlineData("controllers")]
18-
[InlineData("responses")]
19-
[InlineData("forms")]
20-
[InlineData("schemas-by-ref")]
21-
[InlineData("xml")]
22-
public async Task VerifyOpenApiDocument(string documentName)
14+
[InlineData("v1", OpenApiSpecVersion.OpenApi3_0)]
15+
[InlineData("v2", OpenApiSpecVersion.OpenApi3_0)]
16+
[InlineData("controllers", OpenApiSpecVersion.OpenApi3_0)]
17+
[InlineData("responses", OpenApiSpecVersion.OpenApi3_0)]
18+
[InlineData("forms", OpenApiSpecVersion.OpenApi3_0)]
19+
[InlineData("schemas-by-ref", OpenApiSpecVersion.OpenApi3_0)]
20+
[InlineData("xml", OpenApiSpecVersion.OpenApi3_0)]
21+
[InlineData("v1", OpenApiSpecVersion.OpenApi3_1)]
22+
[InlineData("v2", OpenApiSpecVersion.OpenApi3_1)]
23+
[InlineData("controllers", OpenApiSpecVersion.OpenApi3_1)]
24+
[InlineData("responses", OpenApiSpecVersion.OpenApi3_1)]
25+
[InlineData("forms", OpenApiSpecVersion.OpenApi3_1)]
26+
[InlineData("schemas-by-ref", OpenApiSpecVersion.OpenApi3_1)]
27+
[InlineData("xml", OpenApiSpecVersion.OpenApi3_1)]
28+
public async Task VerifyOpenApiDocument(string documentName, OpenApiSpecVersion version)
2329
{
2430
var documentService = fixture.Services.GetRequiredKeyedService<OpenApiDocumentService>(documentName);
2531
var scopedServiceProvider = fixture.Services.CreateScope();
2632
var document = await documentService.GetOpenApiDocumentAsync(scopedServiceProvider.ServiceProvider);
27-
await Verifier.Verify(GetOpenApiJson(document))
28-
.UseDirectory(SkipOnHelixAttribute.OnHelix()
29-
? Path.Combine(Environment.GetEnvironmentVariable("HELIX_WORKITEM_ROOT"), "Integration", "snapshots")
30-
: "snapshots")
33+
var json = await document.SerializeAsJsonAsync(version);
34+
var baseSnapshotsDirectory = SkipOnHelixAttribute.OnHelix()
35+
? Path.Combine(Environment.GetEnvironmentVariable("HELIX_WORKITEM_ROOT"), "Integration", "snapshots")
36+
: "snapshots";
37+
var outputDirectory = Path.Combine(baseSnapshotsDirectory, version.ToString());
38+
await Verifier.Verify(json)
39+
.UseDirectory(outputDirectory)
40+
.AutoVerify()
3141
.UseParameters(documentName);
3242
}
33-
34-
private static string GetOpenApiJson(OpenApiDocument document)
35-
{
36-
using var textWriter = new StringWriter(CultureInfo.InvariantCulture);
37-
var jsonWriter = new OpenApiJsonWriter(textWriter);
38-
document.SerializeAsV31(jsonWriter);
39-
return textWriter.ToString();
40-
}
4143
}

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

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,6 @@
5454
}
5555
}
5656
},
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-
},
7657
"/forms": {
7758
"post": {
7859
"tags": [
@@ -107,29 +88,6 @@
10788
}
10889
}
10990
},
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-
},
13391
"tags": [
13492
{
13593
"name": "Test"

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,24 @@
198198
"properties": {
199199
"id": {
200200
"type": "integer",
201+
"description": "The unique identifier of the to-do item.",
201202
"format": "int32"
202203
},
203204
"title": {
204-
"type": "string"
205+
"type": "string",
206+
"description": "The title of the to-do item."
205207
},
206208
"completed": {
207-
"type": "boolean"
209+
"type": "boolean",
210+
"description": "Indicates whether the to-do item is completed."
208211
},
209212
"createdAt": {
210213
"type": "string",
214+
"description": "The date and time when the to-do item was created.",
211215
"format": "date-time"
212216
}
213-
}
217+
},
218+
"description": "Represents a to-do item."
214219
}
215220
}
216221
},

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,24 @@
163163
"properties": {
164164
"id": {
165165
"type": "integer",
166+
"description": "The unique identifier of the to-do item.",
166167
"format": "int32"
167168
},
168169
"title": {
169-
"type": "string"
170+
"type": "string",
171+
"description": "The title of the to-do item."
170172
},
171173
"completed": {
172-
"type": "boolean"
174+
"type": "boolean",
175+
"description": "Indicates whether the to-do item is completed."
173176
},
174177
"createdAt": {
175178
"type": "string",
179+
"description": "The date and time when the to-do item was created.",
176180
"format": "date-time"
177181
}
178-
}
182+
},
183+
"description": "Represents a to-do item."
179184
},
180185
"Triangle": {
181186
"type": "object",

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,24 @@
136136
"properties": {
137137
"id": {
138138
"type": "integer",
139+
"description": "The unique identifier of the to-do item.",
139140
"format": "int32"
140141
},
141142
"title": {
142-
"type": "string"
143+
"type": "string",
144+
"description": "The title of the to-do item."
143145
},
144146
"completed": {
145-
"type": "boolean"
147+
"type": "boolean",
148+
"description": "Indicates whether the to-do item is completed."
146149
},
147150
"createdAt": {
148151
"type": "string",
152+
"description": "The date and time when the to-do item was created.",
149153
"format": "date-time"
150154
}
151-
}
155+
},
156+
"description": "Represents a to-do item."
152157
},
153158
"TodoWithDueDate": {
154159
"required": [
@@ -162,23 +167,29 @@
162167
"properties": {
163168
"dueDate": {
164169
"type": "string",
170+
"description": "The due date of the to-do item.",
165171
"format": "date-time"
166172
},
167173
"id": {
168174
"type": "integer",
175+
"description": "The unique identifier of the to-do item.",
169176
"format": "int32"
170177
},
171178
"title": {
172-
"type": "string"
179+
"type": "string",
180+
"description": "The title of the to-do item."
173181
},
174182
"completed": {
175-
"type": "boolean"
183+
"type": "boolean",
184+
"description": "Indicates whether the to-do item is completed."
176185
},
177186
"createdAt": {
178187
"type": "string",
188+
"description": "The date and time when the to-do item was created.",
179189
"format": "date-time"
180190
}
181-
}
191+
},
192+
"description": "Represents a to-do item with a due date."
182193
}
183194
},
184195
"securitySchemes": {

0 commit comments

Comments
 (0)