Skip to content

Commit f34e3b9

Browse files
authored
Merge pull request #36 from thekensta/recursive-load-fix
Recursive load fix
2 parents 6245eac + cd245ba commit f34e3b9

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema pare
4141
this.subSchema = obainSubSchemaNode(schemaNode);
4242
}
4343

44+
public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
45+
ValidatorTypeCode validatorType, JsonSchema subSchema) {
46+
this.schemaPath = schemaPath;
47+
this.schemaNode = schemaNode;
48+
this.parentSchema = parentSchema;
49+
this.validatorType = validatorType;
50+
this.subSchema = subSchema;
51+
}
52+
4453
protected String getSchemaPath() {
4554
return schemaPath;
4655
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,23 @@ public class JsonSchema extends BaseJsonValidator {
4747
JsonSchema(ObjectMapper mapper, String schemaPath, JsonNode schemaNode,
4848
JsonSchema parent) {
4949
super(schemaPath, schemaNode, parent, null);
50-
this.mapper = mapper;
50+
this.init(mapper, schemaNode);
51+
}
5152

52-
validators = new LinkedHashMap<String, JsonValidator>();
53+
JsonSchema(ObjectMapper mapper, String schemaPath, JsonNode schemaNode,
54+
JsonSchema parent, JsonSchema subSchema) {
55+
super(schemaPath, schemaNode, parent, null, subSchema);
56+
this.init(mapper, schemaNode);
57+
}
5358

54-
read(schemaNode);
59+
public JsonSchema(ObjectMapper mapper, JsonNode schemaNode, JsonSchema subSchema) {
60+
this(mapper, "#", schemaNode, null, subSchema);
61+
}
62+
63+
private void init(ObjectMapper mapper, JsonNode schemaNode) {
64+
this.mapper = mapper;
65+
this.validators = new LinkedHashMap<String, JsonValidator>();
66+
this.read(schemaNode);
5567
}
5668

5769
/**

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
import java.net.URL;
2727

2828
public class JsonSchemaFactory {
29+
30+
// Draft 6 uses "$id"
31+
private static final String DRAFT_4_ID = "id";
32+
2933
private static final Logger logger = LoggerFactory
3034
.getLogger(JsonSchemaFactory.class);
3135
private ObjectMapper mapper;
@@ -60,8 +64,15 @@ public JsonSchema getSchema(InputStream schemaStream) {
6064

6165
public JsonSchema getSchema(URL schemaURL) {
6266
try {
67+
6368
JsonNode schemaNode = mapper.readTree(schemaURL.openStream());
69+
70+
if (this.idMatchesSourceUrl(schemaNode, schemaURL)) {
71+
return new JsonSchema(mapper, schemaNode, null);
72+
}
73+
6474
return new JsonSchema(mapper, schemaNode);
75+
6576
} catch (IOException ioe) {
6677
logger.error("Failed to load json schema!", ioe);
6778
throw new JsonSchemaException(ioe);
@@ -72,4 +83,18 @@ public JsonSchema getSchema(JsonNode jsonNode) {
7283
return new JsonSchema(mapper, jsonNode);
7384
}
7485

86+
private boolean idMatchesSourceUrl(JsonNode schema, URL schemaUrl) {
87+
88+
JsonNode idNode = schema.get(DRAFT_4_ID);
89+
90+
if (idNode == null) {
91+
return false;
92+
}
93+
94+
String id = idNode.asText();
95+
logger.info("Matching " + id + " to " + schemaUrl.toString());
96+
return id.equals(schemaUrl.toString());
97+
98+
}
99+
75100
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import java.io.File;
3232
import java.io.InputStream;
33+
import java.net.URL;
3334
import java.util.ArrayList;
3435
import java.util.List;
3536

@@ -105,6 +106,14 @@ private void runTestFile(String testCaseFile) throws Exception {
105106
}
106107
}
107108

109+
@Test(/*expected = java.lang.StackOverflowError.class*/)
110+
public void testLoadingWithId() throws Exception {
111+
URL url = new URL("http://localhost:1234/self_ref/selfRef.json");
112+
JsonNode schemaJson = mapper.readTree(url);
113+
JsonSchemaFactory factory = new JsonSchemaFactory();
114+
JsonSchema schema = factory.getSchema(schemaJson);
115+
}
116+
108117
@Test
109118
public void testBignumValidator() throws Exception {
110119
runTestFile("tests/optional/bignum.json");
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"id": "http://localhost:1234/self_ref/selfRef.json",
3+
"description": "Schema with ID set to its own URL"
4+
}

0 commit comments

Comments
 (0)