Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import it.aboutbits.springboot.toolbox.jackson.CustomTypeSerializer;
import it.aboutbits.springboot.toolbox.type.CustomType;
import it.aboutbits.springboot.toolbox.web.CustomTypePropertyEditor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -14,7 +13,6 @@

import java.util.Set;

@Slf4j
@Configuration
public class CustomTypeConfiguration {
@ControllerAdvice
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.oas.models.media.Schema;
import it.aboutbits.springboot.toolbox.swagger.SwaggerMetaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.customizers.PropertyCustomizer;

@Slf4j
public class CustomTypePropertyCustomizerForCodeGenerator implements PropertyCustomizer {
@Override
public Schema<?> customize(Schema property, AnnotatedType annotatedType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import it.aboutbits.springboot.toolbox._support.HttpTest;
import it.aboutbits.springboot.toolbox.autoconfiguration.mvc.body.BodyWithEntityId;
import it.aboutbits.springboot.toolbox.autoconfiguration.mvc.body.BodyWithEnumEntityId;
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeEnumTestModel;
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeTestModel;
import lombok.NonNull;
import org.junit.jupiter.api.Nested;
Expand Down Expand Up @@ -67,6 +69,51 @@ void emailAddressAsBody() throws Exception {
}
}

@Nested
class EnumEntityId {
@Test
void emailAddressAsPathVariable() throws Exception {
var value = new CustomTypeEnumTestModel.ID(CustomTypeEnumTestModel.CustomTypeEnum.ENUM_FIRST);

var resultAsString = performGetAndReturnResult(
String.format("/test/entity-id/CustomTypeEnumTestModel.ID/as-path-variable/%s", value)
);

var actual = objectMapper.readValue(resultAsString, CustomTypeEnumTestModel.ID.class);

assertThat(actual).isEqualTo(value);
}

@Test
void emailAddressAsRequestParameter() throws Exception {
var value = new CustomTypeEnumTestModel.ID(CustomTypeEnumTestModel.CustomTypeEnum.ENUM_OTHER);

var resultAsString = performGetAndReturnResult(
String.format("/test/entity-id/CustomTypeEnumTestModel.ID/as-request-parameter?value=%s", value)
);

var actual = objectMapper.readValue(resultAsString, CustomTypeEnumTestModel.ID.class);

assertThat(actual).isEqualTo(value);
}

@Test
void emailAddressAsBody() throws Exception {
var value = new BodyWithEnumEntityId(
new CustomTypeEnumTestModel.ID(CustomTypeEnumTestModel.CustomTypeEnum.ENUM_LAST)
);

var resultAsString = performPostAndReturnResult(
"/test/entity-id/CustomTypeEnumTestModel.ID/as-body",
value
);

var actual = objectMapper.readValue(resultAsString, BodyWithEnumEntityId.class);

assertThat(actual).isEqualTo(value);
}
}

private @NonNull String performGetAndReturnResult(@NonNull String url) throws Exception {
var requestBuilder = MockMvcRequestBuilders.get(url)
.contentType(MediaType.APPLICATION_JSON);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package it.aboutbits.springboot.toolbox.autoconfiguration.mvc.body;

import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeEnumTestModel;

public record BodyWithEnumEntityId(
CustomTypeEnumTestModel.ID enumEntityId
) {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package it.aboutbits.springboot.toolbox.autoconfiguration.mvc.controller;

import it.aboutbits.springboot.toolbox.autoconfiguration.mvc.body.BodyWithEntityId;
import it.aboutbits.springboot.toolbox.autoconfiguration.mvc.body.BodyWithEnumEntityId;
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeEnumTestModel;
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeTestModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -27,4 +29,19 @@ public CustomTypeTestModel.ID customTypeTestModelIdAsRequestParameter(@RequestPa
public BodyWithEntityId customTypeTestModelIdAsBody(@RequestBody BodyWithEntityId value) {
return value;
}

@GetMapping("/CustomTypeEnumTestModel.ID/as-path-variable/{value}")
public CustomTypeEnumTestModel.ID customTypeEnumTestModelIdAsPathVariable(@PathVariable CustomTypeEnumTestModel.ID value) {
return value;
}

@GetMapping("/CustomTypeEnumTestModel.ID/as-request-parameter")
public CustomTypeEnumTestModel.ID customTypeEnumTestModelIdAsRequestParameter(@RequestParam CustomTypeEnumTestModel.ID value) {
return value;
}

@PostMapping("/CustomTypeEnumTestModel.ID/as-body")
public BodyWithEnumEntityId customTypeEnumTestModelIdAsBody(@RequestBody BodyWithEnumEntityId value) {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

import it.aboutbits.springboot.toolbox._support.ApplicationTest;
import it.aboutbits.springboot.toolbox._support.WithPersistence;
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeEnumTestModel;
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeEnumTestModelRepository;
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeTestModel;
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeTestModelRepository;
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.ReferencedTestModel;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Random;

import static org.assertj.core.api.Assertions.assertThat;

@ApplicationTest
Expand All @@ -17,6 +21,9 @@ public class EntityIdJpaTest {
@Autowired
CustomTypeTestModelRepository repository;

@Autowired
CustomTypeEnumTestModelRepository repositoryEnum;

@Nested
class OwnId {
@Test
Expand Down Expand Up @@ -51,4 +58,26 @@ void inAndOut_shouldSucceed() {
.isEqualTo(savedItem);
}
}

@Nested
class EnumId {
@Test
void inAndOut_shouldSucceed() {
var values = CustomTypeEnumTestModel.CustomTypeEnum.values();

var item = new CustomTypeEnumTestModel();
item.setId(new CustomTypeEnumTestModel.ID(
values[new Random().nextInt(values.length)]
));

var savedItem = repositoryEnum.save(item);

var retrievedItem = repositoryEnum.findById(savedItem.getId());

assertThat(retrievedItem).isPresent()
.get()
.usingRecursiveComparison()
.isEqualTo(savedItem);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa;

import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.AutoRegisteredJavaType;
import it.aboutbits.springboot.toolbox.persistence.javatype.base.WrappedEnumJavaType;
import it.aboutbits.springboot.toolbox.type.identity.EntityId;
import it.aboutbits.springboot.toolbox.type.identity.Identified;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.JavaType;

@Entity
@Getter
@Setter
@Table(name = "custom_type_enum_test_model")
public class CustomTypeEnumTestModel implements Identified<CustomTypeEnumTestModel.ID> {
@Id
@JavaType(CustomTypeEnumTestModel.ID.JavaType.class)
private ID id;

public enum CustomTypeEnum {
ENUM_FIRST, ENUM_OTHER, ENUM_LAST
}

public record ID(
CustomTypeEnum value
) implements EntityId<CustomTypeEnum> {

@Override
public String toString() {
return value().name();
}

public static class JavaType extends WrappedEnumJavaType<ID> implements AutoRegisteredJavaType<ID> {
public JavaType() {
super(ID.class);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa;

import org.springframework.data.jpa.repository.JpaRepository;

public interface CustomTypeEnumTestModelRepository extends JpaRepository<CustomTypeEnumTestModel, CustomTypeEnumTestModel.ID> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
databaseChangeLog:
- changeSet:
author: Thomas Sapelza
id: 2025-08-29-create-custom-type-enum-testing-table
changes:
- createTable:
tableName: custom_type_enum_test_model
columns:
- column:
name: id
type: text
constraints:
nullable: false
primaryKey: true
3 changes: 3 additions & 0 deletions src/test/resources/db/changelog/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ databaseChangeLog:
- include:
file: 2024-09-26-create-query-transformer-testing-table.yml
relativeToChangelogFile: true
- include:
file: 2025-08-29-create-custom-type-enum-testing-table.yml
relativeToChangelogFile: true