From 3f9974f679f74babc1ffc3b1f22911e473658952 Mon Sep 17 00:00:00 2001 From: Dennis Melzer Date: Mon, 20 Feb 2023 18:32:49 +0100 Subject: [PATCH] Fix broken unwrap bean validation and adapt tests (#50) --- .../JsonNullableJakartaValueExtractor.java | 2 +- .../nullable/JsonNullableValueExtractor.java | 2 +- .../JsonNullableValueExtractorHelper.java | 20 ++----------------- .../JsonNullableValueExtractorTest.java | 11 +++++----- 4 files changed, 9 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/openapitools/jackson/nullable/JsonNullableJakartaValueExtractor.java b/src/main/java/org/openapitools/jackson/nullable/JsonNullableJakartaValueExtractor.java index 64ad9f5..90f17a0 100644 --- a/src/main/java/org/openapitools/jackson/nullable/JsonNullableJakartaValueExtractor.java +++ b/src/main/java/org/openapitools/jackson/nullable/JsonNullableJakartaValueExtractor.java @@ -11,6 +11,6 @@ public class JsonNullableJakartaValueExtractor implements ValueExtractor> { @Override public void extractValues(JsonNullable originalValue, ValueReceiver receiver) { - JsonNullableValueExtractorHelper.extractValues(originalValue, receiver::indexedValue, receiver::value); + JsonNullableValueExtractorHelper.extractValues(originalValue, receiver::value); } } diff --git a/src/main/java/org/openapitools/jackson/nullable/JsonNullableValueExtractor.java b/src/main/java/org/openapitools/jackson/nullable/JsonNullableValueExtractor.java index de91f8c..9ad2be0 100644 --- a/src/main/java/org/openapitools/jackson/nullable/JsonNullableValueExtractor.java +++ b/src/main/java/org/openapitools/jackson/nullable/JsonNullableValueExtractor.java @@ -11,6 +11,6 @@ public class JsonNullableValueExtractor implements ValueExtractor> { @Override public void extractValues(JsonNullable originalValue, ValueReceiver receiver) { - JsonNullableValueExtractorHelper.extractValues(originalValue, receiver::indexedValue, receiver::value); + JsonNullableValueExtractorHelper.extractValues(originalValue, receiver::value); } } diff --git a/src/main/java/org/openapitools/jackson/nullable/JsonNullableValueExtractorHelper.java b/src/main/java/org/openapitools/jackson/nullable/JsonNullableValueExtractorHelper.java index 8bdc8a1..b8796a8 100644 --- a/src/main/java/org/openapitools/jackson/nullable/JsonNullableValueExtractorHelper.java +++ b/src/main/java/org/openapitools/jackson/nullable/JsonNullableValueExtractorHelper.java @@ -1,28 +1,12 @@ package org.openapitools.jackson.nullable; -import java.util.Collection; - abstract class JsonNullableValueExtractorHelper { - public static void extractValues(JsonNullable originalValue, IndexedValueSetter indexedValueSetter, ValueSetter valueSetter) { + public static void extractValues(JsonNullable originalValue, ValueSetter valueSetter) { if (originalValue.isPresent()) { - Object unwrapped = originalValue.get(); - if (unwrapped instanceof Collection) { - Collection unwrappedList = (Collection) unwrapped; - Object[] objects = unwrappedList.toArray(); - for (int i = 0; i < objects.length; i++) { - indexedValueSetter.apply("", i, objects[i]); - } - } else { - valueSetter.apply(null, originalValue.get()); - } + valueSetter.apply(null, originalValue.get()); } } - @FunctionalInterface - interface IndexedValueSetter { - void apply(String var1, int var2, Object var3); - } - @FunctionalInterface interface ValueSetter { void apply(String var1, Object var2); diff --git a/src/test/java/org/openapitools/jackson/nullable/JsonNullableValueExtractorTest.java b/src/test/java/org/openapitools/jackson/nullable/JsonNullableValueExtractorTest.java index 5f029d0..da82b16 100644 --- a/src/test/java/org/openapitools/jackson/nullable/JsonNullableValueExtractorTest.java +++ b/src/test/java/org/openapitools/jackson/nullable/JsonNullableValueExtractorTest.java @@ -57,10 +57,9 @@ public void testValidationIsAppliedOnDefinedValue_whenNullValueExtracted() { Set> violations = validator.validate(unitIssue); assertEquals(1, violations.size()); } - - // ensure that JsonNullable> gets unwrapped and the collection items are validated as well + @Test - public void testUnwrapList() { + public void testCollection() { Car aCar = new Car(); // test for java.util.List @@ -76,7 +75,7 @@ public void testUnwrapList() { assertEquals(3, validationResult.size()); assertTrue(validationResult.stream().anyMatch(c -> c.getPropertyPath().toString().equals("wheels[1].screws") && c.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName().equals("NotNull"))); assertTrue(validationResult.stream().anyMatch(c -> c.getPropertyPath().toString().equals("wheels[3].screws") && c.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName().equals("NotNull"))); - assertTrue(validationResult.stream().anyMatch(c -> c.getPropertyPath().toString().equals("persons[0].role") && c.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName().equals("NotNull"))); + assertTrue(validationResult.stream().anyMatch(c -> c.getPropertyPath().toString().equals("persons[].role") && c.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName().equals("NotNull"))); } @@ -120,10 +119,10 @@ public void setNotNullString(String value) { private static class Car { @Valid - private JsonNullable> wheels = JsonNullable.undefined(); + private JsonNullable> wheels = JsonNullable.undefined(); @Valid - private JsonNullable> persons = JsonNullable.undefined(); + private JsonNullable> persons = JsonNullable.undefined(); public void addWheel(Wheel wheel) { if (wheels == null || !wheels.isPresent()) {