Skip to content

Commit 5c94bb7

Browse files
authored
Merge pull request #37 from eskabetxe/issue_26
Issue 26
2 parents 6087924 + 1889e50 commit 5c94bb7

File tree

1 file changed

+50
-22
lines changed

1 file changed

+50
-22
lines changed

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

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,26 @@ public class RefValidator extends BaseJsonValidator implements JsonValidator {
3131
private static final Logger logger = LoggerFactory.getLogger(RefValidator.class);
3232

3333
protected JsonSchema schema;
34+
35+
private final String REF_DOMAIN = "/";
36+
private final String REF_CURRENT = "#";
37+
private final String REF_RELATIVE = "../";
3438

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

3741
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.REF);
3842
String refValue = schemaNode.asText();
39-
if (refValue.startsWith("#")) {
40-
// handle local $ref
41-
if (refValue.equals("#")) {
42-
schema = parentSchema.findAncestor();
43-
} else {
44-
45-
JsonNode node = parentSchema.getRefSchemaNode(refValue);
46-
if (node != null) {
47-
schema = new JsonSchema(mapper, refValue, node, parentSchema);
48-
}
49-
}
50-
} else {
43+
if (!refValue.startsWith(REF_CURRENT)) {
5144
// handle remote ref
52-
int index = refValue.indexOf("#");
53-
String schemaUrl = refValue;
45+
String schemaUrl = refValue;
46+
int index = refValue.indexOf(REF_CURRENT);
5447
if (index > 0) {
5548
schemaUrl = schemaUrl.substring(0, index);
5649
}
50+
if(isRelativePath(schemaUrl)){
51+
schemaUrl = obtainAbsolutePath(parentSchema, schemaUrl);
52+
}
53+
5754
JsonSchemaFactory factory = new JsonSchemaFactory(mapper);
5855
try {
5956
URL url = new URL(schemaUrl);
@@ -66,16 +63,47 @@ public RefValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSch
6663
schema = parentSchema.findAncestor();
6764
} else {
6865
refValue = refValue.substring(index);
69-
if (refValue.equals("#")) {
70-
schema = parentSchema.findAncestor();
71-
} else {
72-
JsonNode node = parentSchema.getRefSchemaNode(refValue);
73-
if (node != null) {
74-
schema = new JsonSchema(mapper, refValue, node, parentSchema);
75-
}
76-
}
7766
}
7867
}
68+
if (refValue.equals(REF_CURRENT)) {
69+
schema = parentSchema.findAncestor();
70+
} else {
71+
JsonNode node = parentSchema.getRefSchemaNode(refValue);
72+
if (node != null) {
73+
schema = new JsonSchema(mapper, refValue, node, parentSchema);
74+
}
75+
}
76+
}
77+
78+
private boolean isRelativePath(String schemaUrl) {
79+
return !schemaUrl.startsWith("http");
80+
}
81+
82+
private String obtainAbsolutePath(JsonSchema parentSchema, String schemaUrl) {
83+
String baseSchemaUrl = parentSchema.findAncestor().getSchemaNode().get("id").textValue();
84+
int index = baseSchemaUrl.lastIndexOf("/");
85+
baseSchemaUrl = baseSchemaUrl.substring(0, index);
86+
87+
String schemaRef = schemaUrl;
88+
89+
if(schemaRef.startsWith(REF_DOMAIN)){
90+
// from domain add ref
91+
try {
92+
URL url = new URL(baseSchemaUrl);
93+
baseSchemaUrl = url.getProtocol()+"//"+url.getHost();
94+
} catch (MalformedURLException e) {
95+
e.printStackTrace();
96+
}
97+
}else if(schemaRef.startsWith(REF_RELATIVE)){
98+
// relative from schema
99+
while(schemaRef.startsWith(REF_RELATIVE)){
100+
index = baseSchemaUrl.lastIndexOf("/");
101+
baseSchemaUrl = baseSchemaUrl.substring(0, index);
102+
schemaRef = schemaRef.replaceFirst(REF_RELATIVE, "");
103+
}
104+
}
105+
schemaRef = baseSchemaUrl +"/"+ schemaRef;
106+
return schemaRef;
79107
}
80108

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

0 commit comments

Comments
 (0)