Skip to content

Commit 9c1ef88

Browse files
authored
Merge pull request #247 from martin-sladecek/fix/schema_from_id
Resolve schema id from the schema document (for v6 and above)
2 parents 3b50c53 + eaddcd9 commit 9c1ef88

File tree

11 files changed

+136
-115
lines changed

11 files changed

+136
-115
lines changed

src/main/java/com/networknt/schema/JsonSchema.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,18 @@ private JsonSchema(ValidationContext validationContext, String schemaPath, URI
7575
this.currentUri = this.combineCurrentUriWithIds(currentUri, schemaNode);
7676
this.validators = Collections.unmodifiableMap(this.read(schemaNode));
7777
}
78-
78+
7979
private URI combineCurrentUriWithIds(URI currentUri, JsonNode schemaNode) {
80-
final JsonNode idNode = schemaNode.get("id");
81-
if (idNode == null) {
82-
return currentUri;
83-
} else {
84-
try {
85-
return this.validationContext.getURIFactory().create(currentUri, idNode.asText());
86-
} catch (IllegalArgumentException e) {
87-
throw new JsonSchemaException(ValidationMessage.of(ValidatorTypeCode.ID.getValue(), ValidatorTypeCode.ID, idNode.asText(), currentUri.toString()));
80+
final String id = validationContext.resolveSchemaId(schemaNode);
81+
if (id == null) {
82+
return currentUri;
83+
} else {
84+
try {
85+
return this.validationContext.getURIFactory().create(currentUri, id);
86+
} catch (IllegalArgumentException e) {
87+
throw new JsonSchemaException(ValidationMessage.of(ValidatorTypeCode.ID.getValue(), ValidatorTypeCode.ID, id, currentUri.toString()));
88+
}
8889
}
89-
}
9090
}
9191

9292
public URI getCurrentUri()

src/main/java/com/networknt/schema/ValidationContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public JsonValidator newValidator(String schemaPath, String keyword /* keyword *
5050
return metaSchema.newValidator(this, schemaPath, keyword, schemaNode, parentSchema);
5151
}
5252

53+
public String resolveSchemaId(JsonNode schemaNode) {
54+
return metaSchema.readId(schemaNode);
55+
}
56+
5357
public URIFactory getURIFactory() {
5458
return this.uriFactory;
5559
}

src/test/java/com/networknt/schema/V201909JsonSchemaTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@
1111
import org.junit.Test;
1212

1313
import java.io.File;
14-
import java.io.IOException;
1514
import java.io.InputStream;
1615
import java.net.URI;
17-
import java.net.URL;
1816
import java.util.ArrayList;
1917
import java.util.List;
20-
import java.util.Set;
2118

2219
import static io.undertow.Handlers.resource;
23-
import static org.junit.Assert.*;
24-
import static org.junit.Assert.fail;
20+
import static org.junit.Assert.assertEquals;
2521

2622
public class V201909JsonSchemaTest {
2723
protected ObjectMapper mapper = new ObjectMapper();
@@ -400,11 +396,16 @@ public void testRefValidator() throws Exception {
400396
}
401397

402398
@Test
403-
@Ignore
404399
public void testRefRemoteValidator() throws Exception {
405400
runTestFile("draft2019-09/refRemote.json");
406401
}
407402

403+
@Test
404+
@Ignore
405+
public void testRefRemoteValidator_Ignored() throws Exception {
406+
runTestFile("draft2019-09/refRemote_ignored.json");
407+
}
408+
408409
@Test
409410
public void testRequiredValidator() throws Exception {
410411
runTestFile("draft2019-09/required.json");

src/test/java/com/networknt/schema/V6JsonSchemaTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,16 @@ public void testRefValidator() throws Exception {
288288
}
289289

290290
@Test
291-
@Ignore
292291
public void testRefRemoteValidator() throws Exception {
293292
runTestFile("draft6/refRemote.json");
294293
}
295294

295+
@Test
296+
@Ignore
297+
public void testRefRemoteValidator_Ignored() throws Exception {
298+
runTestFile("draft6/refRemote_ignored.json");
299+
}
300+
296301
@Test
297302
public void testRequiredValidator() throws Exception {
298303
runTestFile("draft6/required.json");

src/test/java/com/networknt/schema/V7JsonSchemaTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,16 @@ public void testRefValidator() throws Exception {
389389
}
390390

391391
@Test
392-
@Ignore
393392
public void testRefRemoteValidator() throws Exception {
394393
runTestFile("draft7/refRemote.json");
395394
}
396395

396+
@Test
397+
@Ignore
398+
public void testRefRemoteValidator_Ignored() throws Exception {
399+
runTestFile("draft7/refRemote_ignored.json");
400+
}
401+
397402
@Test
398403
public void testRequiredValidator() throws Exception {
399404
runTestFile("draft7/required.json");

src/test/resources/draft2019-09/refRemote.json

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -98,37 +98,6 @@
9898
}
9999
]
100100
},
101-
{
102-
"description": "base URI change - change folder in subschema",
103-
"schema": {
104-
"$id": "http://localhost:1234/scope_change_defs2.json",
105-
"type" : "object",
106-
"properties": {"list": {"$ref": "#/$defs/baz/$defs/bar"}},
107-
"$defs": {
108-
"baz": {
109-
"$id": "folder/",
110-
"$defs": {
111-
"bar": {
112-
"type": "array",
113-
"items": {"$ref": "folderInteger.json"}
114-
}
115-
}
116-
}
117-
}
118-
},
119-
"tests": [
120-
{
121-
"description": "number is valid",
122-
"data": {"list": [1]},
123-
"valid": true
124-
},
125-
{
126-
"description": "string is invalid",
127-
"data": {"list": ["a"]},
128-
"valid": false
129-
}
130-
]
131-
},
132101
{
133102
"description": "root ref in remote ref",
134103
"schema": {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[
2+
{
3+
"description": "base URI change - change folder in subschema",
4+
"schema": {
5+
"$id": "http://localhost:1234/scope_change_defs2.json",
6+
"type" : "object",
7+
"properties": {"list": {"$ref": "#/$defs/baz/$defs/bar"}},
8+
"$defs": {
9+
"baz": {
10+
"$id": "folder/",
11+
"$defs": {
12+
"bar": {
13+
"type": "array",
14+
"items": {"$ref": "folderInteger.json"}
15+
}
16+
}
17+
}
18+
}
19+
},
20+
"tests": [
21+
{
22+
"description": "number is valid",
23+
"data": {"list": [1]},
24+
"valid": true
25+
},
26+
{
27+
"description": "string is invalid",
28+
"data": {"list": ["a"]},
29+
"valid": false
30+
}
31+
]
32+
},
33+
]

src/test/resources/draft6/refRemote.json

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -100,39 +100,6 @@
100100
}
101101
]
102102
},
103-
{
104-
"description": "base URI change - change folder in subschema",
105-
"schema": {
106-
"$id": "http://localhost:1234/scope_change_defs2.json",
107-
"type" : "object",
108-
"properties": {
109-
"list": {"$ref": "#/definitions/baz/definitions/bar"}
110-
},
111-
"definitions": {
112-
"baz": {
113-
"$id": "folder/",
114-
"definitions": {
115-
"bar": {
116-
"type": "array",
117-
"items": {"$ref": "folderInteger.json"}
118-
}
119-
}
120-
}
121-
}
122-
},
123-
"tests": [
124-
{
125-
"description": "number is valid",
126-
"data": {"list": [1]},
127-
"valid": true
128-
},
129-
{
130-
"description": "string is invalid",
131-
"data": {"list": ["a"]},
132-
"valid": false
133-
}
134-
]
135-
},
136103
{
137104
"description": "root ref in remote ref",
138105
"schema": {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[
2+
{
3+
"description": "base URI change - change folder in subschema",
4+
"schema": {
5+
"$id": "http://localhost:1234/scope_change_defs2.json",
6+
"type" : "object",
7+
"properties": {
8+
"list": {"$ref": "#/definitions/baz/definitions/bar"}
9+
},
10+
"definitions": {
11+
"baz": {
12+
"$id": "folder/",
13+
"definitions": {
14+
"bar": {
15+
"type": "array",
16+
"items": {"$ref": "folderInteger.json"}
17+
}
18+
}
19+
}
20+
}
21+
},
22+
"tests": [
23+
{
24+
"description": "number is valid",
25+
"data": {"list": [1]},
26+
"valid": true
27+
},
28+
{
29+
"description": "string is invalid",
30+
"data": {"list": ["a"]},
31+
"valid": false
32+
}
33+
]
34+
}
35+
]

src/test/resources/draft7/refRemote.json

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -100,39 +100,6 @@
100100
}
101101
]
102102
},
103-
{
104-
"description": "base URI change - change folder in subschema",
105-
"schema": {
106-
"$id": "http://localhost:1234/scope_change_defs2.json",
107-
"type" : "object",
108-
"properties": {
109-
"list": {"$ref": "#/definitions/baz/definitions/bar"}
110-
},
111-
"definitions": {
112-
"baz": {
113-
"$id": "folder/",
114-
"definitions": {
115-
"bar": {
116-
"type": "array",
117-
"items": {"$ref": "folderInteger.json"}
118-
}
119-
}
120-
}
121-
}
122-
},
123-
"tests": [
124-
{
125-
"description": "number is valid",
126-
"data": {"list": [1]},
127-
"valid": true
128-
},
129-
{
130-
"description": "string is invalid",
131-
"data": {"list": ["a"]},
132-
"valid": false
133-
}
134-
]
135-
},
136103
{
137104
"description": "root ref in remote ref",
138105
"schema": {

0 commit comments

Comments
 (0)