Skip to content

Commit e8f48ba

Browse files
authored
Merge pull request #265 from networknt/issue255
Issue255
2 parents ee30c36 + 8f3d4fe commit e8f48ba

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ public JsonValidator newValidator(ValidationContext validationContext, String sc
345345
return kw.newValidator(schemaPath, schemaNode, parentSchema, validationContext);
346346
} catch (InvocationTargetException e) {
347347
if (e.getTargetException() instanceof JsonSchemaException) {
348+
logger.error("Error:", e);
348349
throw (JsonSchemaException) e.getTargetException();
349350
} else {
350351
logger.warn("Could not load validator " + keyword);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ private URI combineCurrentUriWithIds(URI currentUri, JsonNode schemaNode) {
8282
final String id = validationContext.resolveSchemaId(schemaNode);
8383
if (id == null) {
8484
return currentUri;
85+
} else if (isUriFragmentWithNoContext(currentUri, id)) {
86+
return null;
8587
} else {
8688
try {
8789
return this.validationContext.getURIFactory().create(currentUri, id);
@@ -90,6 +92,10 @@ private URI combineCurrentUriWithIds(URI currentUri, JsonNode schemaNode) {
9092
}
9193
}
9294
}
95+
96+
private boolean isUriFragmentWithNoContext(URI currentUri, String id) {
97+
return id.startsWith("#") && currentUri == null;
98+
}
9399

94100
public URI getCurrentUri()
95101
{
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.networknt.schema;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.io.InputStream;
9+
import java.util.Set;
10+
11+
public class Issue255Test {
12+
protected JsonSchema getJsonSchemaFromStreamContent(InputStream schemaContent) {
13+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
14+
return factory.getSchema(schemaContent);
15+
}
16+
17+
protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception {
18+
ObjectMapper mapper = new ObjectMapper();
19+
JsonNode node = mapper.readTree(content);
20+
return node;
21+
}
22+
23+
@Test
24+
public void shouldFailWhenRequiredPropertiesDoNotExistInReferencedSubSchema() throws Exception {
25+
String schemaPath = "/draft2019-09/issue255.json";
26+
String dataPath = "/data/issue255.json";
27+
InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath);
28+
JsonSchema schema = getJsonSchemaFromStreamContent(schemaInputStream);
29+
InputStream dataInputStream = getClass().getResourceAsStream(dataPath);
30+
JsonNode node = getJsonNodeFromStreamContent(dataInputStream);
31+
Set<ValidationMessage> errors = schema.validate(node);
32+
Assert.assertEquals(2, errors.size());
33+
}
34+
}

src/test/resources/data/issue255.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"cars": [
3+
{
4+
5+
}
6+
]
7+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"properties": {
5+
"cars": {
6+
"type": "array",
7+
"items": {
8+
"$ref": "#car"
9+
}
10+
}
11+
},
12+
"definitions": {
13+
"car": {
14+
"$id": "#car",
15+
"type": "object",
16+
"properties": {
17+
"model": {
18+
"type": "string"
19+
},
20+
"manufacturer": {
21+
"$ref": "#manufacturer"
22+
}
23+
},
24+
"required": ["model", "manufacturer"]
25+
},
26+
"manufacturer": {
27+
"$id": "#manufacturer",
28+
"type": "object",
29+
"properties": {
30+
"name": {
31+
"type": "string"
32+
},
33+
"factories": {
34+
"type": "array",
35+
"items": {
36+
"$ref": "#factory"
37+
}
38+
}
39+
}
40+
},
41+
"factory": {
42+
"$id": "#factory",
43+
"type": "object",
44+
"properties": {
45+
"location": {
46+
"$ref": "#location"
47+
}
48+
}
49+
},
50+
"location": {
51+
"$id": "#location",
52+
"type": "object",
53+
"properties": {
54+
"country": {
55+
"type": "string"
56+
},
57+
"city": {
58+
"type": "string"
59+
},
60+
"employees": {
61+
"type": "array",
62+
"items": {
63+
"$ref": "#employee"
64+
}
65+
}
66+
},
67+
"additionalProperties": false
68+
},
69+
"employee": {
70+
"$id": "#employee",
71+
"type": "object",
72+
"properties": {
73+
"name": {
74+
"type": "string"
75+
},
76+
"surname": {
77+
"type": "string"
78+
}
79+
},
80+
"additionalProperties": true,
81+
"maxProperties": 3
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)