-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix errors from dynamic scope when validation schema load (#340)
- Loading branch information
Showing
3 changed files
with
131 additions
and
8 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
Src/Newtonsoft.Json.Schema.Tests/Issues/Issue0339Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#region License | ||
// Copyright (c) Newtonsoft. All Rights Reserved. | ||
// License: https://raw.github.com/JamesNK/Newtonsoft.Json.Schema/master/LICENSE.md | ||
#endregion | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using Newtonsoft.Json.Linq; | ||
using Newtonsoft.Json.Schema.Generation; | ||
using Newtonsoft.Json.Serialization; | ||
#if DNXCORE50 | ||
using Xunit; | ||
using Test = Xunit.FactAttribute; | ||
using Assert = Newtonsoft.Json.Schema.Tests.XUnitAssert; | ||
#else | ||
using NUnit.Framework; | ||
#endif | ||
|
||
namespace Newtonsoft.Json.Schema.Tests.Issues | ||
{ | ||
[TestFixture] | ||
public class Issue0339Tests : TestFixtureBase | ||
{ | ||
[Test] | ||
public void RecursiveRef_DynamicSelection_NoLoadErrors() | ||
{ | ||
string schemaJson = @"{ | ||
""$id"": ""recursiveRef8_main.json"", | ||
""$defs"": { | ||
""inner"": { | ||
""$id"": ""recursiveRef8_inner.json"", | ||
""$recursiveAnchor"": true, | ||
""title"": ""inner"", | ||
""additionalProperties"": { | ||
""$recursiveRef"": ""#"" | ||
} | ||
} | ||
}, | ||
""if"": { | ||
""propertyNames"": { | ||
""pattern"": ""^[a-m]"" | ||
} | ||
}, | ||
""then"": { | ||
""title"": ""any type of node"", | ||
""$id"": ""recursiveRef8_anyLeafNode.json"", | ||
""$recursiveAnchor"": true, | ||
""$ref"": ""recursiveRef8_main.json#/$defs/inner"" | ||
}, | ||
""else"": { | ||
""title"": ""integer node"", | ||
""$id"": ""recursiveRef8_integerNode.json"", | ||
""$recursiveAnchor"": true, | ||
""type"": [ ""object"", ""integer"" ], | ||
""$ref"": ""recursiveRef8_main.json#/$defs/inner"" | ||
} | ||
}"; | ||
|
||
List<ValidationError> schemaErrors = new List<ValidationError>(); | ||
JSchemaReaderSettings settings = new JSchemaReaderSettings(); | ||
settings.ValidationEventHandler += (sender, args) => schemaErrors.Add(args.ValidationError); | ||
|
||
JSchema schema = JSchema.Parse(schemaJson, settings); | ||
|
||
Assert.AreEqual(0, schemaErrors.Count); | ||
} | ||
|
||
[Test] | ||
public void RecursiveRef_DynamicSelection_Duplicates_HasLoadErrors() | ||
{ | ||
string schemaJson = @"{ | ||
""$id"": ""recursiveRef8_main.json"", | ||
""$defs"": { | ||
""inner"": { | ||
""$id"": ""recursiveRef8_inner.json"", | ||
""$recursiveAnchor"": true, | ||
""title"": ""inner"", | ||
""additionalProperties"": { | ||
""$recursiveRef"": ""#"" | ||
} | ||
}, | ||
""inner2"": { | ||
""$id"": ""recursiveRef8_inner.json"", | ||
""$recursiveAnchor"": true, | ||
""title"": ""inner"", | ||
""additionalProperties"": { | ||
""$recursiveRef"": ""#"" | ||
} | ||
} | ||
}, | ||
""if"": { | ||
""propertyNames"": { | ||
""pattern"": ""^[a-m]"" | ||
} | ||
}, | ||
""then"": { | ||
""title"": ""any type of node"", | ||
""$id"": ""recursiveRef8_anyLeafNode.json"", | ||
""$recursiveAnchor"": true, | ||
""$ref"": ""recursiveRef8_main.json#/$defs/inner"" | ||
}, | ||
""else"": { | ||
""title"": ""integer node"", | ||
""$id"": ""recursiveRef8_anyLeafNode.json"", | ||
""$recursiveAnchor"": true, | ||
""type"": [ ""object"", ""integer"" ], | ||
""$ref"": ""recursiveRef8_main.json#/$defs/inner2"" | ||
} | ||
}"; | ||
|
||
List<ValidationError> schemaErrors = new List<ValidationError>(); | ||
JSchemaReaderSettings settings = new JSchemaReaderSettings(); | ||
settings.ValidationEventHandler += (sender, args) => schemaErrors.Add(args.ValidationError); | ||
|
||
JSchema schema = JSchema.Parse(schemaJson, settings); | ||
|
||
Assert.AreEqual(4, schemaErrors.Count); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters