Skip to content

Commit

Permalink
Filter out @Valid annotations from jakarta.validation
Browse files Browse the repository at this point in the history
Add test for filtering out @Valid annotations

Although validation using the Jakarta validation API is not yet supported (it would require a Hibernator version bump and likely migration away from javax validation), this adds a dependency on it to test filtering out Jakarta @Valid annotations.
  • Loading branch information
elyse-yao authored and Randgalt committed Dec 9, 2024
1 parent 9c98f1b commit 978fa4b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<asm-version>7.2</asm-version>
<validation-api-version>2.0.1.Final</validation-api-version>
<hibernate-validator-version>6.2.0.Final</hibernate-validator-version>
<jakarta-validation-api-version>3.1.0</jakarta-validation-api-version>
<javax-el-version>3.0.1-b09</javax-el-version>
</properties>

Expand Down Expand Up @@ -164,6 +165,12 @@
<version>${validation-api-version}</version>
</dependency>

<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>${jakarta-validation-api-version}</version>
</dependency>

<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class InternalRecordBuilderProcessor {

private static final TypeName optionalType = TypeName.get(Optional.class);
private static final TypeName overrideType = TypeName.get(Override.class);
private static final TypeName validType = ClassName.get("javax.validation", "Valid");
private static final TypeName javaxValidType = ClassName.get("javax.validation", "Valid");
private static final TypeName jakartaValidType = ClassName.get("jakarta.validation", "Valid");
private static final TypeName validatorTypeName = ClassName.get("io.soabase.recordbuilder.validator",
"RecordBuilderValidator");
private static final TypeVariableName rType = TypeVariableName.get("R");
Expand Down Expand Up @@ -868,7 +869,7 @@ private boolean filterOutOverride(AnnotationSpec annotationSpec) {
}

private boolean filterOutValid(AnnotationSpec annotationSpec) {
return !annotationSpec.type.equals(validType);
return !annotationSpec.type.equals(javaxValidType) && !annotationSpec.type.equals(jakartaValidType);
}

private void addConstructorAnnotations(RecordClassType component, ParameterSpec.Builder parameterSpecBuilder) {
Expand Down
5 changes: 5 additions & 0 deletions record-builder-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<artifactId>validation-api</artifactId>
</dependency>

<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>

<dependency>
<groupId>io.soabase.record-builder</groupId>
<artifactId>record-builder-processor</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

@RecordBuilder
@RecordBuilder.Options(useValidationApi = true)
public record RequestWithValid(@NotNull @Valid Part part) implements RequestWithValidBuilder.With {
public record RequestWithValid(@NotNull @Valid @jakarta.validation.Valid Part part)
implements RequestWithValidBuilder.With {
public record Part(@NotBlank String name) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
Expand All @@ -31,6 +32,18 @@ void testInheritComponentAnnotationsFalse() throws NoSuchMethodException {
Assertions.assertNull(method.getAnnotation(NotNull.class));
}

@Test
void testFiltersOutJavaXValid() throws NoSuchMethodException {
var method = RequestWithValidBuilder.With.class.getMethod("part");
Assertions.assertNull(method.getAnnotation(Valid.class));
}

@Test
void testFiltersOutJakartaValid() throws NoSuchMethodException {
var method = RequestWithValidBuilder.With.class.getMethod("part");
Assertions.assertNull(method.getAnnotation(jakarta.validation.Valid.class));
}

@Test
void testStaticConstructor() throws NoSuchMethodException {
var method = AnnotatedBuilder.class.getMethod("Annotated", String.class, Integer.TYPE, Double.TYPE);
Expand Down

0 comments on commit 978fa4b

Please sign in to comment.