Skip to content

Commit 3a90110

Browse files
authored
Merge pull request #469 from gregsdennis/schema/base-document-subschemas-base-uri
replicate issue with test
2 parents 3aa49b4 + a1d952f commit 3a90110

File tree

4 files changed

+140
-50
lines changed

4 files changed

+140
-50
lines changed
Lines changed: 131 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,161 @@
11
using System;
22
using System.Text.Json;
33
using System.Text.Json.Nodes;
4+
using FluentAssertions;
45
using NUnit.Framework;
56

6-
namespace Json.Schema.Tests
7+
namespace Json.Schema.Tests;
8+
9+
public class BaseDocumentTests
710
{
8-
public class BaseDocumentTests
11+
[Test]
12+
public void SchemasEmbeddedInJsonCanBeReferenced_Valid()
913
{
10-
[Test]
11-
public void SchemasEmbeddedInJsonCanBeReferenced_Valid()
14+
JsonSchema targetSchema = new JsonSchemaBuilder()
15+
.Type(SchemaValueType.Integer);
16+
17+
var json = new JsonObject
18+
{
19+
["prop1"] = "foo",
20+
["prop2"] = new JsonArray
21+
{
22+
"bar",
23+
JsonSerializer.SerializeToNode(targetSchema)
24+
}
25+
};
26+
27+
var options = new EvaluationOptions
1228
{
13-
JsonSchema targetSchema = new JsonSchemaBuilder()
14-
.Type(SchemaValueType.Integer);
29+
OutputFormat = OutputFormat.List
30+
};
31+
32+
var jsonBaseDoc = new JsonNodeBaseDocument(json, new Uri("http://localhost:1234/doc"));
33+
options.SchemaRegistry.Register(jsonBaseDoc);
34+
35+
JsonSchema subjectSchema = new JsonSchemaBuilder()
36+
.Ref("http://localhost:1234/doc#/prop2/1");
37+
38+
JsonNode instance = 42;
39+
40+
var result = subjectSchema.Evaluate(instance, options);
41+
42+
result.AssertValid();
43+
}
44+
45+
[Test]
46+
public void SchemasEmbeddedInJsonCanBeReferenced_Invalid()
47+
{
48+
JsonSchema targetSchema = new JsonSchemaBuilder()
49+
.Type(SchemaValueType.Integer);
50+
51+
var json = new JsonObject
52+
{
53+
["prop1"] = "foo",
54+
["prop2"] = new JsonArray
55+
{
56+
"bar",
57+
JsonSerializer.SerializeToNode(targetSchema)
58+
}
59+
};
60+
61+
var options = new EvaluationOptions
62+
{
63+
OutputFormat = OutputFormat.List
64+
};
65+
66+
var jsonBaseDoc = new JsonNodeBaseDocument(json, new Uri("http://localhost:1234/doc"));
67+
options.SchemaRegistry.Register(jsonBaseDoc);
1568

16-
var json = new JsonObject
69+
JsonSchema subjectSchema = new JsonSchemaBuilder()
70+
.Ref("http://localhost:1234/doc#/prop2/1");
71+
72+
JsonNode instance = "baz"!;
73+
74+
var result = subjectSchema.Evaluate(instance, options);
75+
76+
result.AssertInvalid();
77+
}
78+
79+
[Test]
80+
public void ReferencesFromWithinEmbeddedSchemas()
81+
{
82+
var json = new JsonObject
83+
{
84+
["prop1"] = "foo",
85+
["prop2"] = new JsonArray
1786
{
18-
["prop1"] = "foo",
19-
["prop2"] = new JsonArray
87+
"bar",
88+
new JsonObject
2089
{
21-
"bar",
22-
JsonSerializer.SerializeToNode(targetSchema)
90+
["type"] = "integer"
2391
}
24-
};
25-
26-
var options = new EvaluationOptions
92+
},
93+
["prop3"] = new JsonObject
2794
{
28-
OutputFormat = OutputFormat.List
29-
};
95+
["$ref"] = "#/prop2/1"
96+
}
97+
};
3098

31-
var jsonBaseDoc = new JsonNodeBaseDocument(json, new Uri("http://localhost:1234/doc"));
32-
options.SchemaRegistry.Register(jsonBaseDoc);
99+
var options = new EvaluationOptions
100+
{
101+
OutputFormat = OutputFormat.List
102+
};
33103

34-
JsonSchema subjectSchema = new JsonSchemaBuilder()
35-
.Ref("http://localhost:1234/doc#/prop2/1");
104+
var jsonBaseDoc = new JsonNodeBaseDocument(json, new Uri("http://localhost:1234/doc"));
105+
options.SchemaRegistry.Register(jsonBaseDoc);
36106

37-
JsonNode instance = 42;
107+
JsonSchema subjectSchema = new JsonSchemaBuilder()
108+
.Ref("http://localhost:1234/doc#/prop3");
38109

39-
var result = subjectSchema.Evaluate(instance, options);
110+
JsonNode instance = 42;
40111

41-
result.AssertValid();
42-
}
112+
var result = subjectSchema.Evaluate(instance, options);
43113

44-
[Test]
45-
public void SchemasEmbeddedInJsonCanBeReferenced_Invalid()
46-
{
47-
JsonSchema targetSchema = new JsonSchemaBuilder()
48-
.Type(SchemaValueType.Integer);
114+
result.IsValid.Should().BeTrue();
115+
}
49116

50-
var json = new JsonObject
117+
[Test]
118+
public void NestedReferencesFromWithinEmbeddedSchemas()
119+
{
120+
var json = new JsonObject
121+
{
122+
["prop1"] = "foo",
123+
["prop2"] = new JsonArray
51124
{
52-
["prop1"] = "foo",
53-
["prop2"] = new JsonArray
125+
"bar",
126+
new JsonObject
54127
{
55-
"bar",
56-
JsonSerializer.SerializeToNode(targetSchema)
128+
["type"] = "integer"
57129
}
58-
};
59-
60-
var options = new EvaluationOptions
130+
},
131+
["prop3"] = new JsonObject
61132
{
62-
OutputFormat = OutputFormat.List
63-
};
133+
["properties"] = new JsonObject
134+
{
135+
["data"] = new JsonObject
136+
{
137+
["$ref"] = "#/prop2/1"
138+
}
139+
140+
}
141+
}
142+
};
143+
144+
var options = new EvaluationOptions
145+
{
146+
OutputFormat = OutputFormat.List
147+
};
64148

65-
var jsonBaseDoc = new JsonNodeBaseDocument(json, new Uri("http://localhost:1234/doc"));
66-
options.SchemaRegistry.Register(jsonBaseDoc);
149+
var jsonBaseDoc = new JsonNodeBaseDocument(json, new Uri("http://localhost:1234/doc"));
150+
options.SchemaRegistry.Register(jsonBaseDoc);
67151

68-
JsonSchema subjectSchema = new JsonSchemaBuilder()
69-
.Ref("http://localhost:1234/doc#/prop2/1");
152+
JsonSchema subjectSchema = new JsonSchemaBuilder()
153+
.Ref("http://localhost:1234/doc#/prop3");
70154

71-
JsonNode instance = "baz"!;
155+
JsonNode instance = new JsonObject { ["data"] = 42 };
72156

73-
var result = subjectSchema.Evaluate(instance, options);
157+
var result = subjectSchema.Evaluate(instance, options);
74158

75-
result.AssertInvalid();
76-
}
159+
result.IsValid.Should().BeTrue();
77160
}
78-
}
161+
}

JsonSchema/JsonNodeBaseDocument.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public JsonNodeBaseDocument(JsonNode node, Uri baseUri)
5252
if (!pointer.TryEvaluate(_node, out var location)) return null;
5353

5454
schema = location.Deserialize<JsonSchema>();
55+
if (schema != null)
56+
JsonSchema.Initialize(schema, options.SchemaRegistry, BaseUri);
57+
5558
_foundSubschemas[pointer] = schema;
5659

5760
return schema;

JsonSchema/JsonSchema.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
<PackageProjectUrl>https://github.com/gregsdennis/json-everything</PackageProjectUrl>
1313
<RepositoryUrl>https://github.com/gregsdennis/json-everything</RepositoryUrl>
1414
<PackageTags>json-schema validation schema json</PackageTags>
15-
<Version>4.1.1</Version>
16-
<FileVersion>4.1.1.0</FileVersion>
15+
<Version>4.1.2</Version>
16+
<FileVersion>4.1.2.0</FileVersion>
1717
<AssemblyVersion>4.0.0.0</AssemblyVersion>
1818
<PackageLicenseFile>LICENSE</PackageLicenseFile>
1919
<AssemblyName>JsonSchema.Net</AssemblyName>

tools/ApiDocsGenerator/release-notes/rn-json-schema.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ title: JsonSchema.Net
44
icon: fas fa-tag
55
order: "8.01"
66
---
7+
# [4.1.2](https://github.com/gregsdennis/json-everything/pull/469) {#release-schema-4.1.2}
8+
9+
[#468](https://github.com/gregsdennis/json-everything/issues/468) - [@Fresa](https://github.com/Fresa) found that `JsonNodeBaseDocument` doesn't properly resolve references in schemas that it contatins and proposed the fix.
10+
711
# [4.1.1](https://github.com/gregsdennis/json-everything/pull/447) {#release-schema-4.1.1}
812

913
[Powershell PR #19610](https://github.com/PowerShell/PowerShell/pull/19610) - Support for \*nix-based file paths caused an error in setting the base URI that resulted in the first folder in the path being interpreted as the URI host.

0 commit comments

Comments
 (0)