Skip to content

Commit

Permalink
Issue OpenAPITools#12 - No need to validate values for JsonNullable.u…
Browse files Browse the repository at this point in the history
…ndefined()
  • Loading branch information
Виктор Крапивин committed Sep 10, 2019
1 parent 7475497 commit 99e8b46
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
public class JsonNullableValueExtractor implements ValueExtractor<JsonNullable<@ExtractedValue ?>> {
@Override
public void extractValues(JsonNullable<?> originalValue, ValueReceiver receiver) {
receiver.value(null, originalValue.isPresent() ? originalValue.get() : null);
if (originalValue.isPresent()) {
receiver.value(null, originalValue.get());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Set;

Expand Down Expand Up @@ -36,6 +37,22 @@ public void testUnwrap() {
assertEquals(4, validate.size());
}

@Test
public void testValidationIsNotApplied_whenValueIsUndefined() {
UnitIssue3 unitIssue = new UnitIssue3();
Set<ConstraintViolation<UnitIssue3>> violations = validator.validate(unitIssue);
assertEquals(0, violations.size());
}

@Test
public void testValidationIsAppliedOnDefinedValue_whenNullValueExtracted() {
UnitIssue3 unitIssue = new UnitIssue3();
unitIssue.setNotNullString(null);
Set<ConstraintViolation<UnitIssue3>> violations = validator.validate(unitIssue);
assertEquals(1, violations.size());
}


private static class UnitIssue2 {
@Size(max = 10)
public String restrictedString;
Expand All @@ -62,4 +79,13 @@ public void setNullableRestrictedInt(Integer nullableRestrictedInt) {
this.nullableRestrictedInt = JsonNullable.of(nullableRestrictedInt);
}
}

private static class UnitIssue3 {
@NotNull
private JsonNullable<String> notNullString = JsonNullable.undefined();

public void setNotNullString(String value) {
notNullString = JsonNullable.of(value);
}
}
}

0 comments on commit 99e8b46

Please sign in to comment.