Skip to content

Commit ef7f5fd

Browse files
committed
Skip processing of properties keyword if not an object
1 parent cbc4caa commit ef7f5fd

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,17 @@ public PropertiesValidator(SchemaLocation schemaLocation, JsonNode schemaNode, S
5757
@Override
5858
public void validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
5959
NodePath instanceLocation) {
60+
if (!node.isObject()) {
61+
// ignore if not object
62+
return;
63+
}
6064
validate(executionContext, node, rootNode, instanceLocation, false);
6165
}
6266

6367
protected void validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
6468
NodePath instanceLocation, boolean walk) {
6569
Set<String> matchedInstancePropertyNames = null;
66-
boolean collectAnnotations = hasUnevaluatedPropertiesInEvaluationPath(executionContext) || collectAnnotations(executionContext);
70+
boolean collectAnnotations = hasUnevaluatedPropertiesInEvaluationPath(executionContext) || collectAnnotations(executionContext);
6771
for (Entry<String, Schema> entry : this.schemas.entrySet()) {
6872
JsonNode propertyNode = node.get(entry.getKey());
6973
if (propertyNode != null) {

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.networknt.schema.BaseJsonSchemaValidatorTest;
55
import com.networknt.schema.Error;
66
import com.networknt.schema.InputFormat;
7+
import com.networknt.schema.OutputFormat;
78
import com.networknt.schema.Result;
89
import com.networknt.schema.Schema;
910
import com.networknt.schema.SchemaRegistry;
@@ -120,4 +121,26 @@ public void onWalkEnd(WalkEvent walkEvent, List<Error> errors) {
120121
assertEquals("#/additionalProperties/type", errors.get(1).getSchemaLocation().toString());
121122
assertEquals("type", errors.get(1).getKeyword());
122123
}
124+
125+
@Test
126+
void shouldNotProcessIfNotObject() {
127+
SchemaRegistry schemaRegistry = SchemaRegistry.withDialect(Dialects.getDraft202012());
128+
String schemaData = "{\n"
129+
+ " \"properties\": {\n"
130+
+ " \"productId\": {\n"
131+
+ " \"type\": \"integer\",\n"
132+
+ " \"minimum\": 1\n"
133+
+ " }\n"
134+
+ " },\n"
135+
+ " \"additionalProperties\": {\n"
136+
+ " \"type\": \"integer\"\n"
137+
+ " },\n"
138+
+ " \"unevaluatedProperties\": false\n"
139+
+ "}";
140+
String instanceData = "\"hello\"";
141+
Schema schema = schemaRegistry.getSchema(schemaData, InputFormat.JSON);
142+
Result result = schema.validate(instanceData, InputFormat.JSON, OutputFormat.RESULT);
143+
assertEquals(true, result.getErrors().isEmpty());
144+
assertEquals(0, result.getExecutionContext().getAnnotations().asMap().size());
145+
}
123146
}

0 commit comments

Comments
 (0)