Skip to content

Recursive load fix #36

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

Merged
merged 3 commits into from
Jun 17, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/com/networknt/schema/BaseJsonValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema pare
this.subSchema = obainSubSchemaNode(schemaNode);
}

public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
ValidatorTypeCode validatorType, JsonSchema subSchema) {
this.schemaPath = schemaPath;
this.schemaNode = schemaNode;
this.parentSchema = parentSchema;
this.validatorType = validatorType;
this.subSchema = subSchema;
}

protected String getSchemaPath() {
return schemaPath;
}
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/com/networknt/schema/JsonSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,23 @@ public class JsonSchema extends BaseJsonValidator {
JsonSchema(ObjectMapper mapper, String schemaPath, JsonNode schemaNode,
JsonSchema parent) {
super(schemaPath, schemaNode, parent, null);
this.mapper = mapper;
this.init(mapper, schemaNode);
}

validators = new LinkedHashMap<String, JsonValidator>();
JsonSchema(ObjectMapper mapper, String schemaPath, JsonNode schemaNode,
JsonSchema parent, JsonSchema subSchema) {
super(schemaPath, schemaNode, parent, null, subSchema);
this.init(mapper, schemaNode);
}

read(schemaNode);
public JsonSchema(ObjectMapper mapper, JsonNode schemaNode, JsonSchema subSchema) {
this(mapper, "#", schemaNode, null, subSchema);
}

private void init(ObjectMapper mapper, JsonNode schemaNode) {
this.mapper = mapper;
this.validators = new LinkedHashMap<String, JsonValidator>();
this.read(schemaNode);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/networknt/schema/JsonSchemaFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
import java.net.URL;

public class JsonSchemaFactory {

// Draft 6 uses "$id"
private static final String DRAFT_4_ID = "id";

private static final Logger logger = LoggerFactory
.getLogger(JsonSchemaFactory.class);
private ObjectMapper mapper;
Expand Down Expand Up @@ -60,8 +64,15 @@ public JsonSchema getSchema(InputStream schemaStream) {

public JsonSchema getSchema(URL schemaURL) {
try {

JsonNode schemaNode = mapper.readTree(schemaURL.openStream());

if (this.idMatchesSourceUrl(schemaNode, schemaURL)) {
return new JsonSchema(mapper, schemaNode, null);
}

return new JsonSchema(mapper, schemaNode);

} catch (IOException ioe) {
logger.error("Failed to load json schema!", ioe);
throw new JsonSchemaException(ioe);
Expand All @@ -72,4 +83,18 @@ public JsonSchema getSchema(JsonNode jsonNode) {
return new JsonSchema(mapper, jsonNode);
}

private boolean idMatchesSourceUrl(JsonNode schema, URL schemaUrl) {

JsonNode idNode = schema.get(DRAFT_4_ID);

if (idNode == null) {
return false;
}

String id = idNode.asText();
logger.info("Matching " + id + " to " + schemaUrl.toString());
return id.equals(schemaUrl.toString());

}

}
9 changes: 9 additions & 0 deletions src/test/java/com/networknt/schema/JsonSchemaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -105,6 +106,14 @@ private void runTestFile(String testCaseFile) throws Exception {
}
}

@Test(/*expected = java.lang.StackOverflowError.class*/)
public void testLoadingWithId() throws Exception {
URL url = new URL("http://localhost:1234/self_ref/selfRef.json");
JsonNode schemaJson = mapper.readTree(url);
JsonSchemaFactory factory = new JsonSchemaFactory();
JsonSchema schema = factory.getSchema(schemaJson);
}

@Test
public void testBignumValidator() throws Exception {
runTestFile("tests/optional/bignum.json");
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/tests/self_ref/selfRef.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": "http://localhost:1234/self_ref/selfRef.json",
"description": "Schema with ID set to its own URL"
}