Skip to content

Commit

Permalink
handle optional in Jackson
Browse files Browse the repository at this point in the history
create bean Jdk8Module and ObjectMapper

Fix #10751
  • Loading branch information
fabienpuissant committed Sep 8, 2024
1 parent bc3f04c commit b49362a
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()
Expand All @@ -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"))
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ spring:
virtual:
enabled: true
jackson:
default-property-inclusion: non_null
default-property-inclusion: non_absent
application:
name: jhlite
6 changes: 6 additions & 0 deletions src/main/resources/generator/dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<liquibase.version>4.29.2</liquibase.version>
<reflections.version>0.10.2</reflections.version>
<json-web-token.version>0.12.6</json-web-token.version>
<jackson-datatype-jdk8.version>2.17.2</jackson-datatype-jdk8.version>
<commons-lang3.version>3.17.0</commons-lang3.version>
<ehcache.version>3.10.8</ehcache.version>
<thymeleaf-layout-dialect.version>3.3.0</thymeleaf-layout-dialect.version>
Expand Down Expand Up @@ -274,6 +275,11 @@
<artifactId>jjwt-jackson</artifactId>
<version>${json-web-token.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>${jackson-datatype-jdk8.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<String> optional = Optional.of("test");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(jacksonConfiguration.jdk8Module());
assertThat(objectMapper.writeValueAsString(optional)).isEqualTo("\"test\"");
}
}
4 changes: 4 additions & 0 deletions src/test/features/server/springboot/spring-boot-core.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ void shouldBuildModuleOnProjectWithoutDefaultGoal() {
</dependency>
"""
)
.containing(
"""
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
"""
)
.containing(
"""
<dependency>
Expand Down Expand Up @@ -156,6 +164,8 @@ void shouldBuildModuleOnProjectWithoutDefaultGoal() {
spring:
application:
name: Myapp
jackson:
default-property-inclusion: non_absent
"""
)
.and()
Expand Down Expand Up @@ -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");
}

Expand Down

0 comments on commit b49362a

Please sign in to comment.