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

Fix serialization of ZonedDateTime #1135

Merged
merged 6 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
Copy link
Collaborator

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?

Copy link
Contributor Author

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

<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.14.0</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -147,6 +152,10 @@
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>ch.obermuhlner</groupId>
<artifactId>big-math</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.hubspot.jinjava.lib.filter;

import com.fasterxml.jackson.databind.node.POJONode;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
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;
Expand Down Expand Up @@ -48,7 +51,17 @@ 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
Copy link

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I'll update.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 PrettyPrintFilter.

I need to look through the PR to figure out where the right place to do it would be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 ObjectMapper.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

.registerModule(new JavaTimeModule())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean we register the module every time the function is called? Is this an expensive operation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I raised the same question on an above thread.

.writerWithDefaultPrettyPrinter()
.writeValueAsString(var);
} catch (JsonProcessingException e) {
throw new InvalidInputException(interpreter, this, InvalidReason.JSON_WRITE);
}
}

return EscapeFilter.escapeHtmlEntities(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public void ppList() {
@Test
public void ppObject() {
MyClass myClass = new MyClass();

assertThat(f.filter(myClass, i))
.isEqualTo(
String.format(
Expand All @@ -73,7 +74,8 @@ public void ppObject() {
" &quot;nestedClass&quot; : {\n" +
" &quot;fooField&quot; : &quot;%s&quot;,\n" +
" &quot;barField&quot; : %d\n" +
" }\n" +
" },\n" +
" &quot;date&quot; : 1702252800.000000000\n" +
"}){%% endraw %%}",
myClass.getFoo(),
myClass.getBar(),
Expand All @@ -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());
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this method used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this tests that ZonedDateTime (the problem type) is being serialized successfully.


public MyNestedClass getNestedClass() {
return new MyNestedClass();
}
Expand Down