Skip to content

Commit d2143be

Browse files
committed
Optimise AdditionalPropertiesValidator
1 parent 805632d commit d2143be

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

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

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public AdditionalPropertiesValidator(SchemaLocation schemaLocation, JsonNode sch
7474
patternProperties.add(RegularExpression.compile(it.next(), schemaContext));
7575
}
7676
} else {
77-
this.patternProperties = Collections.emptyList();
77+
this.patternProperties = null;
7878
}
7979
}
8080

@@ -91,29 +91,11 @@ protected void validate(ExecutionContext executionContext, JsonNode node, JsonNo
9191
return;
9292
}
9393

94-
Set<String> matchedInstancePropertyNames = null;
95-
96-
boolean collectAnnotations = hasUnevaluatedPropertiesInEvaluationPath(executionContext)
97-
|| collectAnnotations(executionContext);
98-
// if allowAdditionalProperties is true, add all the properties as evaluated.
99-
if (allowAdditionalProperties && collectAnnotations) {
100-
for (Iterator<String> it = node.fieldNames(); it.hasNext();) {
101-
if (matchedInstancePropertyNames == null) {
102-
matchedInstancePropertyNames = new LinkedHashSet<>();
103-
}
104-
String fieldName = it.next();
105-
matchedInstancePropertyNames.add(fieldName);
106-
}
107-
}
108-
10994
for (Iterator<Entry<String, JsonNode>> it = node.fields(); it.hasNext(); ) {
11095
Entry<String, JsonNode> entry = it.next();
11196
String pname = entry.getKey();
112-
// skip the context items
113-
if (pname.startsWith("#")) {
114-
continue;
115-
}
116-
if (!allowedProperties.contains(pname) && !handledByPatternProperties(pname)) {
97+
if (!allowedProperties.contains(pname)
98+
&& (this.patternProperties == null || !handledByPatternProperties(pname))) {
11799
if (!allowAdditionalProperties) {
118100
executionContext.addError(error().instanceNode(node).property(pname)
119101
.instanceLocation(instanceLocation)
@@ -133,7 +115,20 @@ protected void validate(ExecutionContext executionContext, JsonNode node, JsonNo
133115
}
134116
}
135117
}
118+
boolean collectAnnotations = hasUnevaluatedPropertiesInEvaluationPath(executionContext)
119+
|| collectAnnotations(executionContext);
136120
if (collectAnnotations) {
121+
Set<String> matchedInstancePropertyNames = null;
122+
// if allowAdditionalProperties is true, add all the properties as evaluated.
123+
if (allowAdditionalProperties) {
124+
for (Iterator<String> it = node.fieldNames(); it.hasNext();) {
125+
if (matchedInstancePropertyNames == null) {
126+
matchedInstancePropertyNames = new LinkedHashSet<>();
127+
}
128+
String fieldName = it.next();
129+
matchedInstancePropertyNames.add(fieldName);
130+
}
131+
}
137132
executionContext.getAnnotations().put(Annotation.builder().instanceLocation(instanceLocation)
138133
.evaluationPath(executionContext.getEvaluationPath()).schemaLocation(this.schemaLocation).keyword(getKeyword())
139134
.value(matchedInstancePropertyNames != null ? matchedInstancePropertyNames : Collections.emptySet())
@@ -156,11 +151,8 @@ public void walk(ExecutionContext executionContext, JsonNode node, JsonNode root
156151
// Else continue walking.
157152
for (Iterator<String> it = node.fieldNames(); it.hasNext(); ) {
158153
String pname = it.next();
159-
// skip the context items
160-
if (pname.startsWith("#")) {
161-
continue;
162-
}
163-
if (!allowedProperties.contains(pname) && !handledByPatternProperties(pname)) {
154+
if (!allowedProperties.contains(pname)
155+
&& (this.patternProperties == null || !handledByPatternProperties(pname))) {
164156
if (allowAdditionalProperties) {
165157
if (additionalPropertiesSchema != null) {
166158
additionalPropertiesSchema.walk(executionContext, node.get(pname), rootNode,

0 commit comments

Comments
 (0)