38
38
public class JsonSchema extends BaseJsonValidator {
39
39
private static final Pattern intPattern = Pattern .compile ("^[0-9]+$" );
40
40
protected final Map <String , JsonValidator > validators ;
41
+ private final String idKeyword ;
41
42
private final ValidationContext validationContext ;
42
43
43
44
/**
@@ -72,6 +73,7 @@ private JsonSchema(ValidationContext validationContext, String schemaPath, URI
72
73
validationContext .getConfig () != null && validationContext .getConfig ().isFailFast ());
73
74
this .validationContext = validationContext ;
74
75
this .config = validationContext .getConfig ();
76
+ this .idKeyword = validationContext .getMetaSchema ().getIdKeyword ();
75
77
this .currentUri = this .combineCurrentUriWithIds (currentUri , schemaNode );
76
78
this .validators = Collections .unmodifiableMap (this .read (schemaNode ));
77
79
}
@@ -119,16 +121,19 @@ public JsonNode getRefSchemaNode(String ref) {
119
121
node = node .get (key );
120
122
}
121
123
if (node == null ){
122
- JsonSchema subSchema = schema .fetchSubSchemaNode (validationContext );
123
- if (subSchema != null ) {
124
- node = subSchema .getRefSchemaNode (ref );
125
- }
124
+ node = handleNullNode (ref , schema );
126
125
}
127
126
if (node == null ){
128
127
break ;
129
128
}
130
129
}
131
130
}
131
+ else if (ref .startsWith ("#" ) && ref .length () > 1 ) {
132
+ node = getNodeById (ref , node );
133
+ if (node == null ) {
134
+ node = handleNullNode (ref , schema );
135
+ }
136
+ }
132
137
return node ;
133
138
}
134
139
@@ -140,6 +145,37 @@ public JsonSchema findAncestor() {
140
145
return ancestor ;
141
146
}
142
147
148
+ private JsonNode handleNullNode (String ref , JsonSchema schema ) {
149
+ JsonSchema subSchema = schema .fetchSubSchemaNode (validationContext );
150
+ if (subSchema != null ) {
151
+ return subSchema .getRefSchemaNode (ref );
152
+ }
153
+ return null ;
154
+ }
155
+
156
+ private JsonNode getNodeById (String ref , JsonNode node ) {
157
+ if (nodeContainsRef (ref , node )) {
158
+ return node ;
159
+ } else {
160
+ Iterator <JsonNode > children = node .elements ();
161
+ while (children .hasNext ()) {
162
+ JsonNode refNode = getNodeById (ref , children .next ());
163
+ if (refNode != null ) {
164
+ return refNode ;
165
+ }
166
+ }
167
+ }
168
+ return null ;
169
+ }
170
+
171
+ private boolean nodeContainsRef (String ref , JsonNode node ) {
172
+ JsonNode id = node .get (idKeyword );
173
+ if (id != null ) {
174
+ return ref .equals (id .asText ());
175
+ }
176
+ return false ;
177
+ }
178
+
143
179
private Map <String , JsonValidator > read (JsonNode schemaNode ) {
144
180
Map <String , JsonValidator > validators = new HashMap <String , JsonValidator >();
145
181
if (schemaNode .isBoolean ()) {
0 commit comments