-
-
Notifications
You must be signed in to change notification settings - Fork 216
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
handle optional in Jackson #10781
Conversation
@DamnClin I don't know what do you think about it |
1d56351
to
40d3759
Compare
Importing the starter-web leads to side effects (especially with Spring cloud), so I just import jackson-datatype-jdk8 |
7ace8bb
to
b49362a
Compare
This is probably the wrong module to add it so :). There should be a module with this dependency already on and his must be done here. We can't add the web starter in this module if it wasn't there since SpringBoot can be use for other needs (like batch), my bad on identifying the sweet spot |
@@ -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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove the changes from this file, they're not used anymore
b49362a
to
3ef9973
Compare
...h/jhipster/lite/generator/server/springboot/mvc/web/domain/SpringBootMvcsModulesFactory.java
Show resolved
Hide resolved
Optional<String> optional = Optional.of("test"); | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
objectMapper.registerModule(jacksonConfiguration.jdk8Module()); | ||
assertThat(objectMapper.writeValueAsString(optional)).isEqualTo("\"test\""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you're just testing the Jdk8Module
which is an external dependency, so there's no real reason to test it again.
What could be more interesting is an integration test where you inject the ObjectMapper provided by Spring Boot, and assert that it indeed correctly map Optional.
3ef9973
to
d253191
Compare
d253191
to
b7f47ca
Compare
c6e1383
to
5a5003e
Compare
To avoid errors, use default spring boot jackson configuration and enrich with jdk8Module |
class JacksonConfiguration { | ||
|
||
@Bean | ||
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just tested it, the following code is sufficient and more extensible (since it doesn't overwrite SpringBoot configuration):
@Bean
Jdk8Module jdk8Module() {
return new Jdk8Module();
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true xD
Kinda dark magic
@@ -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"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Subfolders of infrastructure
folder are either primary
or secondary
:
private static final String WIRE_JACKSON_CONFIG = "wire/jackson/infrastructure/config"; | |
private static final String WIRE_JACKSON_CONFIG = "wire/jackson/infrastructure/primary"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need the update the JacksonConfiguration.java content too:
package {{packageName}}.wire.jackson.infrastructure.config;
-> package {{packageName}}.wire.jackson.infrastructure.primary;
@@ -145,6 +148,11 @@ 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)") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually we test content of a file when it's a template and we want to check the dynamic content.
Here it's not needed since this is a static content handled in the template.
5a5003e
to
513701c
Compare
e8562aa
to
c303f06
Compare
create bean Jdk8Module and ObjectMapper Fix jhipster#10751
c303f06
to
c21b940
Compare
create bean Jdk8Module and ObjectMapper
Fix #10751