-
Notifications
You must be signed in to change notification settings - Fork 170
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
Fix serialization of ZonedDateTime #1135
Changes from 3 commits
f9c4893
66c1bea
c2fdd0c
28b524b
d8a9994
38221b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.hubspot.jinjava.el.JinjavaInterpreterResolver; | ||
import com.hubspot.jinjava.el.JinjavaObjectUnwrapper; | ||
|
@@ -157,7 +158,8 @@ private JinjavaConfig(Builder builder) { | |
|
||
private ObjectMapper setupObjectMapper(@Nullable ObjectMapper objectMapper) { | ||
if (objectMapper == null) { | ||
objectMapper = new ObjectMapper(); | ||
objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if ObjectMapper is not null? Shouldn't you register JavaTimeModule in that case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's a good point. This will be addressed by the change Julia suggested. |
||
|
||
if (legacyOverrides.isUseSnakeCasePropertyNaming()) { | ||
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package com.hubspot.jinjava.lib.filter; | ||
|
||
import com.fasterxml.jackson.databind.node.POJONode; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaDoc; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaParam; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; | ||
import com.hubspot.jinjava.interpret.InvalidInputException; | ||
import com.hubspot.jinjava.interpret.InvalidReason; | ||
import com.hubspot.jinjava.interpret.JinjavaInterpreter; | ||
import com.hubspot.jinjava.objects.date.PyishDate; | ||
import java.util.Map; | ||
|
@@ -48,7 +50,16 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) | |
) { | ||
varStr = Objects.toString(var); | ||
} else { | ||
varStr = new POJONode(var).toPrettyString(); | ||
try { | ||
varStr = | ||
interpreter | ||
.getConfig() | ||
.getObjectMapper() | ||
Comment on lines
+55
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thoughts on registering the module here so we scope the change to the object mapper to the pretty print filter? I think my concern here is if changing the mapper on the config level will inadvertently change something else There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. I'll update. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think registering here is the right move. This will register the module on every call to the I need to look through the PR to figure out where the right place to do it would be. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated to remove module registration, but the remaining changes are still needed to support using the interpreter's config's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
.writerWithDefaultPrettyPrinter() | ||
.writeValueAsString(var); | ||
} catch (JsonProcessingException e) { | ||
throw new InvalidInputException(interpreter, this, InvalidReason.JSON_WRITE); | ||
} | ||
} | ||
|
||
return EscapeFilter.escapeHtmlEntities( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,7 @@ public void ppList() { | |
@Test | ||
public void ppObject() { | ||
MyClass myClass = new MyClass(); | ||
|
||
assertThat(f.filter(myClass, i)) | ||
.isEqualTo( | ||
String.format( | ||
|
@@ -73,7 +74,8 @@ public void ppObject() { | |
" "nestedClass" : {\n" + | ||
" "fooField" : "%s",\n" + | ||
" "barField" : %d\n" + | ||
" }\n" + | ||
" },\n" + | ||
" "date" : 1702252800.000000000\n" + | ||
"}){%% endraw %%}", | ||
myClass.getFoo(), | ||
myClass.getBar(), | ||
|
@@ -94,6 +96,10 @@ public int getBar() { | |
return 123; | ||
} | ||
|
||
public ZonedDateTime getDate() { | ||
return ZonedDateTime.of(2023, 12, 11, 0, 0, 0, 0, ZonedDateTime.now().getZone()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this method used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this tests that |
||
|
||
public MyNestedClass getNestedClass() { | ||
return new MyNestedClass(); | ||
} | ||
|
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.
Do you still need those POM update?
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.
Only for the test, but I'm going to move it into my upcoming
cos-renderer
PR