Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.nifi.processors.jolt;

import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.joltcommunity.jolt.JoltTransform;
import io.joltcommunity.jolt.JsonUtil;
Expand Down Expand Up @@ -112,6 +113,15 @@ public class JoltTransformJSON extends AbstractJoltTransform {
.addValidator(StandardValidators.DATA_SIZE_VALIDATOR)
.build();

public static final PropertyDescriptor RETAIN_UNICODE_ESCAPE_SEQUENCES = new PropertyDescriptor.Builder()
.name("Retain Unicode Escape Sequences")
.description("Allows for retaining Unicode escape sequences instead of resolving them (e.g. retain text \\u00E9 without resolving to é)")
.required(true)
.defaultValue("false")
.allowableValues("true", "false")
.addValidator(StandardValidators.BOOLEAN_VALIDATOR)
.build();

public static final Relationship REL_SUCCESS = new Relationship.Builder()
.name("success")
.description("The FlowFile with successfully transformed content or updated attribute will be routed to this relationship")
Expand All @@ -127,7 +137,8 @@ public class JoltTransformJSON extends AbstractJoltTransform {
JSON_SOURCE,
JSON_SOURCE_ATTRIBUTE,
PRETTY_PRINT,
MAX_STRING_LENGTH
MAX_STRING_LENGTH,
RETAIN_UNICODE_ESCAPE_SEQUENCES
)
).toList();

Expand Down Expand Up @@ -160,6 +171,14 @@ public void setup(final ProcessContext context) {

final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.getFactory().setStreamReadConstraints(streamReadConstraints);

final boolean retainUnicodeEscapeSequences = context.getProperty(RETAIN_UNICODE_ESCAPE_SEQUENCES).asBoolean();
if (retainUnicodeEscapeSequences) {
objectMapper.getFactory().enable(JsonWriteFeature.ESCAPE_NON_ASCII.mappedFeature());
} else {
objectMapper.getFactory().disable(JsonWriteFeature.ESCAPE_NON_ASCII.mappedFeature());
}

jsonUtil = JsonUtils.customJsonUtil(objectMapper);

configuredClassLoader = getClass().getClassLoader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,41 @@ void testInvalidJsonLinesContent() {
runner.assertAllFlowFilesTransferred(JoltTransformJSON.REL_FAILURE);
}

@ParameterizedTest
@MethodSource("unicodeEscaping")
void testWithUnicodeEscaping(boolean retainUnicodeEscapeSequences, String expectedText) {
final String unicodeJson = """
{"value": "\\u011f\\u00fc\\u015f\\u0131\\u00f6\\u00e7\\u011e\\u00dc\\u015e\\u0130\\u00d6\\u00c7"}""";
final String passThroughSpec = """
[
{
"operation": "shift",
"spec": {
"*": "&"
}
}
]""";

runner.setProperty(JoltTransformJSON.JOLT_SPEC, passThroughSpec);
runner.setProperty(JoltTransformJSON.RETAIN_UNICODE_ESCAPE_SEQUENCES, Boolean.toString(retainUnicodeEscapeSequences));
runner.enqueue(unicodeJson);
runner.run();

runner.assertAllFlowFilesTransferred(JoltTransformJSON.REL_SUCCESS);
final MockFlowFile transformed = runner.getFlowFilesForRelationship(JoltTransformJSON.REL_SUCCESS).getFirst();
// NOTE: The JSON specification allows for both uppercase and lowercase hexadecimal letters.
// Jackson seems to prefer to upper case them hence must test with case insensitivity when unicode escape sequence is retained.
assertTrue(transformed.getContent().toLowerCase().contains(expectedText.toLowerCase()));
}

private static Stream<Arguments> unicodeEscaping() {
return Stream.of(
Arguments.argumentSet("Retain Unicode Escape Sequence", true, "\\u011f\\u00fc\\u015f\\u0131\\u00f6\\u00e7\\u011e\\u00dc\\u015e\\u0130\\u00d6\\u00c7"),
Arguments.argumentSet("Resolve Unicode Escape Sequence", false, "ğüşıöçĞÜŞİÖÇ")

);
}

private static Stream<Arguments> getChainrArguments() {
return Stream.of(
Arguments.argumentSet("has no single line comments", Paths.get(CHAINR_SPEC_PATH)),
Expand Down
Loading