|
86 | 86 | import java.util.Map; |
87 | 87 | import java.util.Optional; |
88 | 88 | import java.util.Set; |
| 89 | +import java.util.Objects; |
89 | 90 | import java.util.stream.Collectors; |
90 | 91 | import java.util.stream.Stream; |
91 | 92 |
|
@@ -695,8 +696,8 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context |
695 | 696 | addRequiredItem(model, property.getName()); |
696 | 697 | } |
697 | 698 | final boolean applyNotNullAnnotations = io.swagger.v3.oas.annotations.media.Schema.RequiredMode.AUTO.equals(requiredMode); |
698 | | - annotations = addGenericTypeAnnotations(propDef, annotations); |
699 | | - applyBeanValidatorAnnotations(property, annotations, model, applyNotNullAnnotations); |
| 699 | + annotations = addGenericTypeArgumentAnnotationsForOptionalField(propDef, annotations); |
| 700 | + applyBeanValidatorAnnotations(propDef, property, annotations, model, applyNotNullAnnotations); |
700 | 701 |
|
701 | 702 | props.add(property); |
702 | 703 | } |
@@ -898,22 +899,38 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context |
898 | 899 | return model; |
899 | 900 | } |
900 | 901 |
|
901 | | - private Annotation[] addGenericTypeAnnotations(BeanPropertyDefinition propDef, Annotation[] annotations) { |
902 | | - AnnotatedField field = propDef.getField(); |
903 | | - if (!field.getType().getRawClass().equals(Optional.class)) { |
904 | | - return annotations; |
905 | | - } |
| 902 | + private Annotation[] addGenericTypeArgumentAnnotationsForOptionalField(BeanPropertyDefinition propDef, Annotation[] annotations) { |
| 903 | + |
| 904 | + boolean isNotOptionalType = Optional.ofNullable(propDef) |
| 905 | + .map(BeanPropertyDefinition::getField) |
| 906 | + .map(AnnotatedField::getAnnotated) |
| 907 | + .map(field -> !(field.getType().equals(Optional.class))) |
| 908 | + .orElse(false); |
906 | 909 |
|
907 | | - java.lang.reflect.AnnotatedType annotatedType = field.getAnnotated().getAnnotatedType(); |
908 | | - if (!(annotatedType instanceof AnnotatedParameterizedType)) { |
| 910 | + if (isNotOptionalType) { |
909 | 911 | return annotations; |
910 | 912 | } |
911 | 913 |
|
912 | | - AnnotatedParameterizedType parameterizedType = (AnnotatedParameterizedType) annotatedType; |
| 914 | + Stream<Annotation> genericTypeArgumentAnnotations = extractGenericTypeArgumentAnnotations(propDef); |
| 915 | + return Stream.concat(Stream.of(annotations), genericTypeArgumentAnnotations).toArray(Annotation[]::new); |
| 916 | + } |
| 917 | + |
| 918 | + private Stream<Annotation> extractGenericTypeArgumentAnnotations(BeanPropertyDefinition propDef) { |
| 919 | + return Optional.ofNullable(propDef) |
| 920 | + .map(BeanPropertyDefinition::getField) |
| 921 | + .map(AnnotatedField::getAnnotated) |
| 922 | + .map(this::getGenericTypeArgumentAnnotations) |
| 923 | + .orElseGet(Stream::of); |
| 924 | + } |
913 | 925 |
|
914 | | - Stream<Annotation> genericTypeAnnotations = Stream.of(parameterizedType.getAnnotatedActualTypeArguments()) |
915 | | - .flatMap(type -> Stream.of(type.getAnnotations())); |
916 | | - return Stream.concat(Stream.of(annotations), genericTypeAnnotations).toArray(Annotation[]::new); |
| 926 | + private Stream<Annotation> getGenericTypeArgumentAnnotations(Field field) { |
| 927 | + return Optional.of(field.getAnnotatedType()) |
| 928 | + .filter(annotatedType -> annotatedType instanceof AnnotatedParameterizedType) |
| 929 | + .map(annotatedType -> (AnnotatedParameterizedType) annotatedType) |
| 930 | + .map(AnnotatedParameterizedType::getAnnotatedActualTypeArguments) |
| 931 | + .map(types -> Stream.of(types) |
| 932 | + .flatMap(type -> Stream.of(type.getAnnotations()))) |
| 933 | + .orElseGet(Stream::of); |
917 | 934 | } |
918 | 935 |
|
919 | 936 | private boolean shouldResolveEnumAsRef(io.swagger.v3.oas.annotations.media.Schema resolvedSchemaAnnotation) { |
@@ -1309,6 +1326,15 @@ private AnnotatedType removeJsonIdentityAnnotations(AnnotatedType type) { |
1309 | 1326 | } |
1310 | 1327 | } |
1311 | 1328 |
|
| 1329 | + protected void applyBeanValidatorAnnotations(BeanPropertyDefinition propDef, Schema property, Annotation[] annotations, Schema parent, boolean applyNotNullAnnotations) { |
| 1330 | + applyBeanValidatorAnnotations(property, annotations, parent, applyNotNullAnnotations); |
| 1331 | + |
| 1332 | + if (Objects.nonNull(property.getItems())) { |
| 1333 | + Annotation[] genericTypeArgumentAnnotations = extractGenericTypeArgumentAnnotations(propDef).toArray(Annotation[]::new); |
| 1334 | + applyBeanValidatorAnnotations(property.getItems(), genericTypeArgumentAnnotations, property, applyNotNullAnnotations); |
| 1335 | + } |
| 1336 | + } |
| 1337 | + |
1312 | 1338 | protected void applyBeanValidatorAnnotations(Schema property, Annotation[] annotations, Schema parent, boolean applyNotNullAnnotations) { |
1313 | 1339 | Map<String, Annotation> annos = new HashMap<>(); |
1314 | 1340 | if (annotations != null) { |
|
0 commit comments