Skip to content

Commit fa133ec

Browse files
Add circular reference test
Add a test for circular schema references.
1 parent 25de3e3 commit fa133ec

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

test/Microsoft.OpenApi.Tests/Validations/OpenApiDocumentValidationTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,65 @@ public static void ValidateSchemaReferencesAreInvalid()
131131
Assert.Equal("The schema reference '#/components/schemas/Pet' does not point to an existing schema.", error.Message);
132132
Assert.Equal("#/paths/~1pets/get/responses/200/content/application~1json/schema/$ref", error.Pointer);
133133
}
134+
135+
[Fact]
136+
public static void ValidateCircularSchemaReferencesAreDetected()
137+
{
138+
// Arrange
139+
var document = new OpenApiDocument
140+
{
141+
Components = new OpenApiComponents(),
142+
Info = new OpenApiInfo
143+
{
144+
Title = "Infinite Document",
145+
Version = "1.0.0"
146+
},
147+
Paths = [],
148+
Workspace = new()
149+
};
150+
151+
document.AddComponent("Cycle", new OpenApiSchema
152+
{
153+
Type = JsonSchemaType.Object,
154+
Properties = new Dictionary<string, IOpenApiSchema>()
155+
{
156+
["self"] = new OpenApiSchemaReference("#/components/schemas/Cycle/properties/self", document)
157+
}
158+
});
159+
160+
document.Paths.Add("/cycle", new OpenApiPathItem
161+
{
162+
Operations = new Dictionary<HttpMethod, OpenApiOperation>()
163+
{
164+
[HttpMethod.Get] = new OpenApiOperation
165+
{
166+
Responses = new()
167+
{
168+
["200"] = new OpenApiResponse
169+
{
170+
Description = "OK",
171+
Content = new Dictionary<string, OpenApiMediaType>()
172+
{
173+
["application/json"] = new OpenApiMediaType
174+
{
175+
Schema = new OpenApiSchemaReference("Cycle", document)
176+
}
177+
}
178+
}
179+
}
180+
}
181+
}
182+
});
183+
184+
// Act
185+
var errors = document.Validate(ValidationRuleSet.GetDefaultRuleSet());
186+
var result = !errors.Any();
187+
188+
// Assert
189+
Assert.False(result);
190+
Assert.NotNull(errors);
191+
var error = Assert.Single(errors);
192+
Assert.Equal("Circular reference detected while resolving schema: #/components/schemas/Cycle/properties/self", error.Message);
193+
Assert.Equal("#/components/schemas/Cycle/properties/self/$ref", error.Pointer);
194+
}
134195
}

0 commit comments

Comments
 (0)