Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle optional in Jackson #10781

Merged
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 @@ -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");
fabienpuissant marked this conversation as resolved.
Show resolved Hide resolved
private static final JHipsterSource JACKSON_TEST_SOURCE = from("server/springboot/jackson/test");
private static final String WIRE_JACKSON_CONFIG = "wire/jackson/infrastructure/primary";

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/primary";
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,14 @@
package {{packageName}}.wire.jackson.infrastructure.primary;

import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
class JacksonConfiguration {

@Bean
Jdk8Module jdk8Module() {
return new Jdk8Module();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package {{packageName}}.wire.jackson.infrastructure.primary;

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 objectMapper;

@Test
void shouldHandleOptional() throws JsonProcessingException {
Optional<String> optional = Optional.of("test");
assertThat(objectMapper.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/primary"
| JacksonConfiguration.java |
Then I should have files in "src/test/java/tech/jhipster/chips/wire/jackson/infrastructure/primary"
| 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/primary"
| JacksonConfiguration.java |
Then I should have files in "src/test/java/tech/jhipster/chips/wire/jackson/infrastructure/primary"
| 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/primary"
| JacksonConfiguration.java |
Then I should have files in "src/test/java/tech/jhipster/chips/wire/jackson/infrastructure/primary"
| 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,10 @@ private JHipsterModuleAsserter assertMvcModule(JHipsterModule module) {
</dependency>
"""
)
.and()
.hasFile("src/main/java/tech/jhipster/jhlitest/wire/jackson/infrastructure/primary/JacksonConfiguration.java")
.and()
.hasFile("src/test/java/tech/jhipster/jhlitest/wire/jackson/infrastructure/primary/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,10 @@ void shouldBuildWebfluxNettyModule() {
"""
)
.and()
.hasFile("src/main/java/tech/jhipster/jhlitest/wire/jackson/infrastructure/primary/JacksonConfiguration.java")
.and()
.hasFile("src/test/java/tech/jhipster/jhlitest/wire/jackson/infrastructure/primary/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