Skip to content

Commit 5826b72

Browse files
Snapshot OpenApiDocument as JSON
Serialize the document before snapshotting to avoid test failures caused by internal refactoring in Microsoft.OpenApi ahead of v2.
1 parent f8bcb7d commit 5826b72

103 files changed

Lines changed: 2569 additions & 4708 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

test/Swashbuckle.AspNetCore.SwaggerGen.Test/VerifyTests.cs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Reflection;
56
using System.Text.Json;
7+
using System.Text.RegularExpressions;
68
using System.Threading.Tasks;
79
using Microsoft.AspNetCore.Authentication;
810
using Microsoft.AspNetCore.Http;
@@ -14,14 +16,15 @@
1416
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
1517
using Microsoft.AspNetCore.Routing;
1618
using Microsoft.OpenApi.Models;
19+
using Microsoft.OpenApi.Writers;
1720
using Swashbuckle.AspNetCore.SwaggerGen.Test.Fixtures;
1821
using Swashbuckle.AspNetCore.TestSupport;
1922
using VerifyXunit;
2023
using Xunit;
2124

2225
namespace Swashbuckle.AspNetCore.SwaggerGen.Test;
2326

24-
public class VerifyTests
27+
public partial class VerifyTests
2528
{
2629
[Fact]
2730
public async Task ApiDescriptionsWithMatchingGroupName()
@@ -406,7 +409,7 @@ public async Task ApiParametersThatAreNotBoundToBodyOrForm(string bindingSourceI
406409

407410
var document = subject.GetSwagger("v1");
408411

409-
await Verifier.Verify(document)
412+
await Verifier.Verify(ToJson(document))
410413
.UseDirectory("snapshots")
411414
.UseParameters(bindingSourceId)
412415
.UniqueForTargetFrameworkAndVersion();
@@ -523,7 +526,7 @@ public async Task ActionsWithIllegalHeaderParameters(string action)
523526

524527
var document = subject.GetSwagger("v1");
525528

526-
await Verifier.Verify(document)
529+
await Verifier.Verify(ToJson(document))
527530
.UseDirectory("snapshots")
528531
.UseParameters(action)
529532
.UniqueForTargetFrameworkAndVersion();
@@ -594,7 +597,7 @@ public async Task ActionParameterIsIllegalHeaderParameterWithProvidedOpenApiOper
594597

595598
var document = subject.GetSwagger("v1");
596599

597-
await Verifier.Verify(document)
600+
await Verifier.Verify(ToJson(document))
598601
.UseDirectory("snapshots")
599602
.UseParameters(action)
600603
.UseMethodName("IllegalHeaderForOperation")
@@ -654,7 +657,7 @@ public async Task ActionWithRequiredQueryParameter(string action)
654657

655658
var document = subject.GetSwagger("v1");
656659

657-
await Verifier.Verify(document)
660+
await Verifier.Verify(ToJson(document))
658661
.UseDirectory("snapshots")
659662
.UseParameters(action)
660663
.UniqueForTargetFrameworkAndVersion();
@@ -690,7 +693,7 @@ public async Task ActionWithRequiredBodyParameter(string action)
690693

691694
var document = subject.GetSwagger("v1");
692695

693-
await Verifier.Verify(document)
696+
await Verifier.Verify(ToJson(document))
694697
.UseDirectory("snapshots")
695698
.UseParameters(action)
696699
.UniqueForTargetFrameworkAndVersion();
@@ -895,7 +898,7 @@ public async Task ActionHasConsumesAttribute(string bindingSourceId)
895898

896899
var document = subject.GetSwagger("v1");
897900

898-
await Verifier.Verify(document)
901+
await Verifier.Verify(ToJson(document))
899902
.UseDirectory("snapshots")
900903
.UseParameters(bindingSourceId)
901904
.UniqueForTargetFrameworkAndVersion();
@@ -1488,10 +1491,26 @@ private static SwaggerGenerator Subject(
14881491
}
14891492
};
14901493

1494+
private static string ToJson(OpenApiDocument document)
1495+
{
1496+
using var stringWriter = new StringWriter();
1497+
var jsonWriter = new OpenApiJsonWriter(stringWriter);
1498+
1499+
document.SerializeAsV3(jsonWriter);
1500+
1501+
return NormalizeLineBreaks(stringWriter.ToString());
1502+
}
1503+
14911504
private static async Task Verify(OpenApiDocument document)
14921505
{
1493-
await Verifier.Verify(document)
1506+
await Verifier.Verify(ToJson(document))
14941507
.UseDirectory("snapshots")
14951508
.UniqueForTargetFrameworkAndVersion();
14961509
}
1510+
1511+
private static string NormalizeLineBreaks(string swagger)
1512+
=> UnixNewLineRegex().Replace(swagger, "\\r\\n");
1513+
1514+
[GeneratedRegex(@"(?<!\\r)\\n")]
1515+
private static partial Regex UnixNewLineRegex();
14971516
}
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,31 @@
11
{
2-
Info: {
3-
Title: Test API,
4-
Version: V1
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "Test API",
5+
"version": "V1"
56
},
6-
Paths: {
7-
/resource: {
8-
Operations: {
9-
Post: {
10-
Tags: [
11-
{
12-
Name: Fake,
13-
UnresolvedReference: false
14-
}
15-
],
16-
RequestBody: {
17-
UnresolvedReference: false,
18-
Required: false,
19-
Content: {
20-
application/someMediaType: {
21-
Schema: {
22-
Type: string,
23-
ReadOnly: false,
24-
WriteOnly: false,
25-
AdditionalPropertiesAllowed: true,
26-
Nullable: false,
27-
Deprecated: false,
28-
UnresolvedReference: false
29-
}
7+
"paths": {
8+
"/resource": {
9+
"post": {
10+
"tags": [
11+
"Fake"
12+
],
13+
"requestBody": {
14+
"content": {
15+
"application/someMediaType": {
16+
"schema": {
17+
"type": "string"
3018
}
3119
}
32-
},
33-
Responses: {
34-
200: {
35-
Description: OK,
36-
UnresolvedReference: false
37-
}
38-
},
39-
Deprecated: false
20+
}
21+
},
22+
"responses": {
23+
"200": {
24+
"description": "OK"
25+
}
4026
}
41-
},
42-
UnresolvedReference: false
27+
}
4328
}
4429
},
45-
Components: {},
46-
HashCode: 4CB047722B57E9853D09B038EC48DB9A63B1EE5C19DDFD234A172615EE6225134356F1B92FB3881C2D7DAA40B77D534CB317F93B407B4E7B6B74480224A9E7B7
30+
"components": { }
4731
}
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,31 @@
11
{
2-
Info: {
3-
Title: Test API,
4-
Version: V1
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "Test API",
5+
"version": "V1"
56
},
6-
Paths: {
7-
/resource: {
8-
Operations: {
9-
Post: {
10-
Tags: [
11-
{
12-
Name: Fake,
13-
UnresolvedReference: false
14-
}
15-
],
16-
RequestBody: {
17-
UnresolvedReference: false,
18-
Required: false,
19-
Content: {
20-
application/someMediaType: {
21-
Schema: {
22-
Type: string,
23-
ReadOnly: false,
24-
WriteOnly: false,
25-
AdditionalPropertiesAllowed: true,
26-
Nullable: false,
27-
Deprecated: false,
28-
UnresolvedReference: false
29-
}
7+
"paths": {
8+
"/resource": {
9+
"post": {
10+
"tags": [
11+
"Fake"
12+
],
13+
"requestBody": {
14+
"content": {
15+
"application/someMediaType": {
16+
"schema": {
17+
"type": "string"
3018
}
3119
}
32-
},
33-
Responses: {
34-
200: {
35-
Description: OK,
36-
UnresolvedReference: false
37-
}
38-
},
39-
Deprecated: false
20+
}
21+
},
22+
"responses": {
23+
"200": {
24+
"description": "OK"
25+
}
4026
}
41-
},
42-
UnresolvedReference: false
27+
}
4328
}
4429
},
45-
Components: {},
46-
HashCode: 4CB047722B57E9853D09B038EC48DB9A63B1EE5C19DDFD234A172615EE6225134356F1B92FB3881C2D7DAA40B77D534CB317F93B407B4E7B6B74480224A9E7B7
30+
"components": { }
4731
}
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,41 @@
11
{
2-
Info: {
3-
Title: Test API,
4-
Version: V1
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "Test API",
5+
"version": "V1"
56
},
6-
Paths: {
7-
/resource: {
8-
Operations: {
9-
Post: {
10-
Tags: [
11-
{
12-
Name: Fake,
13-
UnresolvedReference: false
14-
}
15-
],
16-
RequestBody: {
17-
UnresolvedReference: false,
18-
Required: false,
19-
Content: {
20-
application/someMediaType: {
21-
Schema: {
22-
Type: object,
23-
ReadOnly: false,
24-
WriteOnly: false,
25-
Properties: {
26-
param: {
27-
Type: string,
28-
ReadOnly: false,
29-
WriteOnly: false,
30-
AdditionalPropertiesAllowed: true,
31-
Nullable: false,
32-
Deprecated: false,
33-
UnresolvedReference: false
34-
}
35-
},
36-
AdditionalPropertiesAllowed: true,
37-
Nullable: false,
38-
Deprecated: false,
39-
UnresolvedReference: false
40-
},
41-
Encoding: {
42-
param: {
43-
Style: Form
7+
"paths": {
8+
"/resource": {
9+
"post": {
10+
"tags": [
11+
"Fake"
12+
],
13+
"requestBody": {
14+
"content": {
15+
"application/someMediaType": {
16+
"schema": {
17+
"type": "object",
18+
"properties": {
19+
"param": {
20+
"type": "string"
4421
}
4522
}
23+
},
24+
"encoding": {
25+
"param": {
26+
"style": "form"
27+
}
4628
}
4729
}
48-
},
49-
Responses: {
50-
200: {
51-
Description: OK,
52-
UnresolvedReference: false
53-
}
54-
},
55-
Deprecated: false
30+
}
31+
},
32+
"responses": {
33+
"200": {
34+
"description": "OK"
35+
}
5636
}
57-
},
58-
UnresolvedReference: false
37+
}
5938
}
6039
},
61-
Components: {},
62-
HashCode: E5B7D5A3A69521A769EA033C2E26073D98F03E899B1DFE0A7CF240CE54D26EA3294FDF9D39DCABBB4F32CA029376F8CF147C001935036911E75750C367FD288E
40+
"components": { }
6341
}

0 commit comments

Comments
 (0)