Skip to content

Commit

Permalink
Add test of recursion level
Browse files Browse the repository at this point in the history
  • Loading branch information
leflings committed Oct 2, 2022
1 parent 21264b0 commit 3216d04
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/NJsonSchema.Tests/Generation/SampleJsonDataGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,53 @@ public async Task SchemaWithRecursiveDefinition()
Assert.Equal(1.000012, testJson.SelectToken("body.numberContent.value").Value<double>());
}

[Fact]
public async Task GeneratorAdheresToMaxRecursionLevel()
{
//// Arrange
var data = @"{
""$schema"": ""http://json-schema.org/draft-04/schema#"",
""title"": ""test schema"",
""type"": ""object"",
""required"": [
""body"", ""footer""
],
""properties"": {
""body"": {
""$ref"": ""#/definitions/body""
}
},
""definitions"": {
""body"": {
""type"": ""object"",
""additionalProperties"": false,
""properties"": {
""text"": { ""type"": ""string"", ""enum"": [""my_string""] },
""body"": {
""$ref"": ""#/definitions/body""
}
}
}
}
}";
var generator = new SampleJsonDataGenerator(new SampleJsonDataGeneratorSettings() { MaxRecursionLevel = 2 });
var schema = await JsonSchema.FromJsonAsync(data);
//// Act
var testJson = generator.Generate(schema);

//// Assert
var secondBodyToken = testJson.SelectToken("body.body");
Assert.NotNull(secondBodyToken);

var thirdBodyToken = testJson.SelectToken("body.body.body") as JValue;
Assert.NotNull(thirdBodyToken);
Assert.Equal(JTokenType.Null, thirdBodyToken.Type);

var validationResult = schema.Validate(testJson);
Assert.NotNull(validationResult);
Assert.True(validationResult.Count > 0); // It is expected to fail validating the recursive properties (because of max recursion level)
}

[Fact]
public async Task SchemaWithDefinitionUseMultipleTimes()
{
Expand Down

0 comments on commit 3216d04

Please sign in to comment.