Skip to content

Commit 7dce21f

Browse files
committed
EvaluationPath test and fixes
1 parent 6a5ef34 commit 7dce21f

File tree

10 files changed

+331
-97
lines changed

10 files changed

+331
-97
lines changed

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,13 @@ public static NodePath getInstance() {
148148
}
149149

150150
public void validate(ExecutionContext executionContext, JsonNode node) {
151-
/*
152-
* Sets the evaluationPath to point to the schema location fragment if it isn't
153-
* pointing to the document
151+
/* Previously the evaluation path started with the fragment of the schema due to the way it was implemented
152+
* as part of the schema's state
153+
* int count = this.schemaLocation.getFragment().getNameCount();
154+
* for (int x = 0; x < count; x++) {
155+
* executionContext.evaluationPath.addLast(this.schemaLocation.getFragment().getElement(x));
156+
* }
154157
*/
155-
int count = this.schemaLocation.getFragment().getNameCount();
156-
for (int x = 0; x < count; x++) {
157-
executionContext.evaluationPath.addLast(this.schemaLocation.getFragment().getElement(x));
158-
}
159158
validate(executionContext, node, node, atRoot());
160159
}
161160

@@ -711,14 +710,14 @@ private List<KeywordValidator> read(JsonNode schemaNode) {
711710

712711
@Override
713712
public void validate(ExecutionContext executionContext, JsonNode jsonNode, JsonNode rootNode, NodePath instanceLocation) {
714-
// String newEvaluationPath = new EvaluationPath(executionContext.getEvaluationPath()).toString();
715-
// String oldEvaluationPath = getEvaluationPath().toString();
716-
// if (!oldEvaluationPath.equals(newEvaluationPath)) {
717-
// System.out.println("-----------------");
718-
// System.out.println("MISMATCH OLD: " + oldEvaluationPath);
719-
// System.out.println("MISMATCH NEW: " + newEvaluationPath);
720-
// System.out.println("-----------------");
721-
// }
713+
String newEvaluationPath = new EvaluationPath(executionContext.getEvaluationPath()).toString();
714+
String oldEvaluationPath = getEvaluationPath().toString();
715+
if (!oldEvaluationPath.equals(newEvaluationPath)) {
716+
System.out.println("-----------------");
717+
System.out.println("MISMATCH OLD: " + oldEvaluationPath);
718+
System.out.println("MISMATCH NEW: " + newEvaluationPath);
719+
System.out.println("-----------------");
720+
}
722721
executionContext.evaluationSchema.addLast(this);
723722
try {
724723
int currentErrors = executionContext.getErrors().size();

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

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.networknt.schema.dialect.DialectId;
2424
import com.networknt.schema.dialect.DialectRegistry;
2525
import com.networknt.schema.path.NodePath;
26+
import com.networknt.schema.resource.InputStreamSource;
2627
import com.networknt.schema.resource.ResourceLoaders;
2728
import com.networknt.schema.resource.SchemaIdResolvers;
2829
import com.networknt.schema.resource.SchemaLoader;
@@ -33,6 +34,7 @@
3334
import org.slf4j.Logger;
3435
import org.slf4j.LoggerFactory;
3536

37+
import java.io.FileNotFoundException;
3638
import java.io.IOException;
3739
import java.io.InputStream;
3840
import java.net.URI;
@@ -680,38 +682,43 @@ public Schema loadSchema(final SchemaLocation schemaUri) {
680682
}
681683

682684
protected Schema getMappedSchema(final SchemaLocation schemaUri) {
683-
try (InputStream inputStream = this.schemaLoader.getSchemaResource(schemaUri.getAbsoluteIri())
684-
.getInputStream()) {
685-
if (inputStream == null) {
686-
throw new IOException("Cannot load schema at " + schemaUri);
687-
}
688-
final JsonNode schemaNode;
689-
if (isYaml(schemaUri)) {
690-
schemaNode = readTree(inputStream, InputFormat.YAML);
691-
} else {
692-
schemaNode = readTree(inputStream, InputFormat.JSON);
693-
}
685+
InputStreamSource inputStreamSource = this.schemaLoader.getSchemaResource(schemaUri.getAbsoluteIri());
686+
if (inputStreamSource != null) {
687+
try (InputStream inputStream = inputStreamSource.getInputStream()) {
688+
if (inputStream == null) {
689+
throw new IOException("Cannot load schema at " + schemaUri);
690+
}
691+
final JsonNode schemaNode;
692+
if (isYaml(schemaUri)) {
693+
schemaNode = readTree(inputStream, InputFormat.YAML);
694+
} else {
695+
schemaNode = readTree(inputStream, InputFormat.JSON);
696+
}
694697

695-
final Dialect dialect = getDialectOrDefault(schemaNode);
696-
NodePath evaluationPath = new NodePath(getSchemaRegistryConfig().getPathType());
697-
if (schemaUri.getFragment() == null || schemaUri.getFragment().getNameCount() == 0) {
698-
// Schema without fragment
699-
SchemaContext schemaContext = new SchemaContext(dialect, this);
700-
return doCreate(schemaContext, schemaUri, evaluationPath, schemaNode, null,
701-
true /* retrieved via id, resolving will not change anything */);
702-
} else {
703-
// Schema with fragment pointing to sub schema
704-
final SchemaContext schemaContext = createSchemaContext(schemaNode);
705-
SchemaLocation documentLocation = new SchemaLocation(schemaUri.getAbsoluteIri());
706-
Schema document = doCreate(schemaContext, documentLocation, evaluationPath, schemaNode, null, false);
707-
return document.getRefSchema(schemaUri.getFragment());
698+
final Dialect dialect = getDialectOrDefault(schemaNode);
699+
NodePath evaluationPath = new NodePath(getSchemaRegistryConfig().getPathType());
700+
if (schemaUri.getFragment() == null || schemaUri.getFragment().getNameCount() == 0) {
701+
// Schema without fragment
702+
SchemaContext schemaContext = new SchemaContext(dialect, this);
703+
return doCreate(schemaContext, schemaUri, evaluationPath, schemaNode, null,
704+
true /* retrieved via id, resolving will not change anything */);
705+
} else {
706+
// Schema with fragment pointing to sub schema
707+
final SchemaContext schemaContext = createSchemaContext(schemaNode);
708+
SchemaLocation documentLocation = new SchemaLocation(schemaUri.getAbsoluteIri());
709+
Schema document = doCreate(schemaContext, documentLocation, evaluationPath, schemaNode, null,
710+
false);
711+
return document.getRefSchema(schemaUri.getFragment());
712+
}
713+
} catch (IOException e) {
714+
logger.error("Failed to load json schema from {}", schemaUri.getAbsoluteIri(), e);
715+
SchemaException exception = new SchemaException(
716+
"Failed to load json schema from " + schemaUri.getAbsoluteIri());
717+
exception.initCause(e);
718+
throw exception;
708719
}
709-
} catch (IOException e) {
710-
logger.error("Failed to load json schema from {}", schemaUri.getAbsoluteIri(), e);
711-
SchemaException exception = new SchemaException(
712-
"Failed to load json schema from " + schemaUri.getAbsoluteIri());
713-
exception.initCause(e);
714-
throw exception;
720+
} else {
721+
throw new SchemaException(new FileNotFoundException(schemaUri.getAbsoluteIri().toString()));
715722
}
716723
}
717724

src/main/java/com/networknt/schema/keyword/AnyOfValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ public void walk(ExecutionContext executionContext, JsonNode node, JsonNode root
209209
validate(executionContext, node, rootNode, instanceLocation, true);
210210
return;
211211
}
212+
int schemaIndex = 0;
212213
for (Schema schema : this.schemas) {
213-
int schemaIndex = 0;
214214
executionContext.getEvaluationPath().addLast(schemaIndex);
215215
try {
216216
schema.walk(executionContext, node, rootNode, instanceLocation, false);

src/main/java/com/networknt/schema/keyword/ItemsLegacyValidator.java

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.networknt.schema.SchemaLocation;
2929
import com.networknt.schema.SchemaRef;
3030
import com.networknt.schema.annotation.Annotation;
31+
import com.networknt.schema.path.EvaluationPath;
3132
import com.networknt.schema.path.NodePath;
3233
import com.networknt.schema.utils.SchemaRefs;
3334

@@ -257,10 +258,10 @@ public void walk(ExecutionContext executionContext, JsonNode node, JsonNode root
257258
n = defaultNode;
258259
}
259260
}
260-
walkSchema(executionContext, this.schema, n, rootNode, instanceLocation.append(i), shouldValidateSchema, KeywordType.ITEMS.getValue());
261+
walkSchema(executionContext, this.schema, n, rootNode, instanceLocation.append(i), shouldValidateSchema, KeywordType.ITEMS_LEGACY.getValue());
261262
}
262263
} else {
263-
walkSchema(executionContext, this.schema, null, rootNode, instanceLocation.append(0), shouldValidateSchema, KeywordType.ITEMS.getValue());
264+
walkSchema(executionContext, this.schema, null, rootNode, instanceLocation.append(0), shouldValidateSchema, KeywordType.ITEMS_LEGACY.getValue());
264265
}
265266
}
266267
else if (this.tupleSchema != null) {
@@ -283,15 +284,15 @@ else if (this.tupleSchema != null) {
283284
executionContext.getEvaluationPath().addLast(i);
284285
try {
285286
walkSchema(executionContext, this.tupleSchema.get(i), n, rootNode, instanceLocation.append(i),
286-
shouldValidateSchema, KeywordType.ITEMS.getValue());
287+
shouldValidateSchema, KeywordType.ITEMS_LEGACY.getValue());
287288
} finally {
288289
executionContext.getEvaluationPath().removeLast();
289290
}
290291
} else {
291292
executionContext.getEvaluationPath().addLast(i);
292293
try {
293294
walkSchema(executionContext, this.tupleSchema.get(i), null, rootNode,
294-
instanceLocation.append(i), shouldValidateSchema, KeywordType.ITEMS.getValue());
295+
instanceLocation.append(i), shouldValidateSchema, KeywordType.ITEMS_LEGACY.getValue());
295296
} finally {
296297
executionContext.getEvaluationPath().removeLast();
297298
}
@@ -354,27 +355,35 @@ private static JsonNode getDefaultNode(Schema schema) {
354355

355356
private void walkSchema(ExecutionContext executionContext, Schema walkSchema, JsonNode node, JsonNode rootNode,
356357
NodePath instanceLocation, boolean shouldValidateSchema, String keyword) {
357-
boolean executeWalk = executionContext.getWalkConfig().getItemWalkListenerRunner().runPreWalkListeners(executionContext, keyword,
358-
node, rootNode, instanceLocation, walkSchema, this);
359-
int currentErrors = executionContext.getErrors().size();
360-
if (executeWalk) {
361-
boolean additionalItems = "additionalItems".equals(keyword);
362-
if(additionalItems) {
363-
executionContext.getEvaluationPath().removeLast(); // remove items
364-
executionContext.getEvaluationPath().addLast(keyword);
358+
boolean additionalItems = "additionalItems".equals(keyword);
359+
if (additionalItems) {
360+
executionContext.getEvaluationPath().removeLast(); // remove items
361+
executionContext.getEvaluationPath().addLast(keyword);
362+
}
363+
try {
364+
// TOREMOVE
365+
// MISMATCH TEST
366+
String newPath = new EvaluationPath(executionContext.getEvaluationPath()).toString();
367+
String oldPath = walkSchema.getEvaluationPath().toString();
368+
if (!newPath.equals(oldPath)) {
369+
throw new RuntimeException("mismatch: old " + oldPath + " new " + newPath);
365370
}
366-
try {
371+
372+
boolean executeWalk = executionContext.getWalkConfig().getItemWalkListenerRunner()
373+
.runPreWalkListeners(executionContext, keyword, node, rootNode, instanceLocation, walkSchema, this);
374+
int currentErrors = executionContext.getErrors().size();
375+
if (executeWalk) {
367376
walkSchema.walk(executionContext, node, rootNode, instanceLocation, shouldValidateSchema);
368-
} finally {
369-
if (additionalItems) {
370-
executionContext.getEvaluationPath().removeLast();
371-
executionContext.getEvaluationPath().addLast("items");
372-
}
377+
}
378+
executionContext.getWalkConfig().getItemWalkListenerRunner().runPostWalkListeners(executionContext, keyword,
379+
node, rootNode, instanceLocation, walkSchema, this,
380+
executionContext.getErrors().subList(currentErrors, executionContext.getErrors().size()));
381+
} finally {
382+
if (additionalItems) {
383+
executionContext.getEvaluationPath().removeLast();
384+
executionContext.getEvaluationPath().addLast("items");
373385
}
374386
}
375-
executionContext.getWalkConfig().getItemWalkListenerRunner().runPostWalkListeners(executionContext, keyword, node, rootNode,
376-
instanceLocation, walkSchema, this, executionContext.getErrors().subList(currentErrors, executionContext.getErrors().size()));
377-
378387
}
379388

380389
public List<Schema> getTupleSchema() {

src/main/java/com/networknt/schema/keyword/ItemsValidator.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.networknt.schema.SchemaLocation;
2525
import com.networknt.schema.SchemaContext;
2626
import com.networknt.schema.annotation.Annotation;
27+
import com.networknt.schema.path.EvaluationPath;
2728
import com.networknt.schema.path.NodePath;
2829
import com.networknt.schema.utils.SchemaRefs;
2930

@@ -149,9 +150,17 @@ private static JsonNode getDefaultNode(Schema schema) {
149150
private void walkSchema(ExecutionContext executionContext, Schema walkSchema, JsonNode node, JsonNode rootNode,
150151
NodePath instanceLocation, boolean shouldValidateSchema) {
151152
//@formatter:off
153+
154+
// TOREMOVE
155+
// MISMATCH TEST
156+
String newPath = new EvaluationPath(executionContext.getEvaluationPath()).toString();
157+
String oldPath = walkSchema.getEvaluationPath().toString();
158+
if (!newPath.equals(oldPath)) {
159+
throw new RuntimeException("mismatch: old "+oldPath+" new "+newPath);
160+
}
152161
boolean executeWalk = executionContext.getWalkConfig().getItemWalkListenerRunner().runPreWalkListeners(
153162
executionContext,
154-
KeywordType.ITEMS_LEGACY.getValue(),
163+
KeywordType.ITEMS.getValue(),
155164
node,
156165
rootNode,
157166
instanceLocation,
@@ -163,7 +172,7 @@ private void walkSchema(ExecutionContext executionContext, Schema walkSchema, Js
163172
}
164173
executionContext.getWalkConfig().getItemWalkListenerRunner().runPostWalkListeners(
165174
executionContext,
166-
KeywordType.ITEMS_LEGACY.getValue(),
175+
KeywordType.ITEMS.getValue(),
167176
node,
168177
rootNode,
169178
instanceLocation,

src/main/java/com/networknt/schema/keyword/PropertiesValidator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,12 @@ public void walk(ExecutionContext executionContext, JsonNode node, JsonNode root
129129
} else {
130130
WalkListenerRunner propertyWalkListenerRunner = executionContext.getWalkConfig().getPropertyWalkListenerRunner();
131131
for (Map.Entry<String, Schema> entry : this.schemas.entrySet()) {
132-
walkSchema(executionContext, entry, node, rootNode, instanceLocation, shouldValidateSchema, propertyWalkListenerRunner);
132+
executionContext.getEvaluationPath().addLast(entry.getKey());
133+
try {
134+
walkSchema(executionContext, entry, node, rootNode, instanceLocation, shouldValidateSchema, propertyWalkListenerRunner);
135+
} finally {
136+
executionContext.getEvaluationPath().removeLast();
137+
}
133138
}
134139
}
135140
}

src/test/java/com/networknt/schema/PropertiesValidatorTest.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)