Skip to content

Commit

Permalink
Merge pull request #17493 from gsmet/workaround-hv-jandex
Browse files Browse the repository at this point in the history
Hibernate Validator - Work around Jandex issue in enclosingTarget()
  • Loading branch information
gsmet authored May 27, 2021
2 parents 358d856 + f6cffe0 commit 15afd3f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,26 +273,37 @@ public void build(HibernateValidatorRecorder recorder, RecorderContext recorderC
contributeClass(classNamesToBeValidated, indexView, annotation.target().asClass().name());
// no need for reflection in the case of a class level constraint
} else if (annotation.target().kind() == AnnotationTarget.Kind.TYPE) {
// container element constraints
AnnotationTarget enclosingTarget = annotation.target().asType().enclosingTarget();
if (enclosingTarget.kind() == AnnotationTarget.Kind.FIELD) {
contributeClass(classNamesToBeValidated, indexView, enclosingTarget.asField().declaringClass().name());
reflectiveFields.produce(new ReflectiveFieldBuildItem(enclosingTarget.asField()));
if (annotation.target().asType().target() != null) {
contributeClassMarkedForCascadingValidation(classNamesToBeValidated, indexView,
consideredAnnotation,
annotation.target().asType().target());
// there is a bug in Jandex regarding enclosingTarget() when annotations are on constructors located in a separate indexed library
// (enclosing target is a ClassInfo instead of a MethodInfo and enclosingTarget() tries to catch it to a MethodInfo
// leading to a ClassCastException)
// for now we swallow the exception, pending a bugfix in Jandex
// see https://github.com/quarkusio/quarkus/issues/17491
try {
// container element constraints
AnnotationTarget enclosingTarget = annotation.target().asType().enclosingTarget();
if (enclosingTarget.kind() == AnnotationTarget.Kind.FIELD) {
contributeClass(classNamesToBeValidated, indexView,
enclosingTarget.asField().declaringClass().name());
reflectiveFields.produce(new ReflectiveFieldBuildItem(enclosingTarget.asField()));
if (annotation.target().asType().target() != null) {
contributeClassMarkedForCascadingValidation(classNamesToBeValidated, indexView,
consideredAnnotation,
annotation.target().asType().target());
}
} else if (enclosingTarget.kind() == AnnotationTarget.Kind.METHOD) {
contributeClass(classNamesToBeValidated, indexView,
enclosingTarget.asMethod().declaringClass().name());
reflectiveMethods.produce(new ReflectiveMethodBuildItem(enclosingTarget.asMethod()));
if (annotation.target().asType().target() != null) {
contributeClassMarkedForCascadingValidation(classNamesToBeValidated, indexView,
consideredAnnotation,
annotation.target().asType().target());
}
contributeMethodsWithInheritedValidation(methodsWithInheritedValidation, indexView,
enclosingTarget.asMethod());
}
} else if (enclosingTarget.kind() == AnnotationTarget.Kind.METHOD) {
contributeClass(classNamesToBeValidated, indexView, enclosingTarget.asMethod().declaringClass().name());
reflectiveMethods.produce(new ReflectiveMethodBuildItem(enclosingTarget.asMethod()));
if (annotation.target().asType().target() != null) {
contributeClassMarkedForCascadingValidation(classNamesToBeValidated, indexView,
consideredAnnotation,
annotation.target().asType().target());
}
contributeMethodsWithInheritedValidation(methodsWithInheritedValidation, indexView,
enclosingTarget.asMethod());
} catch (Exception e) {
// ignore
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public void testMethodReturnValueContainerElementConstraint() throws NoSuchMetho
MethodReturnValueTestBean.class.getMethod("test"), invalidMap)).hasSize(1);
}

@Test
public void testConstructorParameterContainerElementConstraint() throws NoSuchMethodException, SecurityException {
List<String> invalidList = Collections.singletonList("");

assertThat(validatorFactory.getValidator().forExecutables().validateConstructorParameters(
ConstructorParameterTestBean.class.getConstructor(List.class), new Object[] { invalidList })).hasSize(1);
}

static class TestBean {

public Map<String, @NotBlank String> constrainedMap;
Expand Down Expand Up @@ -92,4 +100,11 @@ static class MethodReturnValueTestBean {
return null;
}
}

static class ConstructorParameterTestBean {

public ConstructorParameterTestBean(List<@NotBlank String> constrainedList) {
// do nothing
}
}
}

0 comments on commit 15afd3f

Please sign in to comment.