|
| 1 | +package it.aboutbits.springboot.toolbox.swagger.customization.custom_type; |
| 2 | + |
| 3 | +import io.swagger.v3.oas.models.media.ArraySchema; |
| 4 | +import io.swagger.v3.oas.models.media.ObjectSchema; |
| 5 | +import io.swagger.v3.oas.models.parameters.Parameter; |
| 6 | +import it.aboutbits.springboot.toolbox.reflection.util.CustomTypeReflectionUtil; |
| 7 | +import it.aboutbits.springboot.toolbox.swagger.SwaggerMetaUtil; |
| 8 | +import it.aboutbits.springboot.toolbox.type.CustomType; |
| 9 | +import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal; |
| 10 | +import it.aboutbits.springboot.toolbox.type.identity.EntityId; |
| 11 | +import lombok.SneakyThrows; |
| 12 | +import lombok.extern.slf4j.Slf4j; |
| 13 | +import org.springdoc.core.customizers.ParameterCustomizer; |
| 14 | +import org.springframework.core.MethodParameter; |
| 15 | + |
| 16 | +import java.math.BigDecimal; |
| 17 | +import java.math.BigInteger; |
| 18 | +import java.util.Optional; |
| 19 | +import java.util.UUID; |
| 20 | + |
| 21 | +@Slf4j |
| 22 | +public class CustomTypeParameterCustomizer implements ParameterCustomizer { |
| 23 | + |
| 24 | + public Optional<Class<?>> getClassFromSchemaReference(String schemaRef) { |
| 25 | + try { |
| 26 | + var className = schemaRef.replace("#/components/schemas/", ""); |
| 27 | + return Optional.of(Class.forName(className)); |
| 28 | + } catch (ClassNotFoundException e) { |
| 29 | + // Log the error if needed |
| 30 | + return Optional.empty(); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + @SuppressWarnings("checkstyle:MethodLength") |
| 36 | + @Override |
| 37 | + @SneakyThrows(NoSuchMethodException.class) |
| 38 | + public Parameter customize(Parameter parameter, MethodParameter methodParameter) { |
| 39 | + if (parameter == null) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + String schemaRef = null; |
| 44 | + var property = parameter.getSchema(); |
| 45 | + |
| 46 | + if (parameter.getSchema() instanceof ArraySchema |
| 47 | + && parameter.getSchema().getItems() != null |
| 48 | + && parameter.getSchema().getItems().get$ref() != null |
| 49 | + ) { |
| 50 | + schemaRef = parameter.getSchema().getItems().get$ref(); |
| 51 | + property = parameter.getSchema().getItems(); |
| 52 | + } |
| 53 | + if (parameter.getSchema() instanceof ObjectSchema |
| 54 | + && parameter.getSchema().get$ref() != null |
| 55 | + ) { |
| 56 | + schemaRef = parameter.getSchema().getItems().get$ref(); |
| 57 | + } |
| 58 | + |
| 59 | + if (schemaRef == null) { |
| 60 | + return parameter; |
| 61 | + } |
| 62 | + |
| 63 | + var optionalClass = getClassFromSchemaReference(schemaRef); |
| 64 | + |
| 65 | + if (optionalClass.isEmpty()) { |
| 66 | + log.error("Can not resolve type for schema reference: {}", schemaRef); |
| 67 | + return parameter; // throw new RuntimeException(); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + var rawClass = optionalClass.get(); |
| 72 | + |
| 73 | + if (rawClass.getName().contains(".") && !rawClass.getName().startsWith("java.")) { |
| 74 | + property.setDescription(SwaggerMetaUtil.setOriginalTypeFqn( |
| 75 | + property.getDescription(), |
| 76 | + rawClass.getName() |
| 77 | + )); |
| 78 | + } |
| 79 | + |
| 80 | + if (CustomType.class.isAssignableFrom(rawClass)) { |
| 81 | + Class<?> wrappedType = null; |
| 82 | + if (!rawClass.equals(EntityId.class)) { |
| 83 | + wrappedType = CustomTypeReflectionUtil.getWrappedType( |
| 84 | + (Class<? extends CustomType<?>>) rawClass |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + var isIdentity = EntityId.class.isAssignableFrom(rawClass); |
| 89 | + |
| 90 | + var description = SwaggerMetaUtil.setIsIdentity(property.getDescription(), isIdentity); |
| 91 | + |
| 92 | + if (wrappedType == null) { |
| 93 | + return parameter; |
| 94 | + } |
| 95 | + |
| 96 | + if (Boolean.class.isAssignableFrom(wrappedType)) { |
| 97 | + property.type("boolean"); |
| 98 | + property.format(null); |
| 99 | + property.setDescription(description); |
| 100 | + property.setProperties(null); |
| 101 | + property.set$ref(null); |
| 102 | + return parameter; |
| 103 | + } |
| 104 | + if (Byte.class.isAssignableFrom(wrappedType)) { |
| 105 | + property.type("integer"); |
| 106 | + property.format(""); |
| 107 | + property.setDescription(description); |
| 108 | + property.setProperties(null); |
| 109 | + property.set$ref(null); |
| 110 | + return parameter; |
| 111 | + } |
| 112 | + if (Short.class.isAssignableFrom(wrappedType)) { |
| 113 | + property.type("integer"); |
| 114 | + property.format(""); |
| 115 | + property.setDescription(description); |
| 116 | + property.setProperties(null); |
| 117 | + property.set$ref(null); |
| 118 | + return parameter; |
| 119 | + } |
| 120 | + if (Integer.class.isAssignableFrom(wrappedType)) { |
| 121 | + property.type("integer"); |
| 122 | + property.format("int32"); |
| 123 | + property.setDescription(description); |
| 124 | + property.setProperties(null); |
| 125 | + property.set$ref(null); |
| 126 | + return parameter; |
| 127 | + } |
| 128 | + if (Long.class.isAssignableFrom(wrappedType)) { |
| 129 | + property.type("integer"); |
| 130 | + property.format("int64"); |
| 131 | + property.setDescription(description); |
| 132 | + property.setProperties(null); |
| 133 | + property.set$ref(null); |
| 134 | + return parameter; |
| 135 | + } |
| 136 | + if (BigInteger.class.isAssignableFrom(wrappedType)) { |
| 137 | + property.type("integer"); |
| 138 | + property.format(""); |
| 139 | + property.setDescription(description); |
| 140 | + property.setProperties(null); |
| 141 | + property.set$ref(null); |
| 142 | + return parameter; |
| 143 | + } |
| 144 | + if (Float.class.isAssignableFrom(wrappedType)) { |
| 145 | + property.type("number"); |
| 146 | + property.format("float"); |
| 147 | + property.setDescription(description); |
| 148 | + property.setProperties(null); |
| 149 | + property.set$ref(null); |
| 150 | + return parameter; |
| 151 | + } |
| 152 | + if (Double.class.isAssignableFrom(wrappedType)) { |
| 153 | + property.type("number"); |
| 154 | + property.format("double"); |
| 155 | + property.setDescription(description); |
| 156 | + property.setProperties(null); |
| 157 | + property.set$ref(null); |
| 158 | + return parameter; |
| 159 | + } |
| 160 | + if (BigDecimal.class.isAssignableFrom(wrappedType)) { |
| 161 | + property.type("number"); |
| 162 | + property.format(""); |
| 163 | + property.setDescription(description); |
| 164 | + property.setProperties(null); |
| 165 | + property.set$ref(null); |
| 166 | + return parameter; |
| 167 | + } |
| 168 | + if (ScaledBigDecimal.class.isAssignableFrom(wrappedType)) { |
| 169 | + property.type("number"); |
| 170 | + property.format(""); |
| 171 | + property.setDescription(description); |
| 172 | + property.setProperties(null); |
| 173 | + property.set$ref(null); |
| 174 | + return parameter; |
| 175 | + } |
| 176 | + if (String.class.isAssignableFrom(wrappedType)) { |
| 177 | + property.type("string"); |
| 178 | + property.format(null); |
| 179 | + property.setDescription(description); |
| 180 | + property.setProperties(null); |
| 181 | + property.set$ref(null); |
| 182 | + return parameter; |
| 183 | + } |
| 184 | + if (Character.class.isAssignableFrom(wrappedType)) { |
| 185 | + property.type("string"); |
| 186 | + property.format(null); |
| 187 | + property.minLength(1); |
| 188 | + property.maxLength(1); |
| 189 | + property.setDescription(description); |
| 190 | + property.setProperties(null); |
| 191 | + property.set$ref(null); |
| 192 | + return parameter; |
| 193 | + } |
| 194 | + if (UUID.class.isAssignableFrom(wrappedType)) { |
| 195 | + property.type("string"); |
| 196 | + property.format("uuid"); |
| 197 | + property.minLength(36); |
| 198 | + property.maxLength(36); |
| 199 | + property.setDescription(description); |
| 200 | + property.setProperties(null); |
| 201 | + property.set$ref(null); |
| 202 | + return parameter; |
| 203 | + } |
| 204 | + |
| 205 | + log.warn("Property {} of type WrappedValue: Can not resolve parameter type!", property.getName()); |
| 206 | + } |
| 207 | + |
| 208 | + return parameter; |
| 209 | + } |
| 210 | +} |
0 commit comments