@@ -31,29 +31,26 @@ public class RefValidator extends BaseJsonValidator implements JsonValidator {
31
31
private static final Logger logger = LoggerFactory .getLogger (RefValidator .class );
32
32
33
33
protected JsonSchema schema ;
34
+
35
+ private final String REF_DOMAIN = "/" ;
36
+ private final String REF_CURRENT = "#" ;
37
+ private final String REF_RELATIVE = "../" ;
34
38
35
39
public RefValidator (String schemaPath , JsonNode schemaNode , JsonSchema parentSchema , ObjectMapper mapper ) {
36
40
37
41
super (schemaPath , schemaNode , parentSchema , ValidatorTypeCode .REF );
38
42
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 )) {
51
44
// handle remote ref
52
- int index = refValue . indexOf ( "#" ) ;
53
- String schemaUrl = refValue ;
45
+ String schemaUrl = refValue ;
46
+ int index = refValue . indexOf ( REF_CURRENT ) ;
54
47
if (index > 0 ) {
55
48
schemaUrl = schemaUrl .substring (0 , index );
56
49
}
50
+ if (isRelativePath (schemaUrl )){
51
+ schemaUrl = obtainAbsolutePath (parentSchema , schemaUrl );
52
+ }
53
+
57
54
JsonSchemaFactory factory = new JsonSchemaFactory (mapper );
58
55
try {
59
56
URL url = new URL (schemaUrl );
@@ -66,16 +63,47 @@ public RefValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSch
66
63
schema = parentSchema .findAncestor ();
67
64
} else {
68
65
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
- }
77
66
}
78
67
}
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 ;
79
107
}
80
108
81
109
public Set <ValidationMessage > validate (JsonNode node , JsonNode rootNode , String at ) {
0 commit comments