-
Notifications
You must be signed in to change notification settings - Fork 328
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
base: master
Are you sure you want to change the base?
Changes from all 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,66 @@ | ||
/* | ||
* Copyright (c) 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.networknt.schema.utils; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.networknt.schema.JsonSchema; | ||
import com.networknt.schema.JsonSchemaRef; | ||
|
||
/** | ||
* The 'Defaults' class provides utility methods for retrieving default values | ||
* from a JSON schema. | ||
* | ||
* This class contains a single static method, 'getDefaultNode', which takes a | ||
* 'JsonSchema' object as input | ||
* and returns the default value specified in the schema. If the schema does not | ||
* have a default value, | ||
* it checks if the schema has a reference to another schema and recursively | ||
* calls itself with the referenced schema. | ||
* | ||
* Usage: | ||
* JsonSchema schema = ...; // create or obtain a JSON schema | ||
* JsonNode defaultNode = Defaults.getDefaultNode(schema); // retrieve the | ||
* default value from the schema | ||
* | ||
* Note: This class requires the 'com.networknt.schema.JsonSchema' and | ||
* 'com.networknt.schema.JsonSchemaRef' classes | ||
* from the 'networknt/json-schema-validator' library. | ||
*/ | ||
public class Defaults { | ||
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. Add the javadoc for the class. 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 |
||
/** | ||
* Retrieves the default value specified in the JSON schema. | ||
* | ||
* This method takes a 'JsonSchema' object as input and returns the default | ||
* value specified in the schema. | ||
* If the schema does not have a default value, it checks if the schema has a | ||
* reference to another schema | ||
* and recursively calls itself with the referenced schema. | ||
* | ||
* @param schema the JSON schema from which to retrieve the default value | ||
* @return the default value specified in the schema, or null if no default | ||
* value is found | ||
*/ | ||
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,53 @@ | ||
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.InputStream; | ||
|
||
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.SpecVersion.VersionFlag; | ||
public class DefaultsTest { | ||
|
||
@Test | ||
void testGetDefaultNodeNotNull() 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 = Defaults.getDefaultNode(schema); | ||
assertNotNull(result, "Default node should not be null"); | ||
} | ||
|
||
@Test | ||
void testGetDefaultNodeWhenDefaultNotFound() throws Exception { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
// Create a JsonNode without a "default" field | ||
JsonNode node = mapper.readTree("{\"notDefault\": \"value\"}"); | ||
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V7); | ||
JsonSchema schema = factory.getSchema(node); | ||
JsonNode result = Defaults.getDefaultNode(schema); | ||
// Assert that the result is null, as there's no "default" node in the schema | ||
assertNull(result, "Default node should be null when 'default' node is not found in the schema"); | ||
} | ||
|
||
@Test | ||
void testGetDefaultNodeWhenDefaultInRef() 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. Don't create external resources for the test. Just use strings inline as it makes the test hard to read and verify. This test isn't even valid. You have a unused variable |
||
InputStream mainSchemaInputStream = DefaultsTest.class.getResourceAsStream("/mainSchema.json"); | ||
InputStream referredSchemaInputStream = DefaultsTest.class.getResourceAsStream("/referredSchema.json"); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
JsonNode mainSchemaNode = mapper.readTree(mainSchemaInputStream); | ||
JsonNode referredSchemaNode = mapper.readTree(referredSchemaInputStream); | ||
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V7); | ||
JsonSchema mainSchema = factory.getSchema(referredSchemaNode); // Use referred schema here | ||
JsonNode result = Defaults.getDefaultNode(mainSchema); | ||
assertNotNull(result, "Default node should not be null when 'default' node is in the referred schema"); | ||
assertEquals("defaultValue", result.asText(), "Default node should have the default value in the referred schema"); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"$ref": "referredSchema.json" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"type": "string", | ||
"default": "defaultValue" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just change this to
The 'Defaults' class provides utility methods for retrieving default values from a JSON schema.