Skip to content

Commit

Permalink
Fix validating duplicate key in dependencies and dependentSchemas (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored Feb 21, 2021
1 parent 714cfeb commit 1e9d980
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 4 deletions.
94 changes: 94 additions & 0 deletions Src/Newtonsoft.Json.Schema.Tests/JSchemaValidatingReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3500,6 +3500,100 @@ public void ValidateDynamicRef()
Assert.AreEqual(1, validationEventArgs.ValidationError.ChildErrors[0].ChildErrors.Count);
Assert.AreEqual("Invalid type. Expected Integer, Object but got Number.", validationEventArgs.ValidationError.ChildErrors[0].ChildErrors[0].Message);
}

[Test]
public void Read_KeyDuplicatedInDependenciesAndDependentSchemas_IgnoreSecond()
{
string json = @"{
""$schema"": ""http://json-schema.org/draft-07/schema"",
""$id"": ""http://api.example.com/profile.json"",
""title"": ""The Root Schema"",
""type"": ""object"",
""required"": [
""minimum"",
""workspace-v""
],
""properties"": {
""minimum"": {
""type"": ""object"",
""required"": [
""width_px"",
""height_px""
],
""properties"": {
""width_px"": {
""type"": ""integer""
},
""height_px"": {
""type"": ""integer""
}
}
},
""workspace-v"": {
""type"": ""object"",
""required"": [
""x_px"",
""y_px"",
""width_px"",
""height_px"",
""width_mm"",
""height_mm""
],
""properties"": {
""x_px"": {
""type"": ""integer""
},
""y_px"": {
""type"": ""integer""
},
""width_px"": {
""type"": ""integer""
},
""height_px"": {
""type"": ""integer""
},
""width_mm"": {
""type"": ""integer""
},
""height_mm"": {
""type"": ""integer""
}
}
},
""widgets"": {
""type"": ""array"",
""minProperties"": 1,
""uniqueItems"": true,
""items"": {
""type"": ""string"",
""pattern"": ""\\b(?:color|color_picker|center|resize|orientation)\\b$""
}
}
},
""dependencies"": {
""widgets"": {
""orientation"": {
""properties"": {
""workspace-h"": { ""type"": ""string"" }
},
""required"": [""workspace-h""]
}
}
},
""dependentSchemas"": {
""widgets"": {
""properties"": {
""workspace-h"": {""type"": ""string""}
}
}
}
}";

JSchema s = JSchema.Parse(json);

JObject o = new JObject();
Assert.IsFalse(o.IsValid(s));
}
}

public sealed class JsonReaderStubWithIsClosed : JsonReader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,14 @@ public void InitializeScopes(JsonToken token)
{
if (dependency.Value is JSchema dependencySchema)
{
SchemaScope scope = CreateTokenScope(token, dependencySchema, CreateConditionalContext(), null, InitialDepth);
_dependencyScopes!.Add(dependency.Key, scope);
ValidationUtils.Assert(_dependencyScopes != null);

// Could be duplicated in "dependencies" and "dependentSchemas"
if (!_dependencyScopes.ContainsKey(dependency.Key))
{
SchemaScope scope = CreateTokenScope(token, dependencySchema, CreateConditionalContext(), null, InitialDepth);
_dependencyScopes.Add(dependency.Key, scope);
}
}
}
}
Expand All @@ -453,8 +459,14 @@ public void InitializeScopes(JsonToken token)
{
if (dependency.Value is JSchema dependencySchema)
{
SchemaScope scope = CreateTokenScope(token, dependencySchema, CreateConditionalContext(), null, InitialDepth);
_dependencyScopes!.Add(dependency.Key, scope);
ValidationUtils.Assert(_dependencyScopes != null);

// Could be duplicated in "dependencies" and "dependentSchemas"
if (!_dependencyScopes.ContainsKey(dependency.Key))
{
SchemaScope scope = CreateTokenScope(token, dependencySchema, CreateConditionalContext(), null, InitialDepth);
_dependencyScopes.Add(dependency.Key, scope);
}
}
}
}
Expand Down

0 comments on commit 1e9d980

Please sign in to comment.