Skip to content

Commit

Permalink
Merge pull request #1175 from HubSpot/serialize-optional
Browse files Browse the repository at this point in the history
Use Jdk8Module to serialize Optional values
  • Loading branch information
jasmith-hs authored Apr 22, 2024
2 parents 024dde3 + 0632811 commit f3ab01a
Show file tree
Hide file tree
Showing 6 changed files with 43 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 @@ -9,6 +9,7 @@
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.google.common.annotations.Beta;
import com.hubspot.jinjava.interpret.DeferredValueException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
Expand Down Expand Up @@ -45,6 +46,7 @@ private static ObjectMapper getPyishObjectMapper() {
ObjectMapper mapper = new ObjectMapper(
new JsonFactoryBuilder().quoteChar('\'').build()
)
.registerModule(new Jdk8Module())
.registerModule(
new SimpleModule()
.setSerializerModifier(PyishBeanSerializerModifier.INSTANCE)
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 @@ -16,6 +16,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;

Expand Down Expand Up @@ -168,6 +169,11 @@ public void itSerializesToSnakeCaseWhenLegacyOverrideIsSet() {
}
}

@Test
public void itSerializesOptional() {
assertThat(PyishObjectMapper.getAsPyishString(Optional.of("foo"))).isEqualTo("'foo'");
}

static class Foo {

private final String bar;
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']");
assertThat(eagerResolveExpression("[optionally(false)]").toString())
.isEqualTo("[null]");
}

@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 f3ab01a

Please sign in to comment.