File tree Expand file tree Collapse file tree
main/java/it/aboutbits/springboot/toolbox
test/java/it/aboutbits/springboot/toolbox/reflection/util Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -20,15 +20,25 @@ public static <T extends CustomType<?>> Constructor<T> getCustomTypeConstructor(
2020 }
2121 }
2222
23- public static Class <?> getWrappedType (Class <? extends CustomType <?>> customType ) {
24- var customTypeInterface = Arrays .stream (customType .getGenericInterfaces ())
25- .filter (i ->
26- i instanceof ParameterizedType
27- && CustomType .class .isAssignableFrom ((Class <?>) ((ParameterizedType ) i ).getRawType ())
28- ).findFirst ()
29- .map (i -> (ParameterizedType ) i )
30- .orElseThrow ();
31-
32- return (Class <?>) customTypeInterface .getActualTypeArguments ()[0 ];
23+ public static Class <?> getWrappedType (Class <? extends CustomType <?>> customType ) throws NoSuchMethodException {
24+ Class <?> currentClass = customType ;
25+ while (currentClass != null ) {
26+ // Check interfaces of the current class
27+ var customTypeInterface = Arrays .stream (currentClass .getGenericInterfaces ())
28+ .filter (i ->
29+ i instanceof ParameterizedType
30+ && CustomType .class .isAssignableFrom ((Class <?>) ((ParameterizedType ) i ).getRawType ())
31+ ).findFirst ()
32+ .map (i -> (ParameterizedType ) i );
33+
34+ if (customTypeInterface .isPresent ()) {
35+ return (Class <?>) customTypeInterface .get ().getActualTypeArguments ()[0 ];
36+ }
37+
38+ // Move to the parent class
39+ currentClass = currentClass .getSuperclass ();
40+ }
41+
42+ throw new NoSuchMethodException ();
3343 }
3444}
Original file line number Diff line number Diff line change 99import it .aboutbits .springboot .toolbox .type .CustomType ;
1010import it .aboutbits .springboot .toolbox .type .ScaledBigDecimal ;
1111import it .aboutbits .springboot .toolbox .type .identity .EntityId ;
12+ import lombok .SneakyThrows ;
1213
1314import java .math .BigDecimal ;
1415import java .math .BigInteger ;
1718public class CustomTypeModelConverter implements ModelConverter {
1819
1920 @ Override
21+ @ SneakyThrows (NoSuchMethodException .class )
2022 public Schema <?> resolve (
2123 AnnotatedType annotatedType ,
2224 ModelConverterContext context ,
Original file line number Diff line number Diff line change 88import it .aboutbits .springboot .toolbox .type .CustomType ;
99import it .aboutbits .springboot .toolbox .type .ScaledBigDecimal ;
1010import it .aboutbits .springboot .toolbox .type .identity .EntityId ;
11+ import lombok .SneakyThrows ;
1112import lombok .extern .slf4j .Slf4j ;
1213import org .springdoc .core .customizers .PropertyCustomizer ;
1314
1718@ Slf4j
1819public class CustomTypePropertyCustomizer implements PropertyCustomizer {
1920 @ Override
21+ @ SneakyThrows (NoSuchMethodException .class )
2022 public Schema <?> customize (Schema property , AnnotatedType annotatedType ) {
2123 var type = annotatedType .getType ();
2224
Original file line number Diff line number Diff line change @@ -44,6 +44,20 @@ void testGetCustomTypeConstructorWithMismatchingConstructor() {
4444 );
4545 }
4646
47+ @ Test
48+ void testGetCustomTypeConstructorWithInheritedCustomType () throws Exception {
49+ // given
50+ var customTypeClass = ChildClass .class ;
51+
52+ // when
53+ var constructor = CustomTypeReflectionUtil .getCustomTypeConstructor (customTypeClass );
54+
55+ // then
56+ assertThat (constructor ).isNotNull ();
57+ assertThat (constructor .getParameterCount ()).isEqualTo (1 );
58+ assertThat (constructor .getParameterTypes ()[0 ]).isEqualTo (Integer .class );
59+ }
60+
4761 @ SuppressWarnings ("checkstyle:RedundantModifier" )
4862 @ CustomTypeScanner .DisableCustomTypeConfiguration
4963 public static class ValidCustomType implements CustomType <String > {
@@ -88,4 +102,27 @@ public String value() {
88102 return value ;
89103 }
90104 }
105+
106+ @ SuppressWarnings ("checkstyle:RedundantModifier" )
107+ @ CustomTypeScanner .DisableCustomTypeConfiguration
108+ public static class ParentClass implements CustomType <Integer > {
109+ private final Integer value ;
110+
111+ public ParentClass (Integer value ) {
112+ this .value = value ;
113+ }
114+
115+ @ Override
116+ public Integer value () {
117+ return value ;
118+ }
119+ }
120+
121+ @ SuppressWarnings ("checkstyle:RedundantModifier" )
122+ @ CustomTypeScanner .DisableCustomTypeConfiguration
123+ public static class ChildClass extends ParentClass {
124+ public ChildClass (Integer value ) {
125+ super (value );
126+ }
127+ }
91128}
You can’t perform that action at this time.
0 commit comments