From f69c68fc9f513cb4394ec774283fd3a180f34b77 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 ee0db0a90..79f44a3bf 100644 --- a/src/NJsonSchema.Tests/Generation/SampleJsonDataGeneratorTests.cs +++ b/src/NJsonSchema.Tests/Generation/SampleJsonDataGeneratorTests.cs @@ -245,6 +245,53 @@ public async Task SchemaWithRecursiveDefinition() Assert.True(validationResult.Count > 0); // It is expected to fail validating the recursive properties (because of max recursion level) } + [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() {