Skip to content

Snapshot OpenApiDocument as JSON #3315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions test/Swashbuckle.AspNetCore.SwaggerGen.Test/VerifyTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
Expand All @@ -14,14 +16,15 @@
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
using Microsoft.AspNetCore.Routing;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Writers;
using Swashbuckle.AspNetCore.SwaggerGen.Test.Fixtures;
using Swashbuckle.AspNetCore.TestSupport;
using VerifyXunit;
using Xunit;

namespace Swashbuckle.AspNetCore.SwaggerGen.Test;

public class VerifyTests
public partial class VerifyTests
{
[Fact]
public async Task ApiDescriptionsWithMatchingGroupName()
Expand Down Expand Up @@ -406,7 +409,7 @@ public async Task ApiParametersThatAreNotBoundToBodyOrForm(string bindingSourceI

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

await Verifier.Verify(document)
await Verifier.Verify(ToJson(document))
.UseDirectory("snapshots")
.UseParameters(bindingSourceId)
.UniqueForTargetFrameworkAndVersion();
Expand Down Expand Up @@ -523,7 +526,7 @@ public async Task ActionsWithIllegalHeaderParameters(string action)

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

await Verifier.Verify(document)
await Verifier.Verify(ToJson(document))
.UseDirectory("snapshots")
.UseParameters(action)
.UniqueForTargetFrameworkAndVersion();
Expand Down Expand Up @@ -594,7 +597,7 @@ public async Task ActionParameterIsIllegalHeaderParameterWithProvidedOpenApiOper

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

await Verifier.Verify(document)
await Verifier.Verify(ToJson(document))
.UseDirectory("snapshots")
.UseParameters(action)
.UseMethodName("IllegalHeaderForOperation")
Expand Down Expand Up @@ -654,7 +657,7 @@ public async Task ActionWithRequiredQueryParameter(string action)

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

await Verifier.Verify(document)
await Verifier.Verify(ToJson(document))
.UseDirectory("snapshots")
.UseParameters(action)
.UniqueForTargetFrameworkAndVersion();
Expand Down Expand Up @@ -690,7 +693,7 @@ public async Task ActionWithRequiredBodyParameter(string action)

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

await Verifier.Verify(document)
await Verifier.Verify(ToJson(document))
.UseDirectory("snapshots")
.UseParameters(action)
.UniqueForTargetFrameworkAndVersion();
Expand Down Expand Up @@ -895,7 +898,7 @@ public async Task ActionHasConsumesAttribute(string bindingSourceId)

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

await Verifier.Verify(document)
await Verifier.Verify(ToJson(document))
.UseDirectory("snapshots")
.UseParameters(bindingSourceId)
.UniqueForTargetFrameworkAndVersion();
Expand Down Expand Up @@ -1488,10 +1491,26 @@ private static SwaggerGenerator Subject(
}
};

private static string ToJson(OpenApiDocument document)
{
using var stringWriter = new StringWriter();
var jsonWriter = new OpenApiJsonWriter(stringWriter);

document.SerializeAsV3(jsonWriter);

return NormalizeLineBreaks(stringWriter.ToString());
}

private static async Task Verify(OpenApiDocument document)
{
await Verifier.Verify(document)
await Verifier.Verify(ToJson(document))
.UseDirectory("snapshots")
.UniqueForTargetFrameworkAndVersion();
}

private static string NormalizeLineBreaks(string swagger)
=> UnixNewLineRegex().Replace(swagger, "\\r\\n");

[GeneratedRegex(@"(?<!\\r)\\n")]
private static partial Regex UnixNewLineRegex();
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,31 @@
{
Info: {
Title: Test API,
Version: V1
"openapi": "3.0.1",
"info": {
"title": "Test API",
"version": "V1"
},
Paths: {
/resource: {
Operations: {
Post: {
Tags: [
{
Name: Fake,
UnresolvedReference: false
}
],
RequestBody: {
UnresolvedReference: false,
Required: false,
Content: {
application/someMediaType: {
Schema: {
Type: string,
ReadOnly: false,
WriteOnly: false,
AdditionalPropertiesAllowed: true,
Nullable: false,
Deprecated: false,
UnresolvedReference: false
}
"paths": {
"/resource": {
"post": {
"tags": [
"Fake"
],
"requestBody": {
"content": {
"application/someMediaType": {
"schema": {
"type": "string"
}
}
},
Responses: {
200: {
Description: OK,
UnresolvedReference: false
}
},
Deprecated: false
}
},
"responses": {
"200": {
"description": "OK"
}
}
},
UnresolvedReference: false
}
}
},
Components: {},
HashCode: 4CB047722B57E9853D09B038EC48DB9A63B1EE5C19DDFD234A172615EE6225134356F1B92FB3881C2D7DAA40B77D534CB317F93B407B4E7B6B74480224A9E7B7
"components": { }
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,31 @@
{
Info: {
Title: Test API,
Version: V1
"openapi": "3.0.1",
"info": {
"title": "Test API",
"version": "V1"
},
Paths: {
/resource: {
Operations: {
Post: {
Tags: [
{
Name: Fake,
UnresolvedReference: false
}
],
RequestBody: {
UnresolvedReference: false,
Required: false,
Content: {
application/someMediaType: {
Schema: {
Type: string,
ReadOnly: false,
WriteOnly: false,
AdditionalPropertiesAllowed: true,
Nullable: false,
Deprecated: false,
UnresolvedReference: false
}
"paths": {
"/resource": {
"post": {
"tags": [
"Fake"
],
"requestBody": {
"content": {
"application/someMediaType": {
"schema": {
"type": "string"
}
}
},
Responses: {
200: {
Description: OK,
UnresolvedReference: false
}
},
Deprecated: false
}
},
"responses": {
"200": {
"description": "OK"
}
}
},
UnresolvedReference: false
}
}
},
Components: {},
HashCode: 4CB047722B57E9853D09B038EC48DB9A63B1EE5C19DDFD234A172615EE6225134356F1B92FB3881C2D7DAA40B77D534CB317F93B407B4E7B6B74480224A9E7B7
"components": { }
}
Original file line number Diff line number Diff line change
@@ -1,63 +1,41 @@
{
Info: {
Title: Test API,
Version: V1
"openapi": "3.0.1",
"info": {
"title": "Test API",
"version": "V1"
},
Paths: {
/resource: {
Operations: {
Post: {
Tags: [
{
Name: Fake,
UnresolvedReference: false
}
],
RequestBody: {
UnresolvedReference: false,
Required: false,
Content: {
application/someMediaType: {
Schema: {
Type: object,
ReadOnly: false,
WriteOnly: false,
Properties: {
param: {
Type: string,
ReadOnly: false,
WriteOnly: false,
AdditionalPropertiesAllowed: true,
Nullable: false,
Deprecated: false,
UnresolvedReference: false
}
},
AdditionalPropertiesAllowed: true,
Nullable: false,
Deprecated: false,
UnresolvedReference: false
},
Encoding: {
param: {
Style: Form
"paths": {
"/resource": {
"post": {
"tags": [
"Fake"
],
"requestBody": {
"content": {
"application/someMediaType": {
"schema": {
"type": "object",
"properties": {
"param": {
"type": "string"
}
}
},
"encoding": {
"param": {
"style": "form"
}
}
}
},
Responses: {
200: {
Description: OK,
UnresolvedReference: false
}
},
Deprecated: false
}
},
"responses": {
"200": {
"description": "OK"
}
}
},
UnresolvedReference: false
}
}
},
Components: {},
HashCode: E5B7D5A3A69521A769EA033C2E26073D98F03E899B1DFE0A7CF240CE54D26EA3294FDF9D39DCABBB4F32CA029376F8CF147C001935036911E75750C367FD288E
"components": { }
}
Loading