This is an automated email from the ASF dual-hosted git repository.

rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git


The following commit(s) were added to refs/heads/master by this push:
     new c3360bd  JOHNZON-353 enum jsonschema validation nulable fix
c3360bd is described below

commit c3360bd35e98ff951f19c4eb99a5ff2d567a4673
Author: Romain Manni-Bucau <rmannibu...@gmail.com>
AuthorDate: Fri Oct 8 10:53:14 2021 +0200

    JOHNZON-353 enum jsonschema validation nulable fix
---
 .../jsonschema/spi/builtin/BaseValidation.java     |  4 +-
 .../jsonschema/spi/builtin/EnumValidation.java     |  8 +--
 .../jsonschema/JsonSchemaValidatorTest.java        |  2 +-
 .../jsonschema/spi/builtin/EnumValidationTest.java | 60 ++++++++++++++++++++++
 4 files changed, 68 insertions(+), 6 deletions(-)

diff --git 
a/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/spi/builtin/BaseValidation.java
 
b/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/spi/builtin/BaseValidation.java
index bb52fbb..35bc621 100644
--- 
a/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/spi/builtin/BaseValidation.java
+++ 
b/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/spi/builtin/BaseValidation.java
@@ -29,13 +29,13 @@ import javax.json.JsonValue;
 
 import org.apache.johnzon.jsonschema.ValidationResult;
 
-abstract class BaseValidation implements Function<JsonValue, 
Stream<ValidationResult.ValidationError>> {
+public abstract class BaseValidation implements Function<JsonValue, 
Stream<ValidationResult.ValidationError>> {
     protected final String pointer;
     protected final Function<JsonValue, JsonValue> extractor;
     private final JsonValue.ValueType validType;
     private final boolean rootCanBeNull;
 
-    BaseValidation(final String pointer, final Function<JsonValue, JsonValue> 
extractor, final JsonValue.ValueType validType) {
+    public BaseValidation(final String pointer, final Function<JsonValue, 
JsonValue> extractor, final JsonValue.ValueType validType) {
         this.pointer = pointer;
         this.extractor = extractor != null ? extractor : v -> v;
         this.rootCanBeNull = extractor != null;
diff --git 
a/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/spi/builtin/EnumValidation.java
 
b/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/spi/builtin/EnumValidation.java
index 39e6d78..0185841 100644
--- 
a/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/spi/builtin/EnumValidation.java
+++ 
b/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/spi/builtin/EnumValidation.java
@@ -37,15 +37,17 @@ public class EnumValidation implements ValidationExtension {
         return ofNullable(model.getSchema().get("enum"))
                 .filter(it -> it.getValueType() == JsonValue.ValueType.ARRAY)
                 .map(JsonValue::asJsonArray)
-                .map(values -> new Impl(values, model.getValueProvider(), 
model.toPointer()));
+                .map(values -> new Impl(values, model.getValueProvider(), 
model.toPointer(), JsonValue.TRUE.equals(model.getSchema().get("nullable"))));
     }
 
     private static class Impl extends BaseValidation {
         private final Collection<JsonValue> valid;
+        private final boolean nullable;
 
-        private Impl(final Collection<JsonValue> valid, final 
Function<JsonValue, JsonValue> extractor, final String pointer) {
+        private Impl(final Collection<JsonValue> valid, final 
Function<JsonValue, JsonValue> extractor, final String pointer, final boolean 
nullable) {
             super(pointer, extractor, JsonValue.ValueType.OBJECT /* ignored 
*/);
             this.valid = valid;
+            this.nullable = nullable;
         }
 
         @Override
@@ -54,7 +56,7 @@ public class EnumValidation implements ValidationExtension {
                 return Stream.empty();
             }
             final JsonValue value = extractor.apply(root);
-            if (value != null && JsonValue.ValueType.NULL != 
value.getValueType()) {
+            if (nullable && (value == null || JsonValue.ValueType.NULL == 
value.getValueType())) {
                 return Stream.empty();
             }
             if (valid.contains(value)) {
diff --git 
a/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/JsonSchemaValidatorTest.java
 
b/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/JsonSchemaValidatorTest.java
index cefd77b..3331ed3 100644
--- 
a/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/JsonSchemaValidatorTest.java
+++ 
b/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/JsonSchemaValidatorTest.java
@@ -208,7 +208,7 @@ public class JsonSchemaValidatorTest {
         final ValidationResult failure = 
validator.apply(jsonFactory.createObjectBuilder().add("name", 5).build());
         assertFalse(failure.isSuccess());
         final Collection<ValidationResult.ValidationError> errors = 
failure.getErrors();
-        assertEquals(1, errors.size());
+        assertEquals(2, errors.size());
         final ValidationResult.ValidationError error = 
errors.iterator().next();
         assertEquals("/name", error.getField());
         assertEquals("Expected [NULL, STRING] but got NUMBER", 
error.getMessage());
diff --git 
a/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/spi/builtin/EnumValidationTest.java
 
b/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/spi/builtin/EnumValidationTest.java
new file mode 100644
index 0000000..9361483
--- /dev/null
+++ 
b/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/spi/builtin/EnumValidationTest.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.johnzon.jsonschema.spi.builtin;
+
+import org.apache.johnzon.jsonschema.ValidationResult;
+import org.apache.johnzon.jsonschema.spi.ValidationContext;
+import org.junit.Test;
+
+import javax.json.Json;
+import javax.json.JsonValue;
+import java.util.function.Function;
+import java.util.stream.Stream;
+
+import static org.junit.Assert.assertEquals;
+
+public class EnumValidationTest {
+    @Test
+    public void nullIsValidIfNullable() {
+        final Function<JsonValue, Stream<ValidationResult.ValidationError>> 
validator = new EnumValidation()
+                .create(new ValidationContext(
+                        new String[]{"/test"},
+                        Json.createObjectBuilder()
+                                .add("type", "string")
+                                .add("nullable", true)
+                                .add("enum", Json.createArrayBuilder()
+                                        .add("A")
+                                        .add("B"))
+                                .build(),
+                        v -> v.asJsonObject().get("test")))
+                .orElseThrow(IllegalStateException::new);
+        assertEquals(0, validator
+                .apply(Json.createObjectBuilder().build())
+                .count());
+        assertEquals(0, validator
+                .apply(Json.createObjectBuilder().addNull("test").build())
+                .count());
+        assertEquals(0, validator
+                .apply(Json.createObjectBuilder().add("test", "A").build())
+                .count());
+        assertEquals(1, validator
+                .apply(Json.createObjectBuilder().add("test", "C").build())
+                .count());
+    }
+}

Reply via email to