-
Notifications
You must be signed in to change notification settings - Fork 0
Add support for the UUID type #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package it.aboutbits.springboot.toolbox.persistence.converter; | ||
|
|
||
| import jakarta.persistence.AttributeConverter; | ||
| import jakarta.persistence.Converter; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| @Converter | ||
| public class UUIDConverter implements AttributeConverter<UUID, String> { | ||
| @Override | ||
| public String convertToDatabaseColumn(UUID attribute) { | ||
| if (attribute == null) { | ||
| return null; | ||
| } | ||
|
|
||
| return attribute.toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public UUID convertToEntityAttribute(String dbData) { | ||
| if (dbData == null || dbData.isBlank()) { | ||
| return null; | ||
| } | ||
|
|
||
| return UUID.fromString(dbData); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package it.aboutbits.springboot.toolbox.persistence.javatype.base; | ||
|
|
||
| import it.aboutbits.springboot.toolbox.type.CustomType; | ||
| import lombok.SneakyThrows; | ||
| import org.hibernate.type.descriptor.WrapperOptions; | ||
| import org.hibernate.type.descriptor.java.AbstractClassJavaType; | ||
| import org.hibernate.type.descriptor.jdbc.JdbcType; | ||
| import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators; | ||
|
|
||
| import java.lang.reflect.Constructor; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.sql.Types; | ||
| import java.util.UUID; | ||
|
|
||
| public abstract class WrappedUUIDJavaType<T extends CustomType<UUID>> extends AbstractClassJavaType<T> { | ||
| private final transient Constructor<T> constructor; | ||
|
|
||
| protected WrappedUUIDJavaType(Class<T> type) { | ||
| super(type); | ||
|
|
||
| try { | ||
| this.constructor = type.getConstructor(UUID.class); | ||
| } catch (NoSuchMethodException e) { | ||
| throw new IllegalStateException("No method found for " + type.getName(), e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public JdbcType getRecommendedJdbcType(JdbcTypeIndicators indicators) { | ||
| return indicators.getTypeConfiguration() | ||
| .getJdbcTypeRegistry() | ||
| .getDescriptor(Types.OTHER); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Override | ||
| public <X> X unwrap(T id, Class<X> aClass, WrapperOptions wrapperOptions) { | ||
| var javaTypeClass = getJavaTypeClass(); | ||
|
|
||
| if (id == null) { | ||
| return null; | ||
| } | ||
| if (javaTypeClass.isAssignableFrom(aClass)) { | ||
| return (X) id; | ||
| } | ||
| if (UUID.class.isAssignableFrom(aClass)) { | ||
| return (X) id.value(); | ||
| } | ||
| if (String.class.isAssignableFrom(aClass)) { | ||
| return (X) id.value().toString(); | ||
| } | ||
| if (byte[].class.isAssignableFrom(aClass)) { | ||
| return (X) id.value().toString().getBytes(StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| throw unknownUnwrap(aClass); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @SneakyThrows({InstantiationException.class, IllegalAccessException.class, InvocationTargetException.class}) | ||
| @Override | ||
| public <X> T wrap(X value, WrapperOptions wrapperOptions) { | ||
| var clazz = getJavaTypeClass(); | ||
|
|
||
| if (value == null) { | ||
| return null; | ||
| } | ||
| if (clazz.isInstance(value)) { | ||
| return (T) value; | ||
| } | ||
|
|
||
| return switch (value) { | ||
| case UUID uuidValue -> constructor.newInstance(uuidValue); | ||
| case String uuidStringValue -> constructor.newInstance(UUID.fromString(uuidStringValue)); | ||
| case byte[] uuidBytesValue -> constructor.newInstance(UUID.fromString(new String( | ||
| uuidBytesValue, | ||
| StandardCharsets.UTF_8 | ||
| ))); | ||
| default -> throw unknownWrap(value.getClass()); | ||
| }; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
|
|
||
| import java.math.BigDecimal; | ||
| import java.math.BigInteger; | ||
| import java.util.UUID; | ||
|
|
||
| @Slf4j | ||
| public class CustomTypePropertyCustomizer implements PropertyCustomizer { | ||
|
|
@@ -91,7 +92,7 @@ public Schema<?> customize(Schema property, AnnotatedType annotatedType) { | |
| } | ||
| if (BigInteger.class.isAssignableFrom(wrappedType)) { | ||
| property.type("integer"); | ||
| property.format("int64"); | ||
| property.format(""); | ||
|
Comment on lines
-94
to
+95
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A BigDecimal can be bigger than
ThoSap marked this conversation as resolved.
|
||
| property.setDescription(description); | ||
| property.setProperties(null); | ||
| property.set$ref(null); | ||
|
|
@@ -147,6 +148,16 @@ public Schema<?> customize(Schema property, AnnotatedType annotatedType) { | |
| property.set$ref(null); | ||
| return property; | ||
| } | ||
| if (UUID.class.isAssignableFrom(wrappedType)) { | ||
| property.type("string"); | ||
| property.format("uuid"); | ||
|
Comment on lines
+152
to
+153
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The OpenAPI specification has a built-in format for UUID, see https://swagger.io/docs/specification/v3_0/data-models/data-types/#strings |
||
| property.minLength(36); | ||
| property.maxLength(36); | ||
| property.setDescription(description); | ||
| property.setProperties(null); | ||
| property.set$ref(null); | ||
| return property; | ||
| } | ||
|
|
||
| log.warn("Property {} of type WrappedValue: Can not resolve parameter type!", property.getName()); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package it.aboutbits.springboot.toolbox.autoconfiguration.mvc.body; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public record BodyWithUUID( | ||
| UUID uuid | ||
| ) { | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.