Skip to content

Commit

Permalink
Fix broken unwrap bean validation and adapt tests (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
MelleD committed Feb 20, 2023
1 parent 7812ac5 commit 3f9974f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
public class JsonNullableJakartaValueExtractor implements ValueExtractor<JsonNullable<@ExtractedValue ?>> {
@Override
public void extractValues(JsonNullable<?> originalValue, ValueReceiver receiver) {
JsonNullableValueExtractorHelper.extractValues(originalValue, receiver::indexedValue, receiver::value);
JsonNullableValueExtractorHelper.extractValues(originalValue, receiver::value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
public class JsonNullableValueExtractor implements ValueExtractor<JsonNullable<@ExtractedValue ?>> {
@Override
public void extractValues(JsonNullable<?> originalValue, ValueReceiver receiver) {
JsonNullableValueExtractorHelper.extractValues(originalValue, receiver::indexedValue, receiver::value);
JsonNullableValueExtractorHelper.extractValues(originalValue, receiver::value);
}
}
Original file line number Diff line number Diff line change
@@ -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("<list element>", 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ public void testValidationIsAppliedOnDefinedValue_whenNullValueExtracted() {
Set<ConstraintViolation<UnitIssue3>> violations = validator.validate(unitIssue);
assertEquals(1, violations.size());
}

// ensure that JsonNullable<Collection<T>> 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
Expand All @@ -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")));
}


Expand Down Expand Up @@ -120,10 +119,10 @@ public void setNotNullString(String value) {

private static class Car {
@Valid
private JsonNullable<List<Wheel>> wheels = JsonNullable.undefined();
private JsonNullable<List<@Valid Wheel>> wheels = JsonNullable.undefined();

@Valid
private JsonNullable<Set<Person>> persons = JsonNullable.undefined();
private JsonNullable<Set<@Valid Person>> persons = JsonNullable.undefined();

public void addWheel(Wheel wheel) {
if (wheels == null || !wheels.isPresent()) {
Expand Down

0 comments on commit 3f9974f

Please sign in to comment.