-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent from throwing an exception when setting default values (#561)
- this happens when an expected object has a different type i.e. a string. - This is prevented by checking the type of the node before attemping to set the defaults
- Loading branch information
Showing
2 changed files
with
32 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/test/java/com/networknt/schema/PropertiesValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.networknt.schema; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Created by josejulio on 25/04/22. | ||
*/ | ||
public class PropertiesValidatorTest extends BaseJsonSchemaValidatorTest { | ||
|
||
@Test | ||
public void testDoesNotThrowWhenApplyingDefaultPropertiesToNonObjects() throws Exception { | ||
Assertions.assertDoesNotThrow(() -> { | ||
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4); | ||
|
||
SchemaValidatorsConfig schemaValidatorsConfig = new SchemaValidatorsConfig(); | ||
schemaValidatorsConfig.setApplyDefaultsStrategy(new ApplyDefaultsStrategy( | ||
true, | ||
true, | ||
true | ||
)); | ||
|
||
JsonSchema schema = factory.getSchema("{\"type\":\"object\",\"properties\":{\"foo\":{\"type\":\"object\", \"properties\": {} },\"i-have-default\":{\"type\":\"string\",\"default\":\"foo\"}}}", schemaValidatorsConfig); | ||
JsonNode node = getJsonNodeFromStringContent("{\"foo\": \"bar\"}"); | ||
ValidationResult result = schema.walk(node, true); | ||
Assertions.assertEquals(result.getValidationMessages().size(), 1); | ||
}); | ||
} | ||
} |