Skip to content

Commit 2e9d107

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

16 files changed

+495
-1479
lines changed

src/OpenApi/sample/Controllers/TestController.cs

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

44
using System.ComponentModel.DataAnnotations;
55
using System.Diagnostics.CodeAnalysis;
6+
using Microsoft.AspNetCore.Http.HttpResults;
67
using Microsoft.AspNetCore.Mvc;
78

89
[ApiController]
@@ -17,6 +18,13 @@ public string GetByIdAndName(RouteParamsContainer paramsContainer)
1718
return paramsContainer.Id + "_" + paramsContainer.Name;
1819
}
1920

21+
[HttpGet]
22+
[Route("/gettypedresult")]
23+
public Ok<MvcTodo> GetTypedResult()
24+
{
25+
return TypedResults.Ok(new MvcTodo("Title", "Description", true));
26+
}
27+
2028
[HttpPost]
2129
[Route("/forms")]
2230
public IActionResult PostForm([FromForm] MvcTodo todo)
Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
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)
3140
.UseParameters(documentName);
3241
}
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-
}
4142
}

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)