From b49362af9f3fd106acee9e4c4707b20c6a626c4c Mon Sep 17 00:00:00 2001 From: fabienpuissant Date: Fri, 6 Sep 2024 12:50:36 +0200 Subject: [PATCH] handle optional in Jackson create bean Jdk8Module and ObjectMapper Fix #10751 --- .../domain/SpringBootCoreModuleFactory.java | 5 ++++ src/main/resources/config/application.yml | 2 +- .../resources/generator/dependencies/pom.xml | 6 +++++ .../config/JacksonConfiguration.java.mustache | 15 ++++++++++++ .../JacksonConfigurationTest.java.mustache | 23 +++++++++++++++++++ .../springboot/spring-boot-core.feature | 4 ++++ .../SpringBootCoreModuleFactoryTest.java | 15 ++++++++++++ 7 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/generator/server/springboot/core/main/wire/jackson/infrastructure/config/JacksonConfiguration.java.mustache create mode 100644 src/main/resources/generator/server/springboot/core/test/wire/jackson/infrastructure/config/JacksonConfigurationTest.java.mustache diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/core/domain/SpringBootCoreModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/core/domain/SpringBootCoreModuleFactory.java index 4a6d1e1ef47..38c85554ee5 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/core/domain/SpringBootCoreModuleFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/core/domain/SpringBootCoreModuleFactory.java @@ -35,6 +35,7 @@ public class SpringBootCoreModuleFactory { private static final JHipsterDestination MAIN_RESOURCE_DESTINATION = to("src/main/resources"); private static final JHipsterDestination TEST_RESOURCES_DESTINATION = to("src/test/resources"); + private static final String WIRE_JACKSON_CONFIG = "wire/jackson/infrastructure/config"; public JHipsterModule buildModule(JHipsterModuleProperties properties) { Assert.notNull("properties", properties); @@ -59,6 +60,7 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) { .addDependency(SPRING_BOOT_GROUP, artifactId("spring-boot-starter")) .addDependency(springBootConfigurationProcessor()) .addDependency(groupId("org.apache.commons"), artifactId("commons-lang3")) + .addDependency(groupId("com.fasterxml.jackson.datatype"), artifactId("jackson-datatype-jdk8")) .addDependency(springBootTest()) .and() .mavenPlugins() @@ -73,12 +75,15 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) { .add(MAIN_SOURCE.template("ApplicationStartupTraces.java"), toSrcMainJava().append(packagePath).append("ApplicationStartupTraces.java")) .add(TEST_SOURCE.template("IntegrationTest.java"), testDestination.append("IntegrationTest.java")) .add(MAIN_SOURCE.template("logback-spring.xml"), MAIN_RESOURCE_DESTINATION.append("logback-spring.xml")) + .add(MAIN_SOURCE.append(WIRE_JACKSON_CONFIG).template("JacksonConfiguration.java"), toSrcMainJava().append(packagePath).append(WIRE_JACKSON_CONFIG).append("JacksonConfiguration.java")) + .add(TEST_SOURCE.append(WIRE_JACKSON_CONFIG).template("JacksonConfigurationTest.java"), toSrcTestJava().append(packagePath).append(WIRE_JACKSON_CONFIG).append("JacksonConfigurationTest.java")) .add(TEST_SOURCE.template("logback.xml"), TEST_RESOURCES_DESTINATION.append("logback.xml")) .add(TEST_SOURCE.template("ApplicationStartupTracesTest.java"), toSrcTestJava().append(packagePath).append("ApplicationStartupTracesTest.java")) .and() .springMainProperties() .set(propertyKey("spring.application.name"), propertyValue(baseName)) .set(propertyKey(basePackageLoggingLevel), propertyValue("INFO")) + .set(propertyKey("spring.jackson.default-property-inclusion"), propertyValue("non_absent")) .and() .springLocalProperties() .set(propertyKey(basePackageLoggingLevel), propertyValue("DEBUG")) diff --git a/src/main/resources/config/application.yml b/src/main/resources/config/application.yml index 364a4bbc1cf..eac327a21a5 100644 --- a/src/main/resources/config/application.yml +++ b/src/main/resources/config/application.yml @@ -80,6 +80,6 @@ spring: virtual: enabled: true jackson: - default-property-inclusion: non_null + default-property-inclusion: non_absent application: name: jhlite diff --git a/src/main/resources/generator/dependencies/pom.xml b/src/main/resources/generator/dependencies/pom.xml index c3fa970823c..dccca8af1e5 100644 --- a/src/main/resources/generator/dependencies/pom.xml +++ b/src/main/resources/generator/dependencies/pom.xml @@ -28,6 +28,7 @@ 4.29.2 0.10.2 0.12.6 + 2.17.2 3.17.0 3.10.8 3.3.0 @@ -274,6 +275,11 @@ jjwt-jackson ${json-web-token.version} + + com.fasterxml.jackson.datatype + jackson-datatype-jdk8 + ${jackson-datatype-jdk8.version} + org.apache.commons commons-lang3 diff --git a/src/main/resources/generator/server/springboot/core/main/wire/jackson/infrastructure/config/JacksonConfiguration.java.mustache b/src/main/resources/generator/server/springboot/core/main/wire/jackson/infrastructure/config/JacksonConfiguration.java.mustache new file mode 100644 index 00000000000..42001141219 --- /dev/null +++ b/src/main/resources/generator/server/springboot/core/main/wire/jackson/infrastructure/config/JacksonConfiguration.java.mustache @@ -0,0 +1,15 @@ +package {{packageName}}.wire.jackson.infrastructure.config; + +import com.fasterxml.jackson.databind.Module; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +class JacksonConfiguration { + + @Bean + public Module jdk8Module() { + return new Jdk8Module(); + } +} diff --git a/src/main/resources/generator/server/springboot/core/test/wire/jackson/infrastructure/config/JacksonConfigurationTest.java.mustache b/src/main/resources/generator/server/springboot/core/test/wire/jackson/infrastructure/config/JacksonConfigurationTest.java.mustache new file mode 100644 index 00000000000..0149f7a3a9e --- /dev/null +++ b/src/main/resources/generator/server/springboot/core/test/wire/jackson/infrastructure/config/JacksonConfigurationTest.java.mustache @@ -0,0 +1,23 @@ +package {{packageName}}.wire.jackson.infrastructure.config; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import {{packageName}}.UnitTest; +import java.util.Optional; +import org.junit.jupiter.api.Test; + +@UnitTest +class JacksonConfigurationTest { + + private final JacksonConfiguration jacksonConfiguration = new JacksonConfiguration(); + + @Test + void shouldHandleOptional() throws JsonProcessingException { + Optional optional = Optional.of("test"); + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.registerModule(jacksonConfiguration.jdk8Module()); + assertThat(objectMapper.writeValueAsString(optional)).isEqualTo("\"test\""); + } +} diff --git a/src/test/features/server/springboot/spring-boot-core.feature b/src/test/features/server/springboot/spring-boot-core.feature index 4935c29f3db..b74a42a9e36 100644 --- a/src/test/features/server/springboot/spring-boot-core.feature +++ b/src/test/features/server/springboot/spring-boot-core.feature @@ -6,6 +6,10 @@ Feature: Spring boot core | baseName | jhipster | Then I should have files in "src/main/java/tech/jhipster/chips" | JhipsterApp.java | + Then I should have files in "src/main/java/tech/jhipster/chips/wire/jackson/infrastructure/config" + | JacksonConfiguration.java | + Then I should have files in "src/test/java/tech/jhipster/chips/wire/jackson/infrastructure/config" + | JacksonConfigurationTest.java | Scenario: Should handle application configuration with properties format When I apply "spring-boot" module to default project with maven file diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/core/domain/SpringBootCoreModuleFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/core/domain/SpringBootCoreModuleFactoryTest.java index 714f95174dd..1ae1b61fb9d 100644 --- a/src/test/java/tech/jhipster/lite/generator/server/springboot/core/domain/SpringBootCoreModuleFactoryTest.java +++ b/src/test/java/tech/jhipster/lite/generator/server/springboot/core/domain/SpringBootCoreModuleFactoryTest.java @@ -37,6 +37,14 @@ void shouldBuildModuleOnProjectWithoutDefaultGoal() { """ ) + .containing( + """ + + com.fasterxml.jackson.datatype + jackson-datatype-jdk8 + + """ + ) .containing( """ @@ -156,6 +164,8 @@ void shouldBuildModuleOnProjectWithoutDefaultGoal() { spring: application: name: Myapp + jackson: + default-property-inclusion: non_absent """ ) .and() @@ -185,6 +195,11 @@ void shouldBuildModuleOnProjectWithoutDefaultGoal() { """ ) .and() + .hasFile("src/main/java/tech/jhipster/jhlitest/wire/jackson/infrastructure/config/JacksonConfiguration.java") + .containing("public Module jdk8Module()") + .and() + .hasFile("src/test/java/tech/jhipster/jhlitest/wire/jackson/infrastructure/config/JacksonConfigurationTest.java") + .and() .hasFiles("src/test/resources/logback.xml", "src/main/resources/logback-spring.xml"); }