Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate enum/const from Jakarta AssertTrue and/or AssertFalse annotations #456

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ private boolean isSupportedEnumValue(Object target) {
}
Class<?> targetType = target.getClass();
return targetType.isPrimitive()
|| Boolean.class.isAssignableFrom(targetType)
|| Number.class.isAssignableFrom(targetType)
|| CharSequence.class.isAssignableFrom(targetType)
|| Enum.class.isAssignableFrom(targetType);
Expand Down
1 change: 1 addition & 0 deletions jsonschema-module-jakarta-validation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Module for the [jsonschema-generator](../jsonschema-generator) – deriving JSON
7. Populate "pattern" for strings. Based on `@Pattern`/`@Email`, when corresponding `JakartaValidationOption.INCLUDE_PATTERN_EXPRESSIONS` is being provided in constructor.
8. Populate "minimum"/"exclusiveMinimum" for numbers. Based on `@Min`/`@DecimalMin`/`@Positive`/`@PositiveOrZero`.
9. Populate "maximum"/"exclusiveMaximum" for numbers. Based on `@Max`/`@DecimalMax`/`@Negative`/`@NegativeOrZero`.
10. Populate "enum"/"const" for booleans. Based on `@AssertTrue`/`@AssertFalse`.
Nephery marked this conversation as resolved.
Show resolved Hide resolved

Schema attributes derived from validation annotations on fields are also applied to their respective getter methods.
Schema attributes derived from validation annotations on getter methods are also applied to their associated fields.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigPart;
import com.github.victools.jsonschema.generator.SchemaKeyword;
import jakarta.validation.Constraint;
import jakarta.validation.constraints.AssertFalse;
CarstenWickner marked this conversation as resolved.
Show resolved Hide resolved
import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.DecimalMax;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.Email;
Expand All @@ -45,12 +47,13 @@
import jakarta.validation.constraints.Size;
import java.lang.annotation.Annotation;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.ToIntBiFunction;
Expand Down Expand Up @@ -116,8 +119,9 @@ public void applyToConfigBuilder(SchemaGeneratorConfigBuilder builder) {
if (this.options.contains(JakartaValidationOption.NOT_NULLABLE_METHOD_IS_REQUIRED)) {
methodConfigPart.withRequiredCheck(this::isRequired);
}
Stream.of(DecimalMax.class, DecimalMin.class, Email.class, Max.class, Min.class, Negative.class, NegativeOrZero.class,
NotBlank.class, NotEmpty.class, Null.class, NotNull.class, Pattern.class, Positive.class, PositiveOrZero.class, Size.class)
Stream.of(AssertFalse.class, AssertTrue.class, DecimalMax.class, DecimalMin.class, Email.class, Max.class, Min.class,
Negative.class, NegativeOrZero.class, NotBlank.class, NotEmpty.class, Null.class, NotNull.class,
Pattern.class, Positive.class, PositiveOrZero.class, Size.class)
.forEach(annotationType -> builder.withAnnotationInclusionOverride(annotationType, AnnotationInclusion.INCLUDE_AND_INHERIT));
}

Expand All @@ -137,6 +141,7 @@ private void applyToConfigPart(SchemaGeneratorConfigPart<?> configPart) {
configPart.withNumberExclusiveMinimumResolver(this::resolveNumberExclusiveMinimum);
configPart.withNumberInclusiveMaximumResolver(this::resolveNumberInclusiveMaximum);
configPart.withNumberExclusiveMaximumResolver(this::resolveNumberExclusiveMaximum);
configPart.withEnumResolver(this::resolveEnum);
configPart.withInstanceAttributeOverride(this::overrideInstanceAttributes);

if (this.options.contains(JakartaValidationOption.INCLUDE_PATTERN_EXPRESSIONS)) {
Expand Down Expand Up @@ -499,6 +504,25 @@ protected BigDecimal resolveNumberExclusiveMaximum(MemberScope<?, ?> member) {
return null;
}

/**
* Look-up the finite list of possible values.
* @param member field/method to determine allowed values for
* @return applicable "const"/"enum" values or null
* @see AssertTrue
* @see AssertFalse
*/
protected List<Object> resolveEnum(MemberScope<?, ?> member) {
List<Object> values;
if (this.getAnnotationFromFieldOrGetter(member, AssertTrue.class, AssertTrue::groups) != null) {
values = Collections.singletonList(true);
} else if (this.getAnnotationFromFieldOrGetter(member, AssertFalse.class, AssertFalse::groups) != null) {
values = Collections.singletonList(false);
} else {
values = null;
}
return values;
}

/**
* Implementation of the functional {@code InstanceAttributeOverrideV2} interface.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.generator.SchemaVersion;
import jakarta.validation.Constraint;
import jakarta.validation.constraints.AssertFalse;
import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.DecimalMax;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.Email;
Expand Down Expand Up @@ -134,6 +136,11 @@ static class TestClass {
@DecimalMin(value = "0", inclusive = false)
@DecimalMax(value = "1", inclusive = false)
public double exclusiveRangeDouble;

@AssertTrue
public boolean trueBoolean;
@AssertFalse
public boolean falseBoolean;
}

static class Book implements Publication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.github.victools.jsonschema.generator.MethodScope;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigPart;
import jakarta.validation.constraints.AssertFalse;
import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.DecimalMax;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.Email;
Expand All @@ -40,6 +42,8 @@
import jakarta.validation.constraints.Size;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
Expand Down Expand Up @@ -146,6 +150,7 @@ private void verifyCommonConfigurations() {
Mockito.verify(this.fieldConfigPart).withNumberExclusiveMinimumResolver(Mockito.any());
Mockito.verify(this.fieldConfigPart).withNumberInclusiveMaximumResolver(Mockito.any());
Mockito.verify(this.fieldConfigPart).withNumberExclusiveMaximumResolver(Mockito.any());
Mockito.verify(this.fieldConfigPart).withEnumResolver(Mockito.any());
Mockito.verify(this.fieldConfigPart).withInstanceAttributeOverride(Mockito.any(InstanceAttributeOverrideV2.class));

Mockito.verify(this.methodConfigPart).withNullableCheck(Mockito.any());
Expand All @@ -158,9 +163,10 @@ private void verifyCommonConfigurations() {
Mockito.verify(this.methodConfigPart).withNumberExclusiveMinimumResolver(Mockito.any());
Mockito.verify(this.methodConfigPart).withNumberInclusiveMaximumResolver(Mockito.any());
Mockito.verify(this.methodConfigPart).withNumberExclusiveMaximumResolver(Mockito.any());
Mockito.verify(this.methodConfigPart).withEnumResolver(Mockito.any());
Mockito.verify(this.methodConfigPart).withInstanceAttributeOverride(Mockito.any(InstanceAttributeOverrideV2.class));

Mockito.verify(this.configBuilder, Mockito.times(15))
Mockito.verify(this.configBuilder, Mockito.times(17))
.withAnnotationInclusionOverride(Mockito.any(), Mockito.eq(AnnotationInclusion.INCLUDE_AND_INHERIT));
}

Expand Down Expand Up @@ -533,6 +539,57 @@ private void testNumberMinMaxResolvers(String fieldName, BigDecimal expectedMinI
Assertions.assertEquals(expectedMaxExclusive, maxExclusive);
}

static Stream<Arguments> parametersForTestEnumResolvers() {
return Stream.of(
Arguments.of("unannotatedBoolean", new Object[]{}),
Arguments.of("trueBoolean", new Object[]{true}),
Arguments.of("trueOnGetterBoolean", new Object[]{true}),
Arguments.of("falseBoolean", new Object[]{false}),
Arguments.of("falseOnGetterBoolean", new Object[]{false}),
Arguments.of("trueAndFalseBoolean", new Object[]{true}),
Arguments.of("trueAndFalseOnGetterBoolean", new Object[]{true})
);
}

@ParameterizedTest
@MethodSource("parametersForTestEnumResolvers")
public void testEnumResolversNoValidationGroup(String fieldName, Object[] expectedValues) {
new JakartaValidationModule().applyToConfigBuilder(this.configBuilder);

this.testEnumResolvers(fieldName, expectedValues);
}

@ParameterizedTest
@MethodSource("parametersForTestEnumResolvers")
public void testEnumResolversMatchingValidationGroup(String fieldName, Object[] expectedValues) {
new JakartaValidationModule()
.forValidationGroups(Test.class)
.applyToConfigBuilder(this.configBuilder);

this.testEnumResolvers(fieldName, expectedValues);
}

@ParameterizedTest
@MethodSource("parametersForTestEnumResolvers")
public void testEnumResolversDifferentValidationGroup(String fieldName, Object[] ignoredExpectedValues) {
new JakartaValidationModule()
.forValidationGroups(Object.class)
.applyToConfigBuilder(this.configBuilder);

// none of the annotated values are actually expected to be returned
this.testEnumResolvers(fieldName);
}

private void testEnumResolvers(String fieldName, Object... values) {
TestType testType = new TestType(TestClassForEnums.class);
FieldScope field = testType.getMemberField(fieldName);

ArgumentCaptor<ConfigFunction<FieldScope, Collection<?>>> enumCaptor = ArgumentCaptor.forClass(ConfigFunction.class);
Mockito.verify(this.fieldConfigPart).withEnumResolver(enumCaptor.capture());
Collection<?> enumValues = enumCaptor.getValue().apply(field);
Assertions.assertEquals(values.length > 0 ? Arrays.asList(values) : null, enumValues);
}

static Stream<Arguments> parametersForTestValidationGroupSetting() {
return Stream.of(
Arguments.of("skippedConfiguringGroups", "fieldWithoutValidationGroup", Boolean.TRUE, null),
Expand Down Expand Up @@ -818,6 +875,36 @@ public Long getNegativeOrZeroOnGetterLong() {
}
}

private static class TestClassForEnums {
boolean unannotatedBoolean;
@AssertTrue(groups = Test.class)
boolean trueBoolean;
boolean trueOnGetterBoolean;
@AssertFalse(groups = Test.class)
boolean falseBoolean;
boolean falseOnGetterBoolean;
@AssertTrue(groups = Test.class)
@AssertFalse(groups = Test.class)
boolean trueAndFalseBoolean;
boolean trueAndFalseOnGetterBoolean;

@AssertTrue(groups = Test.class)
public boolean isTrueOnGetterBoolean() {
return trueOnGetterBoolean;
}

@AssertFalse(groups = Test.class)
public boolean isFalseOnGetterBoolean() {
return falseOnGetterBoolean;
}

@AssertTrue(groups = Test.class)
@AssertFalse(groups = Test.class)
public boolean isTrueAndFalseOnGetterBoolean() {
return trueAndFalseOnGetterBoolean;
}
}

private static class TestClassForValidationGroups {

@Null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"exclusiveMinimum": 0,
"exclusiveMaximum": 1
},
"falseBoolean": {
"type": "boolean",
"const": false
},
"inclusiveRangeInt": {
"type": "integer",
"minimum": 7,
Expand Down Expand Up @@ -113,6 +117,10 @@
"type": "string",
"minLength": 5,
"maxLength": 12
},
"trueBoolean": {
"type": "boolean",
"const": true
}
},
"required": ["notBlankText", "notEmptyList", "notEmptyMap", "notEmptyPatternText", "notNullEmail", "notNullList"]
Expand Down
1 change: 1 addition & 0 deletions slate-docs/source/includes/_jakarta-validation-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(Sc
7. Populate "pattern" for strings. Based on `@Pattern`/`@Email`, if `JakartaValidationOption.INCLUDE_PATTERN_EXPRESSIONS` is being provided (i.e. this is an "opt-in").
8. Populate "minimum"/"exclusiveMinimum" for numbers. Based on `@Min`/`@DecimalMin`/`@Positive`/`@PositiveOrZero`.
9. Populate "maximum"/"exclusiveMaximum" for numbers. Based on `@Max`/`@DecimalMax`/`@Negative`/`@NegativeOrZero`.
10. Populate "enum"/"const" for booleans. Based on `@AssertTrue`/`@AssertFalse`.

Schema attributes derived from validation annotations on fields are also applied to their respective getter methods.
Schema attributes derived from validation annotations on getter methods are also applied to their associated fields.
Expand Down
Loading