From 3216d046e13cb034d13c5dc6701d7df1925b21ff Mon Sep 17 00:00:00 2001 From: Flemming Madsen Date: Sun, 2 Oct 2022 09:02:58 +0200 Subject: [PATCH] Add test of recursion level --- .../SampleJsonDataGeneratorTests.cs | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/NJsonSchema.Tests/Generation/SampleJsonDataGeneratorTests.cs b/src/NJsonSchema.Tests/Generation/SampleJsonDataGeneratorTests.cs index 48a3094ae..171f8dfa9 100644 --- a/src/NJsonSchema.Tests/Generation/SampleJsonDataGeneratorTests.cs +++ b/src/NJsonSchema.Tests/Generation/SampleJsonDataGeneratorTests.cs @@ -245,6 +245,53 @@ public async Task SchemaWithRecursiveDefinition() Assert.Equal(1.000012, testJson.SelectToken("body.numberContent.value").Value()); } + [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() {