Skip to content

Commit

Permalink
Use Jdk8Module to serialize Optional values
Browse files Browse the repository at this point in the history
  • Loading branch information
jasmith-hs committed Apr 22, 2024
1 parent 024dde3 commit f8792d1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>2.14.0</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -150,6 +155,10 @@
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
<groupId>ch.obermuhlner</groupId>
<artifactId>big-math</artifactId>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/hubspot/jinjava/JinjavaConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.google.common.collect.ImmutableSet;
import com.hubspot.jinjava.el.JinjavaInterpreterResolver;
import com.hubspot.jinjava.el.JinjavaObjectUnwrapper;
Expand Down Expand Up @@ -158,7 +159,7 @@ private JinjavaConfig(Builder builder) {

private ObjectMapper setupObjectMapper(@Nullable ObjectMapper objectMapper) {
if (objectMapper == null) {
objectMapper = new ObjectMapper();
objectMapper = new ObjectMapper().registerModule(new Jdk8Module());
if (legacyOverrides.isUseSnakeCasePropertyNaming()) {
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.google.common.annotations.Beta;
import com.hubspot.jinjava.interpret.DeferredValueException;
import com.hubspot.jinjava.objects.PyWrapper;
Expand All @@ -19,6 +20,7 @@ public interface PyishSerializable extends PyWrapper {
ObjectWriter SELF_WRITER = new ObjectMapper(
new JsonFactoryBuilder().quoteChar('\'').build()
)
.registerModule(new Jdk8Module())
.writer(PyishPrettyPrinter.INSTANCE)
.with(PyishCharacterEscapes.INSTANCE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.After;
Expand Down Expand Up @@ -93,6 +94,15 @@ private JinjavaInterpreter getInterpreter(boolean evaluateMapKeys) throws Except
this.getClass().getDeclaredMethod("sleeper")
)
);
jinjava
.getGlobalContext()
.registerFunction(
new ELFunctionDefinition(
"",
"optionally",
this.getClass().getDeclaredMethod("optionally", boolean.class)
)
);
interpreter = new JinjavaInterpreter(jinjava.newInterpreter());
context = interpreter.getContext();
context.put("deferred", DeferredValue.instance());
Expand Down Expand Up @@ -124,6 +134,14 @@ public void itResolvesDeferredBoolean() {
assertThat(interpreter.resolveELExpression(partiallyResolved, 1)).isEqualTo(true);
}

@Test
public void itSerializesNestedOptional() {
assertThat(eagerResolveExpression("[optionally(true)]").toString())
.isEqualTo("['1'|allow_snake_case]");
assertThat(eagerResolveExpression("[optionally(false)]").toString())
.isEqualTo("[null|allow_snake_case]");
}

@Test
public void itResolvesDeferredList() {
context.put("foo", "foo_val");
Expand Down Expand Up @@ -877,6 +895,10 @@ public static long sleeper() throws InterruptedException {
return sleepTime;
}

public static Optional<String> optionally(boolean hasValue) {
return Optional.of(hasValue).filter(Boolean::booleanValue).map(ignored -> "1");
}

private static class Foo {

private final String bar;
Expand Down

0 comments on commit f8792d1

Please sign in to comment.