Skip to content

Commit 4a3ee61

Browse files
Add schema tests
Validate the snapshots against the OpenAPI JSON schemas.
1 parent 3fc696f commit 4a3ee61

8 files changed

Lines changed: 4752 additions & 18 deletions

File tree

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
2424
<PackageVersion Include="MSBuild.ProjectCreation" Version="17.0.1" />
2525
<PackageVersion Include="Newtonsoft.Json" Version="13.0.4" />
26+
<PackageVersion Include="NJsonSchema" Version="11.5.2" />
2627
<PackageVersion Include="NSubstitute" Version="5.3.0" />
2728
<PackageVersion Include="NSwag.CodeGeneration.CSharp" Version="14.6.3" />
2829
<PackageVersion Include="NSwag.MSBuild" Version="14.6.3" />

test/Swashbuckle.AspNetCore.IntegrationTests/CodeGenerationTests.cs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#if NET10_0_OR_GREATER
22

33
using System.Reflection;
4-
using System.Text.Json;
54
using Microsoft.AspNetCore.Http;
65
using Microsoft.Kiota.Abstractions.Authentication;
76
using Microsoft.Kiota.Http.HttpClientLibrary;
@@ -20,28 +19,17 @@ public static TheoryData<ClientGeneratorTool, string> SnapshotTestCases()
2019
{
2120
var testCases = new TheoryData<ClientGeneratorTool, string>();
2221

23-
foreach (var path in Directory.EnumerateFiles(Path.Combine(GetProjectRoot(), "snapshots"), "*.txt", SearchOption.AllDirectories))
22+
foreach (var testCase in SnapshotTestData.Snapshots())
2423
{
24+
var path = testCase.Data.Item1;
25+
var documentVersion = testCase.Data.Item2;
26+
2527
// Deduplicate by ignoring snapshots for other TFMs
2628
if (!path.EndsWith(".DotNet10_0.verified.txt", StringComparison.Ordinal))
2729
{
2830
continue;
2931
}
3032

31-
using var snapshot = File.OpenRead(path);
32-
using var document = JsonDocument.Parse(snapshot);
33-
34-
if (!document.RootElement.TryGetProperty("openapi", out var property) &&
35-
!document.RootElement.TryGetProperty("swagger", out property))
36-
{
37-
continue;
38-
}
39-
40-
if (!Version.TryParse(property.GetString(), out var documentVersion))
41-
{
42-
continue;
43-
}
44-
4533
var version = documentVersion switch
4634
{
4735
{ Major: 2 } => OpenApiSpecVersion.OpenApi2_0,
@@ -70,11 +58,13 @@ public static TheoryData<ClientGeneratorTool, string> SnapshotTestCases()
7058

7159
[Theory]
7260
[MemberData(nameof(SnapshotTestCases))]
73-
public async Task OpenApiDocument_Generates_Valid_Client_Code_From_Snapshot(ClientGeneratorTool tool, string path)
61+
public async Task OpenApiDocument_Generates_Valid_Client_Code_From_Snapshot(ClientGeneratorTool tool, string snapshot)
7462
{
7563
// Arrange
7664
var generator = new ClientGenerator(outputHelper);
77-
var document = await File.ReadAllTextAsync(path, TestContext.Current.CancellationToken);
65+
var snapshotPath = Path.Combine(SnapshotTestData.SnapshotsPath(), snapshot);
66+
67+
var document = await File.ReadAllTextAsync(snapshotPath, TestContext.Current.CancellationToken);
7868

7969
using var project = await generator.GenerateFromStringAsync(tool, document);
8070

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using NJsonSchema;
2+
3+
namespace Swashbuckle.AspNetCore.IntegrationTests;
4+
5+
public class SchemaTests
6+
{
7+
[Theory]
8+
[MemberData(nameof(SnapshotTestData.Snapshots), MemberType = typeof(SnapshotTestData))]
9+
public async Task OpenApiDocumentsAreValid(string snapshot, Version version)
10+
{
11+
// Arrange
12+
var cancellationToken = TestContext.Current.CancellationToken;
13+
14+
var schemaPath = Path.Combine(SnapshotTestData.SchemasPath(), $"schema.{version.Major}.{version.Minor}.json");
15+
var snapshotPath = Path.Combine(SnapshotTestData.SnapshotsPath(), snapshot);
16+
17+
var schema = await JsonSchema.FromFileAsync(schemaPath, cancellationToken);
18+
var specification = await File.ReadAllTextAsync(snapshotPath, cancellationToken);
19+
20+
// Act
21+
var actual = schema.Validate(specification);
22+
23+
// Assert
24+
Assert.NotNull(actual);
25+
Assert.Empty(actual);
26+
}
27+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Reflection;
2+
using System.Text.Json;
3+
4+
namespace Swashbuckle.AspNetCore.IntegrationTests;
5+
6+
public static class SnapshotTestData
7+
{
8+
private static string _projectRoot =
9+
typeof(SnapshotTestData).Assembly
10+
.GetCustomAttributes<AssemblyMetadataAttribute>()
11+
.First((p) => p.Key is "ProjectRoot")
12+
.Value!;
13+
14+
public static string SnapshotsPath() => Path.Combine(_projectRoot, "snapshots");
15+
16+
public static string SchemasPath() => Path.Combine(_projectRoot, "schemas");
17+
18+
public static TheoryData<string, Version> Snapshots()
19+
{
20+
var testCases = new TheoryData<string, Version>();
21+
var snapshotsPath = Path.Combine(_projectRoot, "snapshots");
22+
23+
foreach (var path in Directory.EnumerateFiles(snapshotsPath, "*.txt", SearchOption.AllDirectories))
24+
{
25+
using var snapshot = File.OpenRead(path);
26+
using var document = JsonDocument.Parse(snapshot);
27+
28+
if (!document.RootElement.TryGetProperty("openapi", out var property) &&
29+
!document.RootElement.TryGetProperty("swagger", out property))
30+
{
31+
continue;
32+
}
33+
34+
if (!Version.TryParse(property.GetString(), out var version))
35+
{
36+
continue;
37+
}
38+
39+
var relativePath = Path.GetRelativePath(snapshotsPath, path);
40+
41+
testCases.Add(relativePath, version);
42+
}
43+
44+
return testCases;
45+
}
46+
}

test/Swashbuckle.AspNetCore.IntegrationTests/Swashbuckle.AspNetCore.IntegrationTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<PackageReference Include="Microsoft.NET.Test.Sdk" />
2222
<PackageReference Include="Microsoft.OpenApi.YamlReader" />
2323
<PackageReference Include="Microsoft.Playwright" />
24+
<PackageReference Include="NJsonSchema" />
2425
<PackageReference Include="Verify.XunitV3" />
2526
<PackageReference Include="xunit.runner.visualstudio" />
2627
<PackageReference Include="xunit.v3.mtp-off" />

0 commit comments

Comments
 (0)