From 728197e92ad7bd6f8ef1dd5b213248876f731499 Mon Sep 17 00:00:00 2001 From: Renato Pereira Back Date: Tue, 6 Jul 2021 11:37:06 -0300 Subject: [PATCH] Added recursion at ArcProxyBeanMetaDataClassNormalizer.normalize to fix #18388 (cherry picked from commit 0c66ba507327136728b9922c7b5bceefad086c1b) --- .../ArcProxyBeanMetaDataClassNormalizer.java | 13 +++-- ...cProxyBeanMetaDataClassNormalizerTest.java | 56 +++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 integration-tests/hibernate-validator/src/test/java/io/quarkus/hibernate/validator/runtime/ArcProxyBeanMetaDataClassNormalizerTest.java diff --git a/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/ArcProxyBeanMetaDataClassNormalizer.java b/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/ArcProxyBeanMetaDataClassNormalizer.java index fdb4dba53ade7..674b387ad6d37 100644 --- a/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/ArcProxyBeanMetaDataClassNormalizer.java +++ b/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/ArcProxyBeanMetaDataClassNormalizer.java @@ -6,16 +6,21 @@ /** * In the case of a proxy generated by Arc, return the parent class. + *
+ * This can have more than one level of hierarchy, in example: + * */ public class ArcProxyBeanMetaDataClassNormalizer implements BeanMetaDataClassNormalizer { @Override public Class normalize(Class beanClass) { - if (Subclass.class.isAssignableFrom(beanClass)) { - return beanClass.getSuperclass(); + Class targetClass = beanClass; + while (Subclass.class.isAssignableFrom(targetClass)) { + targetClass = targetClass.getSuperclass(); } - - return beanClass; + return targetClass; } } diff --git a/integration-tests/hibernate-validator/src/test/java/io/quarkus/hibernate/validator/runtime/ArcProxyBeanMetaDataClassNormalizerTest.java b/integration-tests/hibernate-validator/src/test/java/io/quarkus/hibernate/validator/runtime/ArcProxyBeanMetaDataClassNormalizerTest.java new file mode 100644 index 0000000000000..b08b737fbbfae --- /dev/null +++ b/integration-tests/hibernate-validator/src/test/java/io/quarkus/hibernate/validator/runtime/ArcProxyBeanMetaDataClassNormalizerTest.java @@ -0,0 +1,56 @@ +package io.quarkus.hibernate.validator.runtime; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import io.quarkus.arc.Subclass; + +/** + * Tests class normalization, which translates into getting the "original" class, without all the specializations instances of + * {@link io.quarkus.arc.Subclass}.
+ * There's no need to be a @{@link io.quarkus.test.junit.QuarkusTest} because no Quarkus related feature is required. + */ +class ArcProxyBeanMetaDataClassNormalizerTest { + + @Test + @DisplayName("Normalize should return same class if beanClass isn't a Subclass.") + void normalize_simpleClass() { + Class expected = Original.class; + + assertEquals(expected, new ArcProxyBeanMetaDataClassNormalizer().normalize(Original.class)); + } + + @Test + @DisplayName("Normalize should return 'superclass' if beanClass is the only Subclass in it's class hierarchy.") + void normalize_oneSubClass() { + Class expected = Original.class; + + assertEquals(expected, new ArcProxyBeanMetaDataClassNormalizer().normalize(FirstSubclass.class)); + } + + @Test + @DisplayName("Normalize should return upmost superclass if beanClass has more than one Subclass in it's class hierarchy.") + void normalize_multipleSubClasses() { + Class expected = Original.class; + + assertEquals(expected, new ArcProxyBeanMetaDataClassNormalizer().normalize(SecondSubclass.class)); + } + + private static class Original { + } + + /** + * Simulates an object injected through @{@link javax.inject.Inject}. + */ + private static class FirstSubclass extends Original implements Subclass { + } + + /** + * Simulates an object injected through @{@link io.quarkus.test.junit.mockito.InjectMock} + * or @{@link io.quarkus.test.junit.mockito.InjectSpy}. + */ + private static class SecondSubclass extends FirstSubclass { + } +}