Skip to content

Commit

Permalink
Fix another case of a method validation with a class constraint (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov authored and sdelamo committed Aug 31, 2023
1 parent 9fe9148 commit 39dadf7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,15 @@ public <T> Set<ConstraintViolation<T>> validateReturnValue(@NonNull T object,
try (DefaultConstraintValidatorContext.GroupsValidation validation = context.withGroupSequence(groupSequence)) {
// Strip class annotations
AnnotationMetadata returnAm = returnType.asArgument().getAnnotationMetadata();
if (returnAm instanceof AnnotationMetadataHierarchy annotationMetadataHierarchy && returnAm.getDeclaredMetadata() instanceof AnnotationMetadataHierarchy) {
returnAm = new AnnotationMetadataHierarchy(
annotationMetadataHierarchy.getRootMetadata(),
annotationMetadataHierarchy.getDeclaredMetadata().getDeclaredMetadata()
);
if (returnAm instanceof AnnotationMetadataHierarchy annotationMetadataHierarchy) {
if (returnAm.getDeclaredMetadata() instanceof AnnotationMetadataHierarchy) {
returnAm = new AnnotationMetadataHierarchy(
annotationMetadataHierarchy.getRootMetadata(),
annotationMetadataHierarchy.getDeclaredMetadata().getDeclaredMetadata()
);
} else {
returnAm = annotationMetadataHierarchy.getDeclaredMetadata();
}
}
visitElement(context, bean, returnType.asArgument(), returnAm, returnValue, canCascade);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@
class BeanWithConstraintAndPrivateMethods {

@NotBlank
public String publicCombine(@NotNull String a, String b) {
public String publicCombine1(@NotNull String a, String b) {
return privateCombine(a, b);
}

public String publicCombine2(String a, String b) {
return privateCombine(a, b);
}

public String publicCombine3(@NotNull String a, String b) {
return privateCombine(a, b);
}

@NotBlank
public String publicCombine4(String a, String b) {
return privateCombine(a, b);
}

private String privateCombine(String a, String b) {
return a + b;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,25 @@ class CustomConstraintsSpec extends Specification {
BeanWithConstraintAndPrivateMethods abean = applicationContext.getBean(BeanWithConstraintAndPrivateMethods)

when:
def result = abean.publicCombine("A", "B")
def result = abean.publicCombine1("A", "B")

then:
result == "AB"

when:
result = abean.publicCombine2("A", "B")

then:
result == "AB"

when:
result = abean.publicCombine3("A", "B")

then:
result == "AB"

when:
result = abean.publicCombine4("A", "B")

then:
result == "AB"
Expand Down

0 comments on commit 39dadf7

Please sign in to comment.