|
26 | 26 |
|
27 | 27 | package org.springdoc.core.converters;
|
28 | 28 |
|
29 |
| -import java.lang.reflect.Field; |
| 29 | +import java.lang.annotation.Annotation; |
30 | 30 | import java.lang.reflect.Modifier;
|
31 | 31 | import java.util.ArrayList;
|
32 | 32 | import java.util.Collection;
|
33 | 33 | import java.util.Collections;
|
34 | 34 | import java.util.HashSet;
|
35 | 35 | import java.util.Iterator;
|
36 | 36 | import java.util.List;
|
| 37 | +import java.util.Map; |
37 | 38 | import java.util.Set;
|
38 | 39 |
|
39 | 40 | import com.fasterxml.jackson.annotation.JsonUnwrapped;
|
40 |
| -import com.fasterxml.jackson.databind.BeanDescription; |
41 | 41 | import com.fasterxml.jackson.databind.JavaType;
|
42 | 42 | import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
|
43 | 43 | import io.swagger.v3.core.converter.AnnotatedType;
|
|
50 | 50 | import io.swagger.v3.oas.models.media.ObjectSchema;
|
51 | 51 | import io.swagger.v3.oas.models.media.Schema;
|
52 | 52 | import org.apache.commons.lang3.ArrayUtils;
|
53 |
| -import org.apache.commons.lang3.reflect.FieldUtils; |
54 | 53 | import org.springdoc.core.providers.ObjectMapperProvider;
|
55 | 54 |
|
| 55 | +import static java.util.function.Function.identity; |
| 56 | +import static java.util.stream.Collectors.toMap; |
| 57 | + |
56 | 58 | /**
|
57 | 59 | * The type Polymorphic model converter.
|
58 | 60 | *
|
@@ -122,28 +124,18 @@ else if (resolvedSchema.getProperties().containsKey(javaType.getRawClass().getSi
|
122 | 124 | public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
|
123 | 125 | JavaType javaType = springDocObjectMapper.jsonMapper().constructType(type.getType());
|
124 | 126 | if (javaType != null) {
|
125 |
| - BeanDescription javaTypeIntrospection = springDocObjectMapper.jsonMapper().getDeserializationConfig().introspect(javaType); |
126 |
| - for (BeanPropertyDefinition property : javaTypeIntrospection.findProperties()) { |
127 |
| - boolean isUnwrapped = (property.getField() != null && property.getField().hasAnnotation(JsonUnwrapped.class)) || |
128 |
| - (property.getGetter() != null && property.getGetter().hasAnnotation(JsonUnwrapped.class)); |
129 |
| - |
130 |
| - if (isUnwrapped) { |
| 127 | + for (BeanPropertyBiDefinition propertyDef : introspectBeanProperties(javaType)) { |
| 128 | + if (propertyDef.isAnyAnnotated(JsonUnwrapped.class)) { |
131 | 129 | if (!TypeNameResolver.std.getUseFqn())
|
132 | 130 | PARENT_TYPES_TO_IGNORE.add(javaType.getRawClass().getSimpleName());
|
133 | 131 | else
|
134 | 132 | PARENT_TYPES_TO_IGNORE.add(javaType.getRawClass().getName());
|
135 | 133 | }
|
136 | 134 | else {
|
137 |
| - io.swagger.v3.oas.annotations.media.Schema declaredSchema = null; |
138 |
| - if (property.getField() != null) { |
139 |
| - declaredSchema = property.getField().getAnnotation(io.swagger.v3.oas.annotations.media.Schema.class); |
140 |
| - } else if (property.getGetter() != null) { |
141 |
| - declaredSchema = property.getGetter().getAnnotation(io.swagger.v3.oas.annotations.media.Schema.class); |
142 |
| - } |
143 |
| - |
| 135 | + io.swagger.v3.oas.annotations.media.Schema declaredSchema = propertyDef.getAnyAnnotation(io.swagger.v3.oas.annotations.media.Schema.class); |
144 | 136 | if (declaredSchema != null &&
|
145 | 137 | (ArrayUtils.isNotEmpty(declaredSchema.oneOf()) || ArrayUtils.isNotEmpty(declaredSchema.allOf()))) {
|
146 |
| - TYPES_TO_SKIP.add(property.getPrimaryType().getRawClass().getSimpleName()); |
| 138 | + TYPES_TO_SKIP.add(propertyDef.getPrimaryType().getRawClass().getSimpleName()); |
147 | 139 | }
|
148 | 140 | }
|
149 | 141 | }
|
@@ -227,4 +219,99 @@ private boolean isConcreteClass(AnnotatedType type) {
|
227 | 219 | Class<?> clazz = javaType.getRawClass();
|
228 | 220 | return !Modifier.isAbstract(clazz.getModifiers()) && !clazz.isInterface();
|
229 | 221 | }
|
| 222 | + |
| 223 | + /** |
| 224 | + * Introspects the properties of the given Java type based on serialization and deserialization configurations. |
| 225 | + * This method identifies properties present in both JSON serialization and deserialization views, |
| 226 | + * and pairs them into a list of {@code BeanPropertyBiDefinition}. |
| 227 | + */ |
| 228 | + private List<BeanPropertyBiDefinition> introspectBeanProperties(JavaType javaType) { |
| 229 | + Map<String, BeanPropertyDefinition> forSerializationProps = |
| 230 | + springDocObjectMapper.jsonMapper() |
| 231 | + .getSerializationConfig() |
| 232 | + .introspect(javaType) |
| 233 | + .findProperties() |
| 234 | + .stream() |
| 235 | + .collect(toMap(BeanPropertyDefinition::getName, identity())); |
| 236 | + Map<String, BeanPropertyDefinition> forDeserializationProps = |
| 237 | + springDocObjectMapper.jsonMapper() |
| 238 | + .getDeserializationConfig() |
| 239 | + .introspect(javaType) |
| 240 | + .findProperties() |
| 241 | + .stream() |
| 242 | + .collect(toMap(BeanPropertyDefinition::getName, identity())); |
| 243 | + |
| 244 | + return forSerializationProps.keySet().stream() |
| 245 | + .map(key -> new BeanPropertyBiDefinition(forSerializationProps.get(key), forDeserializationProps.get(key))) |
| 246 | + .toList(); |
| 247 | + } |
| 248 | + |
| 249 | + /** |
| 250 | + * A record representing the bi-definition of a bean property, combining both |
| 251 | + * serialization and deserialization property views. |
| 252 | + */ |
| 253 | + private record BeanPropertyBiDefinition( |
| 254 | + BeanPropertyDefinition forSerialization, |
| 255 | + BeanPropertyDefinition forDeserialization |
| 256 | + ) { |
| 257 | + |
| 258 | + /** |
| 259 | + * Retrieves an annotation of the specified type from either the serialization or |
| 260 | + * deserialization property definition (field, getter, setter), returning the first available match. |
| 261 | + */ |
| 262 | + public <A extends Annotation> A getAnyAnnotation(Class<A> acls) { |
| 263 | + A anyForSerializationAnnotation = getAnyAnnotation(forSerialization, acls); |
| 264 | + A anyForDeserializationAnnotation = getAnyAnnotation(forDeserialization, acls); |
| 265 | + |
| 266 | + return anyForSerializationAnnotation != null ? anyForSerializationAnnotation : anyForDeserializationAnnotation; |
| 267 | + } |
| 268 | + |
| 269 | + /** |
| 270 | + * Checks if any annotation of the specified type exists across serialization |
| 271 | + * or deserialization property definitions. |
| 272 | + */ |
| 273 | + public <A extends Annotation> boolean isAnyAnnotated(Class<A> acls) { |
| 274 | + return getAnyAnnotation(acls) != null; |
| 275 | + } |
| 276 | + |
| 277 | + /** |
| 278 | + * Type determined from the primary member for the property being built. |
| 279 | + */ |
| 280 | + public JavaType getPrimaryType() { |
| 281 | + JavaType forSerializationType = null; |
| 282 | + if (forSerialization != null) { |
| 283 | + forSerializationType = forSerialization.getPrimaryType(); |
| 284 | + } |
| 285 | + |
| 286 | + JavaType forDeserializationType = null; |
| 287 | + if (forDeserialization != null) { |
| 288 | + forDeserializationType = forDeserialization.getPrimaryType(); |
| 289 | + } |
| 290 | + |
| 291 | + if (forSerializationType != null && forDeserializationType != null && forSerializationType != forDeserializationType) { |
| 292 | + throw new IllegalStateException("The property " + forSerialization.getName() + " has different types for serialization and deserialization: " |
| 293 | + + forSerializationType + " and " + forDeserializationType); |
| 294 | + } |
| 295 | + |
| 296 | + return forSerializationType != null ? forSerializationType : forDeserializationType; |
| 297 | + } |
| 298 | + |
| 299 | + private <A extends Annotation> A getAnyAnnotation(BeanPropertyDefinition prop, Class<A> acls) { |
| 300 | + if (prop == null) { |
| 301 | + return null; |
| 302 | + } |
| 303 | + |
| 304 | + if (prop.getField() != null) { |
| 305 | + return prop.getField().getAnnotation(acls); |
| 306 | + } |
| 307 | + if (prop.getGetter() != null) { |
| 308 | + return prop.getGetter().getAnnotation(acls); |
| 309 | + } |
| 310 | + if (prop.getSetter() != null) { |
| 311 | + return prop.getSetter().getAnnotation(acls); |
| 312 | + } |
| 313 | + |
| 314 | + return null; |
| 315 | + } |
| 316 | + } |
230 | 317 | }
|
0 commit comments