Skip to content

Commit

Permalink
Don't include @Valid on base interfaces (#99)
Browse files Browse the repository at this point in the history
The validation API doesn't accept `@Valid` on base interfaces. Filter
them out.

Fixes #97
  • Loading branch information
Randgalt authored Mar 21, 2022
1 parent efd1a6b commit 642dd01
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
Expand All @@ -47,6 +48,7 @@ class InternalRecordBuilderProcessor {
private final CollectionBuilderUtils collectionBuilderUtils;

private static final TypeName overrideType = TypeName.get(Override.class);
private static final TypeName validType = ClassName.get("javax.validation", "Valid");
private static final TypeName validatorTypeName = ClassName.get("io.soabase.recordbuilder.validator", "RecordBuilderValidator");
private static final TypeVariableName rType = TypeVariableName.get("R");
private final ProcessingEnvironment processingEnv;
Expand Down Expand Up @@ -729,14 +731,18 @@ private void addNestedGetterMethod(TypeSpec.Builder classBuilder, RecordClassTyp
.addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC)
.addAnnotation(generatedRecordBuilderAnnotation)
.returns(component.typeName());
addAccessorAnnotations(component, methodSpecBuilder);
addAccessorAnnotations(component, methodSpecBuilder, this::filterOutValid);
classBuilder.addMethod(methodSpecBuilder.build());
}

private boolean filterOutOverride(AnnotationSpec annotationSpec) {
return !annotationSpec.type.equals(overrideType);
}

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

private void addConstructorAnnotations(RecordClassType component, ParameterSpec.Builder parameterSpecBuilder) {
if (metaData.inheritComponentAnnotations()) {
component.getCanonicalConstructorAnnotations()
Expand All @@ -747,12 +753,13 @@ private void addConstructorAnnotations(RecordClassType component, ParameterSpec.
}
}

private void addAccessorAnnotations(RecordClassType component, MethodSpec.Builder methodSpecBuilder) {
private void addAccessorAnnotations(RecordClassType component, MethodSpec.Builder methodSpecBuilder, Predicate<AnnotationSpec> additionalFilter) {
if (metaData.inheritComponentAnnotations()) {
component.getAccessorAnnotations()
.stream()
.map(AnnotationSpec::get)
.filter(this::filterOutOverride)
.filter(additionalFilter)
.forEach(methodSpecBuilder::addAnnotation);
}
}
Expand Down Expand Up @@ -894,7 +901,7 @@ public T p() {
.addAnnotation(generatedRecordBuilderAnnotation)
.returns(component.typeName())
.addStatement("return $L", component.name());
addAccessorAnnotations(component, methodSpecBuilder);
addAccessorAnnotations(component, methodSpecBuilder, __ -> true);
builder.addMethod(methodSpecBuilder.build());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright 2019 Jordan Zimmerman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.soabase.recordbuilder.test;

import io.soabase.recordbuilder.core.RecordBuilder;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@RecordBuilder
@RecordBuilder.Options(useValidationApi = true)
public record RequestWithValid(@NotNull @Valid Part part) implements RequestWithValidBuilder.With {
public record Part(@NotBlank String name) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,14 @@ void testValidationWithNewProperty() {
var valid = RequiredRecord2Builder.builder().hey("hey").i(1).build();
Assertions.assertThrows(ValidationException.class, () -> valid.withHey(null));
}

@Test
void testRequestWithValid() {
Assertions.assertDoesNotThrow(() -> RequestWithValidBuilder.builder()
.part(new RequestWithValid.Part("jsfjsf"))
.build());
Assertions.assertThrows(ValidationException.class, () -> RequestWithValidBuilder.builder()
.part(new RequestWithValid.Part(""))
.build());
}
}

0 comments on commit 642dd01

Please sign in to comment.