Skip to content

Commit

Permalink
Also convert to snake case accessible map when serializing map entries
Browse files Browse the repository at this point in the history
  • Loading branch information
jasmith-hs committed Mar 17, 2023
1 parent 5cb42f5 commit b28c7dc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ public void serialize(
);
String key;
String value;
ObjectWriter objectWriter = PyishObjectMapper.PYISH_OBJECT_WRITER.withAttribute(
PyishObjectMapper.ALLOW_SNAKE_CASE_ATTRIBUTE,
serializerProvider.getAttribute(PyishObjectMapper.ALLOW_SNAKE_CASE_ATTRIBUTE)
);
if (remainingLength != null) {
ObjectWriter objectWriter = PyishObjectMapper.PYISH_OBJECT_WRITER.withAttribute(
LengthLimitingWriter.REMAINING_LENGTH_ATTRIBUTE,
remainingLength
);
objectWriter =
objectWriter.withAttribute(
LengthLimitingWriter.REMAINING_LENGTH_ATTRIBUTE,
remainingLength
);
key = objectWriter.writeValueAsString(entry.getKey());
LengthLimitingWriter lengthLimitingWriter = new LengthLimitingWriter(
new CharArrayWriter(),
Expand All @@ -39,8 +44,8 @@ public void serialize(
objectWriter.writeValue(lengthLimitingWriter, entry.getValue());
value = lengthLimitingWriter.toString();
} else {
key = PyishObjectMapper.PYISH_OBJECT_WRITER.writeValueAsString(entry.getKey());
value = PyishObjectMapper.PYISH_OBJECT_WRITER.writeValueAsString(entry.getValue());
key = objectWriter.writeValueAsString(entry.getKey());
value = objectWriter.writeValueAsString(entry.getValue());
}
jsonGenerator.writeRawValue(String.format("fn:map_entry(%s, %s)", key, value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.hubspot.jinjava.LegacyOverrides;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.objects.collections.SizeLimitingPyMap;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -96,6 +97,16 @@ public void itSerializesToSnakeCaseAccessibleMap() {
.isEqualTo("{'fooBar': 'bar'} |allow_snake_case");
}

@Test
public void itSerializesToSnakeCaseAccessibleMapWhenInMapEntry() {
assertThat(
PyishObjectMapper.getAsPyishString(
new AbstractMap.SimpleImmutableEntry<>("foo", new Foo("bar"))
)
)
.isEqualTo("fn:map_entry('foo', {'fooBar': 'bar'} |allow_snake_case)");
}

@Test
public void itDoesNotConvertToSnakeCaseMapWhenResultIsForOutput() {
Jinjava jinjava = new Jinjava(
Expand Down

0 comments on commit b28c7dc

Please sign in to comment.