|
| 1 | +package it.aboutbits.springboot.toolbox.boot.type.swagger; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.type.SimpleType; |
| 4 | +import io.swagger.v3.core.converter.AnnotatedType; |
| 5 | +import io.swagger.v3.oas.models.media.Schema; |
| 6 | +import it.aboutbits.springboot.toolbox.persistence.identity.EntityId; |
| 7 | +import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil; |
| 8 | +import lombok.NonNull; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.springdoc.core.customizers.PropertyCustomizer; |
| 11 | +import org.springframework.stereotype.Component; |
| 12 | + |
| 13 | +import java.math.BigDecimal; |
| 14 | + |
| 15 | +@Slf4j |
| 16 | +@Component |
| 17 | +public class IdentityPropertyCustomizer implements PropertyCustomizer { |
| 18 | + @Override |
| 19 | + public Schema<?> customize(Schema property, AnnotatedType annotatedType) { |
| 20 | + var type = annotatedType.getType(); |
| 21 | + |
| 22 | + if (type instanceof SimpleType simpleType && EntityId.class.isAssignableFrom(simpleType.getRawClass())) { |
| 23 | + String displayName = null; |
| 24 | + |
| 25 | + var bindings = simpleType.getBindings(); |
| 26 | + var boundType = bindings.getBoundType(0); |
| 27 | + |
| 28 | + if (boundType == null) { |
| 29 | + var rawClass = simpleType.getRawClass(); |
| 30 | + displayName = resolveDisplayName(rawClass); |
| 31 | + } |
| 32 | + |
| 33 | + var constructor = RecordReflectionUtil.getCanonicalConstructor(simpleType.getRawClass()); |
| 34 | + |
| 35 | + var wrappedType = constructor.getParameters()[0].getType(); |
| 36 | + |
| 37 | + |
| 38 | + if (Short.class.isAssignableFrom(wrappedType)) { |
| 39 | + property.type("integer"); |
| 40 | + property.format(""); |
| 41 | + property.setDescription(displayName); |
| 42 | + property.setProperties(null); |
| 43 | + property.set$ref(null); |
| 44 | + return property; |
| 45 | + } |
| 46 | + if (Integer.class.isAssignableFrom(wrappedType)) { |
| 47 | + property.type("integer"); |
| 48 | + property.format("int32"); |
| 49 | + property.setDescription(displayName); |
| 50 | + property.setProperties(null); |
| 51 | + property.set$ref(null); |
| 52 | + return property; |
| 53 | + } |
| 54 | + if (Long.class.isAssignableFrom(wrappedType)) { |
| 55 | + property.type("integer"); |
| 56 | + property.format("int64"); |
| 57 | + property.setDescription(displayName); |
| 58 | + property.setProperties(null); |
| 59 | + property.set$ref(null); |
| 60 | + return property; |
| 61 | + } |
| 62 | + if (Float.class.isAssignableFrom(wrappedType)) { |
| 63 | + property.type("number"); |
| 64 | + property.format("float"); |
| 65 | + property.setDescription(displayName); |
| 66 | + property.setProperties(null); |
| 67 | + property.set$ref(null); |
| 68 | + return property; |
| 69 | + } |
| 70 | + if (Double.class.isAssignableFrom(wrappedType)) { |
| 71 | + property.type("number"); |
| 72 | + property.format("double"); |
| 73 | + property.setDescription(displayName); |
| 74 | + property.setProperties(null); |
| 75 | + property.set$ref(null); |
| 76 | + return property; |
| 77 | + } |
| 78 | + if (BigDecimal.class.isAssignableFrom(wrappedType)) { |
| 79 | + property.type("number"); |
| 80 | + property.format(""); |
| 81 | + property.setDescription(displayName); |
| 82 | + property.setProperties(null); |
| 83 | + property.set$ref(null); |
| 84 | + return property; |
| 85 | + } |
| 86 | + if (String.class.isAssignableFrom(wrappedType)) { |
| 87 | + property.type("string"); |
| 88 | + property.format(null); |
| 89 | + property.setDescription(displayName); |
| 90 | + property.setProperties(null); |
| 91 | + property.set$ref(null); |
| 92 | + return property; |
| 93 | + } |
| 94 | + log.warn("Property {} of type EntityId: Can not resolve parameter type!", property.getName()); |
| 95 | + } |
| 96 | + |
| 97 | + return property; |
| 98 | + } |
| 99 | + |
| 100 | + @NonNull |
| 101 | + private static String resolveDisplayName(Class<?> rawClass) { |
| 102 | + var parent = rawClass.getEnclosingClass(); |
| 103 | + if (parent != null) { |
| 104 | + return parent.getSimpleName() + "." + rawClass.getSimpleName(); |
| 105 | + } |
| 106 | + return rawClass.getSimpleName(); |
| 107 | + } |
| 108 | +} |
0 commit comments