Skip to content

Issue 26 #37

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 27, 2017
Merged
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
72 changes: 50 additions & 22 deletions src/main/java/com/networknt/schema/RefValidator.java
Original file line number Diff line number Diff line change
@@ -31,29 +31,26 @@ public class RefValidator extends BaseJsonValidator implements JsonValidator {
private static final Logger logger = LoggerFactory.getLogger(RefValidator.class);

protected JsonSchema schema;

private final String REF_DOMAIN = "/";
private final String REF_CURRENT = "#";
private final String REF_RELATIVE = "../";

public RefValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ObjectMapper mapper) {

super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.REF);
String refValue = schemaNode.asText();
if (refValue.startsWith("#")) {
// handle local $ref
if (refValue.equals("#")) {
schema = parentSchema.findAncestor();
} else {

JsonNode node = parentSchema.getRefSchemaNode(refValue);
if (node != null) {
schema = new JsonSchema(mapper, refValue, node, parentSchema);
}
}
} else {
if (!refValue.startsWith(REF_CURRENT)) {
// handle remote ref
int index = refValue.indexOf("#");
String schemaUrl = refValue;
String schemaUrl = refValue;
int index = refValue.indexOf(REF_CURRENT);
if (index > 0) {
schemaUrl = schemaUrl.substring(0, index);
}
if(isRelativePath(schemaUrl)){
schemaUrl = obtainAbsolutePath(parentSchema, schemaUrl);
}

JsonSchemaFactory factory = new JsonSchemaFactory(mapper);
try {
URL url = new URL(schemaUrl);
@@ -66,16 +63,47 @@ public RefValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSch
schema = parentSchema.findAncestor();
} else {
refValue = refValue.substring(index);
if (refValue.equals("#")) {
schema = parentSchema.findAncestor();
} else {
JsonNode node = parentSchema.getRefSchemaNode(refValue);
if (node != null) {
schema = new JsonSchema(mapper, refValue, node, parentSchema);
}
}
}
}
if (refValue.equals(REF_CURRENT)) {
schema = parentSchema.findAncestor();
} else {
JsonNode node = parentSchema.getRefSchemaNode(refValue);
if (node != null) {
schema = new JsonSchema(mapper, refValue, node, parentSchema);
}
}
}

private boolean isRelativePath(String schemaUrl) {
return !schemaUrl.startsWith("http");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about relative local paths? With this change I can't load local schemas anymore using $ref.

}

private String obtainAbsolutePath(JsonSchema parentSchema, String schemaUrl) {
String baseSchemaUrl = parentSchema.findAncestor().getSchemaNode().get("id").textValue();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does this "id" property come from and why is it treated as a URL?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found https://spacetelescope.github.io/understanding-json-schema/structuring.html?highlight=dependencies#the-id-property. But it should be optional, I'd say. I still want to use $ref on local relative files.

int index = baseSchemaUrl.lastIndexOf("/");
baseSchemaUrl = baseSchemaUrl.substring(0, index);

String schemaRef = schemaUrl;

if(schemaRef.startsWith(REF_DOMAIN)){
// from domain add ref
try {
URL url = new URL(baseSchemaUrl);
baseSchemaUrl = url.getProtocol()+"//"+url.getHost();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}else if(schemaRef.startsWith(REF_RELATIVE)){
// relative from schema
while(schemaRef.startsWith(REF_RELATIVE)){
index = baseSchemaUrl.lastIndexOf("/");
baseSchemaUrl = baseSchemaUrl.substring(0, index);
schemaRef = schemaRef.replaceFirst(REF_RELATIVE, "");
}
}
schemaRef = baseSchemaUrl +"/"+ schemaRef;
return schemaRef;
}

public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {