Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undo a change made to refkeyword #80

Merged
merged 2 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions Json.More/Json.More.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 99 additions & 0 deletions JsonSchema.Tests/GithubTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using Json.More;
using NUnit.Framework;

namespace Json.Schema.Tests
Expand Down Expand Up @@ -373,5 +375,102 @@ public void Issue76_GetHashCodeIsNotConsistent_WhitespaceInObjectValue()
Assert.IsTrue(schema1.Equals(schema2));
Assert.AreEqual(schema1.GetHashCode(), schema2.GetHashCode());
}

[Test]
public void Issue79_RefsTryingToResolveParent()
{
var schema1Str = @"
{
""$schema"": ""http://json-schema.org/draft-07/schema#"",
""$id"": ""schema1.json"",
""definitions"": {
""myDef"": {
""properties"": {
""abc"": { ""type"": ""string"" }
}
}
},
""$ref"": ""#/definitions/myDef""
}";
var schema2Str = @"
{
""$schema"": ""http://json-schema.org/draft-07/schema#"",
""$id"": ""schema2.json"",
""$ref"": ""schema1.json""
}";
var jsonStr = @"{ ""abc"": ""s"" }";
var schema1 = JsonSerializer.Deserialize<JsonSchema>(schema1Str);
var schema2 = JsonSerializer.Deserialize<JsonSchema>(schema2Str);
var json = JsonDocument.Parse(jsonStr).RootElement;
var uri1 = new Uri("http://first.com/schema1.json");
var uri2 = new Uri("http://first.com/schema2.json");
var firstBaseUri = new Uri("http://first.com");
var map = new Dictionary<Uri, JsonSchema>
{
{ uri1, schema1 },
{ uri2, schema2 },
};
var options = new ValidationOptions
{
OutputFormat = OutputFormat.Verbose,
DefaultBaseUri = firstBaseUri,
SchemaRegistry =
{
Fetch = uri =>
{
Console.WriteLine("Fetching {0}", uri);
return map.TryGetValue(uri, out var ret) ? ret : null;
//return map[uri];
}
}
};
var validation = schema2.Validate(json, options);
Console.WriteLine("Validation: {0}", JsonSerializer.Serialize(validation, new JsonSerializerOptions {WriteIndented = true}));
}

[Test]
public void Issue79_RefsTryingToResolveParent_Explanation()
{
var schemaText = @"{
""$id"": ""https://mydomain.com/outer"",
""properties"": {
""foo"": {
""$id"": ""https://mydomain.com/foo"",
""properties"": {
""inner1"": {
""$anchor"": ""bar"",
""type"": ""string""
},
""inner2"": {
""$ref"": ""#bar""
}
}
},
""bar"": {
""$anchor"": ""bar"",
""type"": ""integer""
}
}
}";
var passingText = @"
{
""foo"": {
""inner2"": ""value""
}
}";
var failingText = @"
{
""foo"": {
""inner2"": 42
}
}";

var schema = JsonSerializer.Deserialize<JsonSchema>(schemaText);
var passing = JsonDocument.Parse(passingText).RootElement;
var failing = JsonDocument.Parse(failingText).RootElement;

schema.Validate(passing, new ValidationOptions{OutputFormat = OutputFormat.Detailed}).AssertValid();
schema.Validate(failing, new ValidationOptions{OutputFormat = OutputFormat.Detailed}).AssertInvalid();
}
}
}
4 changes: 2 additions & 2 deletions JsonSchema/JsonSchema.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<PackageProjectUrl>https://github.com/gregsdennis/json-everything</PackageProjectUrl>
<RepositoryUrl>https://github.com/gregsdennis/json-everything</RepositoryUrl>
<PackageTags>json-schema validation schema json</PackageTags>
<Version>1.9.1</Version>
<FileVersion>1.9.1.0</FileVersion>
<Version>1.9.2</Version>
<FileVersion>1.9.2.0</FileVersion>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<AssemblyName>JsonSchema.Net</AssemblyName>
Expand Down
2 changes: 1 addition & 1 deletion JsonSchema/RefKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void Validate(ValidationContext context)
else
{
newUri = context.CurrentUri;
baseSchema = context.Options.SchemaRegistry.Get(newUri) ?? context.SchemaRoot;
baseSchema = context.SchemaRoot;
}

JsonSchema? schema;
Expand Down
4 changes: 4 additions & 0 deletions docs_source/release-notes/json-schema.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# [1.9.2](https://github.com/gregsdennis/json-everything/pull/80)

[#79](https://github.com/gregsdennis/json-everything/issues/79) - `$ref` was calling out to resolve a parent URI.

# [1.9.1](https://github.com/gregsdennis/json-everything/pull/77)

Fixes found by the release of the 2020-12 test suite.
Expand Down