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 15, 2024
1 parent bc3f04c commit c6e1383
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public class SpringBootMvcsModulesFactory {
private static final JHipsterSource MAIN_SOURCE = SOURCE.append("main");
private static final JHipsterSource TEST_SOURCE = SOURCE.append("test");

private static final JHipsterSource JACKSON_MAIN_SOURCE = from("server/springboot/jackson/main");
private static final JHipsterSource JACKSON_TEST_SOURCE = from("server/springboot/jackson/test");
private static final String WIRE_JACKSON_CONFIG = "wire/jackson/infrastructure/config";

private static final GroupId SPRING_BOOT_GROUP = groupId("org.springframework.boot");
private static final ArtifactId STARTER_WEB_ARTIFACT_ID = artifactId("spring-boot-starter-web");

Expand Down Expand Up @@ -84,11 +88,14 @@ private JHipsterModuleBuilder springMvcBuilder(JHipsterModuleProperties properti
.and()
.springMainProperties()
.set(SERVER_PORT, propertyValue(properties.serverPort().get()))
.set(propertyKey("spring.jackson.default-property-inclusion"), propertyValue("non_absent"))
.and()
.springTestProperties()
.set(SERVER_PORT, propertyValue(0))
.and()
.files()
.add(JACKSON_MAIN_SOURCE.append(WIRE_JACKSON_CONFIG).template("JacksonConfiguration.java"), toSrcMainJava().append(packagePath).append(WIRE_JACKSON_CONFIG).append("JacksonConfiguration.java"))
.add(JACKSON_TEST_SOURCE.append(WIRE_JACKSON_CONFIG).template("JacksonConfigurationIT.java"), toSrcTestJava().append(packagePath).append(WIRE_JACKSON_CONFIG).append("JacksonConfigurationIT.java"))
.add(SOURCE.file("resources/404.html"), to("src/main/resources/public/error/404.html"))
.batch(MAIN_SOURCE.append(CORS), mainDestination.append(CORS_PRIMARY))
.addTemplate("CorsFilterConfiguration.java")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
public class SpringBootWebfluxModuleFactory {

private static final JHipsterSource SOURCE = from("server/springboot/webflux/web");

private static final JHipsterSource JACKSON_MAIN_SOURCE = from("server/springboot/jackson/main");
private static final JHipsterSource JACKSON_TEST_SOURCE = from("server/springboot/jackson/test");
private static final String WIRE_JACKSON_CONFIG = "wire/jackson/infrastructure/config";
private static final PropertyKey SERVER_PORT = propertyKey("server.port");

private static final GroupId SPRING_GROUP = groupId("org.springframework.boot");
Expand All @@ -39,11 +41,14 @@ public JHipsterModule buildNettyModule(JHipsterModuleProperties properties) {
.and()
.springMainProperties()
.set(SERVER_PORT, propertyValue(properties.serverPort().get()))
.set(propertyKey("spring.jackson.default-property-inclusion"), propertyValue("non_absent"))
.and()
.springTestProperties()
.set(SERVER_PORT, propertyValue(0))
.and()
.files()
.add(JACKSON_MAIN_SOURCE.append(WIRE_JACKSON_CONFIG).template("JacksonConfiguration.java"), toSrcMainJava().append(packagePath).append(WIRE_JACKSON_CONFIG).append("JacksonConfiguration.java"))
.add(JACKSON_TEST_SOURCE.append(WIRE_JACKSON_CONFIG).template("JacksonConfigurationIT.java"), toSrcTestJava().append(packagePath).append(WIRE_JACKSON_CONFIG).append("JacksonConfigurationIT.java"))
.batch(SOURCE.append("main"), toSrcMainJava().append(packagePath).append(EXCEPTION_PRIMARY))
.addTemplate("FieldErrorDTO.java")
.addTemplate("HeaderUtil.java")
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package {{packageName}}.wire.jackson.infrastructure.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

@Configuration
class JacksonConfiguration {
@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
return builder.createXmlMapper(false).build();
}

@Bean
public ObjectMapper objectMapperOptional() {
return new ObjectMapper().registerModule(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}}.IntegrationTest;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

@IntegrationTest
class JacksonConfigurationIT {
@Autowired
private ObjectMapper objectMapperOptional;
@Test
void shouldHandleOptional() throws JsonProcessingException {
Optional<String> optional = Optional.of("test");
assertThat(objectMapperOptional.writeValueAsString(optional)).isEqualTo("\"test\"");
}
}
8 changes: 8 additions & 0 deletions src/test/features/server/springboot/spring-mvc.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Feature: Spring MVC
| spring-boot-mvc-empty |
| spring-boot-tomcat |
Then I should have "<artifactId>spring-boot-starter-web</artifactId>" in "pom.xml"
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"
| JacksonConfigurationIT.java |

Scenario: Should apply spring mvc undertow module
When I apply modules to default project
Expand All @@ -15,3 +19,7 @@ Feature: Spring MVC
| spring-boot-mvc-empty |
| spring-boot-undertow |
Then I should have "<artifactId>spring-boot-starter-undertow</artifactId>" in "pom.xml"
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"
| JacksonConfigurationIT.java |
4 changes: 4 additions & 0 deletions src/test/features/server/springboot/webflux.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ Feature: Webflux module
Then I should have "<artifactId>spring-boot-starter-webflux</artifactId>" in "pom.xml"
Then I should have files in "src/main/java/tech/jhipster/chips/shared/error/infrastructure/primary"
| FieldErrorDTO.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"
| JacksonConfigurationIT.java |
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ private JHipsterModuleAsserter assertMvcModule(JHipsterModule module) {
"""
server:
port: 9000
spring:
jackson:
default-property-inclusion: non_absent
"""
)
.and()
Expand Down Expand Up @@ -145,6 +148,12 @@ private JHipsterModuleAsserter assertMvcModule(JHipsterModule module) {
</dependency>
"""
)
.and()
.hasFile("src/main/java/tech/jhipster/jhlitest/wire/jackson/infrastructure/config/JacksonConfiguration.java")
.containing("public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder)")
.containing("public ObjectMapper objectMapperOptional()")
.and()
.hasFile("src/test/java/tech/jhipster/jhlitest/wire/jackson/infrastructure/config/JacksonConfigurationIT.java")
.and();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ void shouldBuildWebfluxNettyModule() {
"""
server:
port: 9000
spring:
jackson:
default-property-inclusion: non_absent
"""
)
.and()
Expand All @@ -67,6 +70,12 @@ void shouldBuildWebfluxNettyModule() {
"""
)
.and()
.hasFile("src/main/java/tech/jhipster/jhlitest/wire/jackson/infrastructure/config/JacksonConfiguration.java")
.containing("public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder)")
.containing("public ObjectMapper objectMapperOptional()")
.and()
.hasFile("src/test/java/tech/jhipster/jhlitest/wire/jackson/infrastructure/config/JacksonConfigurationIT.java")
.and()
.hasPrefixedFiles("src/main/java/tech/jhipster/jhlitest/shared/error/infrastructure/primary", "HeaderUtil.java", "FieldErrorDTO.java")
.hasPrefixedFiles(
"src/test/java/tech/jhipster/jhlitest/shared/error/infrastructure/primary",
Expand Down

0 comments on commit c6e1383

Please sign in to comment.