Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This project does not adhere to [Semantic Versioning](https://semver.org/) and m

### Changed

- Render null enum members as `null` in validation errors and avoid listing null twice for nullable schemas. [#1373](https://github.com/networknt/json-schema-validator/issues/1373)
- Avoid duplicate numeric text-buffer reads when classifying JSON/YAML decimals; pin fast-path boundaries, randomized classification, and Jackson 3 name-matcher cache invalidation with regression tests. [#1368](https://github.com/networknt/json-schema-validator/issues/1368)
- Reduce decimal-preserving tree parser overhead by caching per token and avoiding text and decimal allocations for lossless floats. [#1362](https://github.com/networknt/json-schema-validator/issues/1362)
- `JsonMapperFactory.getInstance()` and `YamlMapperFactory.getInstance()` now return `DecimalNode` values for lossy finite floating-point spellings when reading trees, subject to explicit mapper settings and custom node factories. (#1357)
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/networknt/schema/keyword/EnumValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class EnumValidator extends BaseKeywordValidator implements KeywordValida
private final Set<NumericStringValue> looseNumbers;

static String asString(JsonNode node) {
if (node.isObject() || node.isArray() || node.isString()) {
if (node.isObject() || node.isArray() || node.isString() || node.isNull()) {
// toString for isString is so that there are quotes
return node.toString();
}
Expand All @@ -59,8 +59,10 @@ public EnumValidator(SchemaLocation schemaLocation, JsonNode schemaNode, Schema

sb.append('[');
String separator = "";
boolean hasNull = false;

for (JsonNode n : schemaNode) {
hasNull |= n.isNull();
JsonNode converted = n;
if (n.isNumber()) {
converted = processNumberNode(n);
Expand All @@ -80,7 +82,7 @@ public EnumValidator(SchemaLocation schemaLocation, JsonNode schemaNode, Schema
// Whether null is accepted depends on the ancestors the value is
// reached through, which is only known during validation. This
// just reports null as permitted when the owning schema says so.
if (schemaContext.isNullableKeywordEnabled()
if (!hasNull && schemaContext.isNullableKeywordEnabled()
&& JsonNodeTypes.isNodeNullable(parentSchema.getSchemaNode())) {
separator = ", ";
sb.append(separator);
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/com/networknt/schema/EnumValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.junit.jupiter.api.Test;

import com.networknt.schema.dialect.Dialects;
import com.networknt.schema.serialization.NodeReader;

import tools.jackson.core.json.JsonReadFeature;
Expand Down Expand Up @@ -180,4 +181,31 @@ void nonFinite() {
messages = schema.validate(inputData, InputFormat.JSON);
assertFalse(messages.isEmpty());
}

@Test
void nullEnumMembersRenderAsJsonNull() {
for (String members : List.of("null, \"STR-01\", \"STR-02\"",
"\"STR-01\", null, \"STR-02\"", "\"STR-01\", \"STR-02\", null")) {
Schema schema = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12)
.getSchema("{\"type\":[\"string\",\"null\"],\"enum\":[" + members + "]}");
List<Error> errors = schema.validate("\"INVALID\"", InputFormat.JSON);
assertEquals(1, errors.size());
assertEquals(": does not have a value in the enumeration [" + members + "]", errors.get(0).toString());
assertTrue(schema.validate("null", InputFormat.JSON).isEmpty());
}
}

@Test
void nullableEnumListsNullOnce() {
for (String members : List.of("null, \"STR-01\"", "\"STR-01\"")) {
Schema schema = SchemaRegistry.withDialect(Dialects.getOpenApi30())
.getSchema("{\"type\":\"string\",\"nullable\":true,\"enum\":[" + members + "]}");
List<Error> errors = schema.validate("\"INVALID\"", InputFormat.JSON);
assertEquals(1, errors.size());
String expected = members.startsWith("null") ? members : members + ", null";
assertEquals(": does not have a value in the enumeration [" + expected + "]", errors.get(0).toString());
assertTrue(schema.validate("null", InputFormat.JSON).isEmpty());
}
}

}
Loading