Skip to content

Commit

Permalink
Also make default ObjectMapper for ToJsonFilter use snake_properties …
Browse files Browse the repository at this point in the history
…if legacy override is set
  • Loading branch information
jasmith-hs committed Mar 16, 2023
1 parent 47fd189 commit 5cb42f5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
16 changes: 14 additions & 2 deletions src/main/java/com/hubspot/jinjava/JinjavaConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.hubspot.jinjava.lib.fn.Functions.DEFAULT_RANGE_LIMIT;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.hubspot.jinjava.el.JinjavaInterpreterResolver;
import com.hubspot.jinjava.el.JinjavaNodePreProcessor;
import com.hubspot.jinjava.el.JinjavaObjectUnwrapper;
Expand All @@ -44,6 +45,7 @@
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import javax.annotation.Nullable;
import javax.el.ELResolver;

public class JinjavaConfig {
Expand Down Expand Up @@ -135,11 +137,21 @@ private JinjavaConfig(Builder builder) {
legacyOverrides = builder.legacyOverrides;
dateTimeProvider = builder.dateTimeProvider;
enablePreciseDivideFilter = builder.enablePreciseDivideFilter;
objectMapper = builder.objectMapper;
objectMapper = setupObjectMapper(builder.objectMapper);
objectUnwrapper = builder.objectUnwrapper;
nodePreProcessor = builder.nodePreProcessor;
}

private ObjectMapper setupObjectMapper(@Nullable ObjectMapper objectMapper) {
if (objectMapper == null) {
objectMapper = new ObjectMapper();
if (legacyOverrides.isUseSnakeCasePropertyNaming()) {
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
}
}
return objectMapper;
}

public Charset getCharset() {
return charset;
}
Expand Down Expand Up @@ -298,7 +310,7 @@ public static class Builder {
private ExecutionMode executionMode = DefaultExecutionMode.instance();
private LegacyOverrides legacyOverrides = LegacyOverrides.NONE;
private boolean enablePreciseDivideFilter = false;
private ObjectMapper objectMapper = new ObjectMapper();
private ObjectMapper objectMapper = null;

private ObjectUnwrapper objectUnwrapper = new JinjavaObjectUnwrapper();
private BiConsumer<Node, JinjavaInterpreter> nodePreProcessor = new JinjavaNodePreProcessor();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.hubspot.jinjava.lib.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
Expand All @@ -19,7 +18,6 @@
snippets = { @JinjavaSnippet(code = "{{object|fromJson}}") }
)
public class FromJsonFilter implements Filter {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
Expand All @@ -31,7 +29,10 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args)
throw new InvalidInputException(interpreter, this, InvalidReason.STRING);
}
try {
return OBJECT_MAPPER.readValue((String) var, Object.class);
return interpreter
.getConfig()
.getObjectMapper()
.readValue((String) var, Object.class);
} catch (IOException e) {
throw new InvalidInputException(interpreter, this, InvalidReason.JSON_READ);
}
Expand Down

0 comments on commit 5cb42f5

Please sign in to comment.