Skip to content

Commit

Permalink
#34 adds support for Collection<JsonNullable<T>> in the JsonNullableV…
Browse files Browse the repository at this point in the history
…alueExtractor (#35)
  • Loading branch information
tofi86 authored Oct 24, 2022
1 parent 74c0a2b commit 8bab929
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javax.validation.valueextraction.ExtractedValue;
import javax.validation.valueextraction.UnwrapByDefault;
import javax.validation.valueextraction.ValueExtractor;
import java.util.Collection;

/**
* Extractor for JsonNullable
Expand All @@ -12,7 +13,16 @@ public class JsonNullableValueExtractor implements ValueExtractor<JsonNullable<@
@Override
public void extractValues(JsonNullable<?> originalValue, ValueReceiver receiver) {
if (originalValue.isPresent()) {
receiver.value(null, originalValue.get());
Object unwrapped = originalValue.get();
if (unwrapped instanceof Collection<?>) {
Collection<?> unwrappedList = (Collection<?>) unwrapped;
Object[] objects = unwrappedList.toArray();
for (int i = 0; i < objects.length; i++) {
receiver.indexedValue("<list element>", i, objects[i]);
}
} else {
receiver.value(null, originalValue.get());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import org.junit.Before;
import org.junit.Test;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.*;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;


public class JsonNullableValueExtractorTest {

Expand Down Expand Up @@ -52,6 +54,27 @@ public void testValidationIsAppliedOnDefinedValue_whenNullValueExtracted() {
assertEquals(1, violations.size());
}

// ensure that JsonNullable<Collection<T>> gets unwrapped and the collection items are validated as well
@Test
public void testUnwrapList() {
Car aCar = new Car();

// test for java.util.List
aCar.addWheel(new Wheel("all"));
aCar.addWheel(new Wheel());
aCar.addWheel(new Wheel("some"));
aCar.addWheel(new Wheel());

// test for java.util.Set
aCar.addPerson(new Person());

Set<ConstraintViolation<Car>> validationResult = validator.validate(aCar);
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")));
}


private static class UnitIssue2 {
@Size(max = 10)
Expand Down Expand Up @@ -80,6 +103,7 @@ public void setNullableRestrictedInt(Integer nullableRestrictedInt) {
}
}


private static class UnitIssue3 {
@NotNull
private JsonNullable<String> notNullString = JsonNullable.undefined();
Expand All @@ -88,4 +112,52 @@ public void setNotNullString(String value) {
notNullString = JsonNullable.of(value);
}
}


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

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

public void addWheel(Wheel wheel) {
if (wheels == null || !wheels.isPresent()) {
wheels = JsonNullable.of(new ArrayList<>());
}
wheels.get().add(wheel);
}

public void addPerson(Person person) {
if (persons == null || !persons.isPresent()) {
persons = JsonNullable.of(new HashSet<>());
}
persons.get().add(person);
}
}

private static class Wheel {
@NotNull
private String screws;

public Wheel() {
}

public Wheel(String screws) {
this.screws = screws;
}
}

private static class Person {
@NotNull
private String role;

public Person() {
}

public Person(String role) {
this.role = role;
}
}

}

0 comments on commit 8bab929

Please sign in to comment.