Skip to content

Commit 518007d

Browse files
micrycfrantuma
authored andcommitted
Add logic for processing generic type arguments in Java Records
1 parent dc8785e commit 518007d

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ private Annotation[] addGenericTypeArgumentAnnotationsForOptionalField(BeanPrope
10821082
.map(field -> !(field.getType().equals(Optional.class)))
10831083
.orElse(false);
10841084

1085-
if (isNotOptionalType) {
1085+
if (isNotOptionalType || isRecordType(propDef)) {
10861086
return annotations;
10871087
}
10881088

@@ -1091,21 +1091,51 @@ private Annotation[] addGenericTypeArgumentAnnotationsForOptionalField(BeanPrope
10911091
}
10921092

10931093
private Stream<Annotation> extractGenericTypeArgumentAnnotations(BeanPropertyDefinition propDef) {
1094+
if (isRecordType(propDef)){
1095+
return getRecordComponentAnnotations(propDef);
1096+
}
10941097
return Optional.ofNullable(propDef)
10951098
.map(BeanPropertyDefinition::getField)
10961099
.map(AnnotatedField::getAnnotated)
10971100
.map(this::getGenericTypeArgumentAnnotations)
10981101
.orElseGet(Stream::of);
10991102
}
11001103

1104+
private Stream<Annotation> getRecordComponentAnnotations(BeanPropertyDefinition propDef) {
1105+
try {
1106+
Method accessor = propDef.getPrimaryMember().getDeclaringClass().getDeclaredMethod(propDef.getName());
1107+
return getGenericTypeArgumentAnnotations(accessor.getAnnotatedReturnType());
1108+
} catch (NoSuchMethodException e) {
1109+
LOGGER.error("Accessor for record component not found");
1110+
return Stream.empty();
1111+
}
1112+
}
1113+
1114+
private Boolean isRecordType(BeanPropertyDefinition propDef){
1115+
try {
1116+
Class<?> clazz = propDef.getPrimaryMember().getDeclaringClass();
1117+
Method isRecordMethod = Class.class.getMethod("isRecord");
1118+
return (Boolean) isRecordMethod.invoke(clazz);
1119+
} catch (NoSuchMethodException e) {
1120+
return false;
1121+
} catch (Exception e) {
1122+
e.printStackTrace();
1123+
return false;
1124+
}
1125+
}
1126+
11011127
private Stream<Annotation> getGenericTypeArgumentAnnotations(Field field) {
1102-
return Optional.of(field.getAnnotatedType())
1103-
.filter(annotatedType -> annotatedType instanceof AnnotatedParameterizedType)
1104-
.map(annotatedType -> (AnnotatedParameterizedType) annotatedType)
1105-
.map(AnnotatedParameterizedType::getAnnotatedActualTypeArguments)
1106-
.map(types -> Stream.of(types)
1107-
.flatMap(type -> Stream.of(type.getAnnotations())))
1108-
.orElseGet(Stream::of);
1128+
return getGenericTypeArgumentAnnotations(field.getAnnotatedType());
1129+
}
1130+
1131+
private Stream<Annotation> getGenericTypeArgumentAnnotations(java.lang.reflect.AnnotatedType annotatedType) {
1132+
return Optional.of(annotatedType)
1133+
.filter(type -> type instanceof AnnotatedParameterizedType)
1134+
.map(type -> (AnnotatedParameterizedType) type)
1135+
.map(AnnotatedParameterizedType::getAnnotatedActualTypeArguments)
1136+
.map(types -> Stream.of(types)
1137+
.flatMap(type -> Stream.of(type.getAnnotations())))
1138+
.orElseGet(Stream::of);
11091139
}
11101140

11111141
private boolean shouldResolveEnumAsRef(io.swagger.v3.oas.annotations.media.Schema resolvedSchemaAnnotation) {

0 commit comments

Comments
 (0)