Skip to content

Commit 7c01430

Browse files
Add unit tests
Add two basic unit tests and a fast path for when the components are registered.
1 parent b14d3a2 commit 7c01430

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

src/Microsoft.OpenApi/Validations/Rules/OpenApiDocumentRules.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ public override void Visit(IOpenApiSchema schema)
8080

8181
private void ValidateSchemaReference(OpenApiSchemaReference reference)
8282
{
83+
if (reference.RecursiveTarget is not null)
84+
{
85+
// The reference was followed to a valid schema somewhere in the document
86+
return;
87+
}
88+
8389
var id = reference.Reference.ReferenceV3;
8490

8591
if (id is { Length: > 0 } && !IsValidSchemaReference(id, document))
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Net.Http;
7+
using Xunit;
8+
9+
namespace Microsoft.OpenApi.Validations.Tests;
10+
11+
public static class OpenApiDocumentValidationTests
12+
{
13+
[Fact]
14+
public static void ValidateSchemaReferencesAreValid()
15+
{
16+
// Arrange
17+
var document = new OpenApiDocument
18+
{
19+
Components = new OpenApiComponents(),
20+
Info = new OpenApiInfo
21+
{
22+
Title = "People Document",
23+
Version = "1.0.0"
24+
},
25+
Paths = [],
26+
Workspace = new()
27+
};
28+
29+
document.AddComponent("Person", new OpenApiSchema
30+
{
31+
Type = JsonSchemaType.Object,
32+
Properties = new Dictionary<string, IOpenApiSchema>()
33+
{
34+
["name"] = new OpenApiSchema { Type = JsonSchemaType.String },
35+
["email"] = new OpenApiSchema { Type = JsonSchemaType.String, Format = "email" }
36+
}
37+
});
38+
39+
document.Paths.Add("/people", new OpenApiPathItem
40+
{
41+
Operations = new Dictionary<HttpMethod, OpenApiOperation>()
42+
{
43+
[HttpMethod.Get] = new OpenApiOperation
44+
{
45+
Responses = new()
46+
{
47+
["200"] = new OpenApiResponse
48+
{
49+
Description = "OK",
50+
Content = new Dictionary<string, OpenApiMediaType>()
51+
{
52+
["application/json"] = new OpenApiMediaType
53+
{
54+
Schema = new OpenApiSchemaReference("Person", document),
55+
}
56+
}
57+
}
58+
}
59+
}
60+
}
61+
});
62+
63+
// Act
64+
var errors = document.Validate(ValidationRuleSet.GetDefaultRuleSet());
65+
var result = !errors.Any();
66+
67+
// Assert
68+
Assert.True(result);
69+
Assert.NotNull(errors);
70+
Assert.Empty(errors);
71+
}
72+
73+
[Fact]
74+
public static void ValidateSchemaReferencesAreInvalid()
75+
{
76+
// Arrange
77+
var document = new OpenApiDocument
78+
{
79+
Components = new OpenApiComponents(),
80+
Info = new OpenApiInfo
81+
{
82+
Title = "Pets Document",
83+
Version = "1.0.0"
84+
},
85+
Paths = [],
86+
Workspace = new()
87+
};
88+
89+
document.AddComponent("Person", new OpenApiSchema
90+
{
91+
Type = JsonSchemaType.Object,
92+
Properties = new Dictionary<string, IOpenApiSchema>()
93+
{
94+
["name"] = new OpenApiSchema { Type = JsonSchemaType.String },
95+
["email"] = new OpenApiSchema { Type = JsonSchemaType.String, Format = "email" }
96+
}
97+
});
98+
99+
document.Paths.Add("/pets", new OpenApiPathItem
100+
{
101+
Operations = new Dictionary<HttpMethod, OpenApiOperation>()
102+
{
103+
[HttpMethod.Get] = new OpenApiOperation
104+
{
105+
Responses = new()
106+
{
107+
["200"] = new OpenApiResponse
108+
{
109+
Description = "OK",
110+
Content = new Dictionary<string, OpenApiMediaType>()
111+
{
112+
["application/json"] = new OpenApiMediaType
113+
{
114+
Schema = new OpenApiSchemaReference("Pet", document),
115+
}
116+
}
117+
}
118+
}
119+
}
120+
}
121+
});
122+
123+
// Act
124+
var errors = document.Validate(ValidationRuleSet.GetDefaultRuleSet());
125+
var result = !errors.Any();
126+
127+
// Assert
128+
Assert.False(result);
129+
Assert.NotNull(errors);
130+
var error = Assert.Single(errors);
131+
Assert.Equal("The schema reference '#/components/schemas/Pet' does not point to an existing schema.", error.Message);
132+
Assert.Equal("#/paths/~1pets/get/responses/200/content/application~1json/schema", error.Pointer);
133+
}
134+
}

0 commit comments

Comments
 (0)