-
Notifications
You must be signed in to change notification settings - Fork 331
segregated getDefaultNode in utils #1005
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
9b1658c
68cd4ec
1ea8ae0
e78395d
729ab14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.networknt.schema.utils; | ||
|
||
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; | ||
} | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to |
||
|
||
@Test | ||
void testGetDefaultNodeNotNull() throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test case is exactly the same as the You should test
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added node not found test case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still need the test for There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.