diff --git a/contentgrid-spring-data-rest/src/main/java/com/contentgrid/spring/data/rest/webmvc/blueprint/AttributeRepresentationModelAssembler.java b/contentgrid-spring-data-rest/src/main/java/com/contentgrid/spring/data/rest/webmvc/blueprint/AttributeRepresentationModelAssembler.java index 4c4eee1b..ba91ad98 100644 --- a/contentgrid-spring-data-rest/src/main/java/com/contentgrid/spring/data/rest/webmvc/blueprint/AttributeRepresentationModelAssembler.java +++ b/contentgrid-spring-data-rest/src/main/java/com/contentgrid/spring/data/rest/webmvc/blueprint/AttributeRepresentationModelAssembler.java @@ -12,21 +12,12 @@ import java.util.Optional; import lombok.RequiredArgsConstructor; import org.springframework.context.MessageSourceResolvable; -import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.data.rest.webmvc.RootResourceInformation; -import org.springframework.hateoas.mediatype.InputTypeFactory; import org.springframework.hateoas.mediatype.MessageResolver; @RequiredArgsConstructor public class AttributeRepresentationModelAssembler { - private static final InputTypeFactory INPUT_TYPE_FACTORY; - - static { - INPUT_TYPE_FACTORY = SpringFactoriesLoader.loadFactories(InputTypeFactory.class, - AttributeRepresentationModelAssembler.class.getClassLoader()).get(0); - } - private final CollectionFiltersMapping collectionFiltersMapping; private final MessageResolver messageResolver; @@ -93,12 +84,11 @@ public Optional toModel(RootResourceInformation in } private String getType(Property property) { - // TODO: How to distinguish between decimals and integers? - var type = INPUT_TYPE_FACTORY.getInputType(property.getTypeInformation().getType()); + var type = DataType.from(property.getTypeInformation().getType()); if (type == null && property.findAnnotation(Embedded.class).isPresent()) { - type = "object"; + type = DataType.OBJECT; } - return type; + return type == null ? null : type.name().toLowerCase(); } private String readDescription(RootResourceInformation information, List properties) { diff --git a/contentgrid-spring-data-rest/src/main/java/com/contentgrid/spring/data/rest/webmvc/blueprint/DataType.java b/contentgrid-spring-data-rest/src/main/java/com/contentgrid/spring/data/rest/webmvc/blueprint/DataType.java new file mode 100644 index 00000000..b53ed3e9 --- /dev/null +++ b/contentgrid-spring-data-rest/src/main/java/com/contentgrid/spring/data/rest/webmvc/blueprint/DataType.java @@ -0,0 +1,55 @@ +package com.contentgrid.spring.data.rest.webmvc.blueprint; + +import java.math.BigDecimal; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZonedDateTime; +import java.util.Collection; +import java.util.List; +import java.util.UUID; +import org.springframework.lang.Nullable; + +public enum DataType { + STRING, + LONG, + DOUBLE, + BOOLEAN, + DATETIME, + OBJECT; + + private static final Collection> STRING_TYPES = List.of(String.class, UUID.class); + private static final Collection> LONG_TYPES = List.of(int.class, long.class, short.class, + Integer.class, Long.class, Short.class); + private static final Collection> DOUBLE_TYPES = List.of(float.class, double.class, + Float.class, Double.class, BigDecimal.class); + private static final Collection> BOOLEAN_TYPES = List.of(boolean.class, Boolean.class); + private static final Collection> DATETIME_TYPES = List.of(Instant.class, LocalDateTime.class, + OffsetDateTime.class, ZonedDateTime.class); + + @Nullable + public static DataType from(Class type) { + + if (STRING_TYPES.contains(type)) { + return STRING; + } + + if (LONG_TYPES.contains(type)) { + return LONG; + } + + if (DOUBLE_TYPES.contains(type)) { + return DOUBLE; + } + + if (BOOLEAN_TYPES.contains(type)) { + return BOOLEAN; + } + + if (DATETIME_TYPES.contains(type)) { + return DATETIME; + } + + return null; + } +}