Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

segregated getDefaultNode in utils #1005

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions src/main/java/com/networknt/schema/ItemsValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.networknt.schema.annotation.JsonNodeAnnotation;
import com.networknt.schema.utils.JsonSchemaRefs;
import com.networknt.schema.utils.SetView;

import com.networknt.schema.utils.Default;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -241,7 +240,7 @@ public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode n
ArrayNode arrayNode = (ArrayNode) node;
JsonNode defaultNode = null;
if (this.validationContext.getConfig().getApplyDefaultsStrategy().shouldApplyArrayDefaults()) {
defaultNode = getDefaultNode(this.schema);
defaultNode = Default.getDefaultNode(this.schema);
}
for (int i = 0; i < count; i++) {
JsonNode n = arrayNode.get(i);
Expand All @@ -266,7 +265,7 @@ else if (this.tupleSchema != null) {
JsonNode defaultNode = null;
JsonNode n = arrayNode.get(i);
if (this.validationContext.getConfig().getApplyDefaultsStrategy().shouldApplyArrayDefaults()) {
defaultNode = getDefaultNode(this.tupleSchema.get(i));
defaultNode = Default.getDefaultNode(this.tupleSchema.get(i));
}
if (n != null) {
if (n.isNull() && defaultNode != null) {
Expand All @@ -293,7 +292,7 @@ else if (this.tupleSchema != null) {
JsonNode defaultNode = null;
JsonNode n = arrayNode.get(i);
if (this.validationContext.getConfig().getApplyDefaultsStrategy().shouldApplyArrayDefaults()) {
defaultNode = getDefaultNode(this.additionalSchema);
defaultNode = Default.getDefaultNode(this.additionalSchema);
}
if (n != null) {
if (n.isNull() && defaultNode != null) {
Expand Down Expand Up @@ -326,17 +325,6 @@ else if (this.tupleSchema != null) {
return validationMessages;
}

private static JsonNode getDefaultNode(JsonSchema schema) {
JsonNode result = schema.getSchemaNode().get("default");
if (result == null) {
JsonSchemaRef schemaRef = JsonSchemaRefs.from(schema);
if (schemaRef != null) {
result = getDefaultNode(schemaRef.getSchema());
}
}
return result;
}

private void walkSchema(ExecutionContext executionContext, JsonSchema walkSchema, JsonNode node, JsonNode rootNode,
JsonNodePath instanceLocation, boolean shouldValidateSchema, Set<ValidationMessage> validationMessages, String keyword) {
boolean executeWalk = this.validationContext.getConfig().getItemWalkListenerRunner().runPreWalkListeners(executionContext, keyword,
Expand Down
16 changes: 3 additions & 13 deletions src/main/java/com/networknt/schema/ItemsValidator202012.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.networknt.schema.annotation.JsonNodeAnnotation;
import com.networknt.schema.utils.JsonSchemaRefs;
import com.networknt.schema.utils.Default;
import com.networknt.schema.utils.SetView;

import org.slf4j.Logger;
Expand Down Expand Up @@ -120,7 +120,7 @@ public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode n
JsonNode defaultNode = null;
if (this.validationContext.getConfig().getApplyDefaultsStrategy().shouldApplyArrayDefaults()
&& this.schema != null) {
defaultNode = getDefaultNode(this.schema);
defaultNode = Default.getDefaultNode(this.schema);
}
boolean evaluated = false;
for (int i = this.prefixCount; i < node.size(); ++i) {
Expand Down Expand Up @@ -155,17 +155,7 @@ public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode n
return validationMessages;
}

private static JsonNode getDefaultNode(JsonSchema schema) {
JsonNode result = schema.getSchemaNode().get("default");
if (result == null) {
JsonSchemaRef schemaRef = JsonSchemaRefs.from(schema);
if (schemaRef != null) {
result = getDefaultNode(schemaRef.getSchema());
}
}
return result;
}


private void walkSchema(ExecutionContext executionContext, JsonSchema walkSchema, JsonNode node, JsonNode rootNode,
JsonNodePath instanceLocation, boolean shouldValidateSchema, Set<ValidationMessage> validationMessages) {
//@formatter:off
Expand Down
14 changes: 2 additions & 12 deletions src/main/java/com/networknt/schema/PrefixItemsValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.networknt.schema.annotation.JsonNodeAnnotation;
import com.networknt.schema.utils.JsonSchemaRefs;
import com.networknt.schema.utils.Default;
import com.networknt.schema.utils.SetView;

import org.slf4j.Logger;
Expand Down Expand Up @@ -110,7 +110,7 @@ public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode n
for (int i = 0; i < count; ++i) {
JsonNode n = node.get(i);
if (this.validationContext.getConfig().getApplyDefaultsStrategy().shouldApplyArrayDefaults()) {
JsonNode defaultNode = getDefaultNode(this.tupleSchema.get(i));
JsonNode defaultNode = Default.getDefaultNode(this.tupleSchema.get(i));
if (n != null) {
// Defaults only set if array index is explicitly null
if (n.isNull() && defaultNode != null) {
Expand Down Expand Up @@ -150,16 +150,6 @@ public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode n
return validationMessages;
}

private static JsonNode getDefaultNode(JsonSchema schema) {
JsonNode result = schema.getSchemaNode().get("default");
if (result == null) {
JsonSchemaRef schemaRef = JsonSchemaRefs.from(schema);
if (schemaRef != null) {
result = getDefaultNode(schemaRef.getSchema());
}
}
return result;
}

private void doWalk(ExecutionContext executionContext, Set<ValidationMessage> validationMessages, int i,
JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation, boolean shouldValidateSchema) {
Expand Down
15 changes: 2 additions & 13 deletions src/main/java/com/networknt/schema/PropertiesValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.fasterxml.jackson.databind.node.MissingNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.networknt.schema.annotation.JsonNodeAnnotation;
import com.networknt.schema.utils.JsonSchemaRefs;
import com.networknt.schema.utils.Default;
import com.networknt.schema.utils.SetView;
import com.networknt.schema.walk.WalkListenerRunner;
import org.slf4j.Logger;
Expand Down Expand Up @@ -192,7 +192,7 @@ private void applyPropertyDefaults(ObjectNode node) {
for (Map.Entry<String, JsonSchema> entry : this.schemas.entrySet()) {
JsonNode propertyNode = node.get(entry.getKey());

JsonNode defaultNode = getDefaultNode(entry.getValue());
JsonNode defaultNode = Default.getDefaultNode(entry.getValue());
if (defaultNode == null) {
continue;
}
Expand All @@ -204,17 +204,6 @@ private void applyPropertyDefaults(ObjectNode node) {
}
}

private static JsonNode getDefaultNode(JsonSchema schema) {
JsonNode result = schema.getSchemaNode().get("default");
if (result == null) {
JsonSchemaRef schemaRef = JsonSchemaRefs.from(schema);
if (schemaRef != null) {
result = getDefaultNode(schemaRef.getSchema());
}
}
return result;
}

private void walkSchema(ExecutionContext executionContext, Map.Entry<String, JsonSchema> entry, JsonNode node,
JsonNode rootNode, JsonNodePath instanceLocation, boolean shouldValidateSchema,
SetView<ValidationMessage> validationMessages, WalkListenerRunner propertyWalkListenerRunner) {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/networknt/schema/utils/Default.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.networknt.schema.utils;
justin-tay marked this conversation as resolved.
Show resolved Hide resolved

import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaRef;

public class Default {
justin-tay marked this conversation as resolved.
Show resolved Hide resolved

public static JsonNode getDefaultNode(JsonSchema schema) {
JsonNode result = schema.getSchemaNode().get("default");
if (result == null) {
JsonSchemaRef schemaRef = JsonSchemaRefs.from(schema);
if (schemaRef != null) {
result = getDefaultNode(schemaRef.getSchema());
}
}
return result;
}
}
38 changes: 38 additions & 0 deletions src/test/java/com/networknt/schema/utils/DefaultTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.networknt.schema.utils;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.JsonSchemaRef;
import com.networknt.schema.SpecVersion.VersionFlag;
public class DefaultTest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to DefaultsTest.


@Test
void testGetDefaultNodeNotNull() throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case is exactly the same as the testGetDefaultNodeEquals case you have so you should remove this. If you want you can move the assertNotNull here to that other test case, but I don't think that is needed.

You should test

  • The behavior when the node cannot be found
  • The behavior when there is a $ref

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added node not found test case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need the test for $ref

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree("{\"default\": \"defaultValue\"}");
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V7);
JsonSchema schema = factory.getSchema(node);
JsonNode result = Default.getDefaultNode(schema);
assertNotNull(result, "Default node should not be null");
}

@Test
void testGetDefaultNodeEquals() throws Exception {
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree("{\"default\": \"defaultValue\"}");
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V7);
JsonSchema schema = factory.getSchema(node);
JsonNode result = Default.getDefaultNode(schema);
assertEquals("defaultValue", result.asText(), "Default node should have the default value");
}
}