From e718cf8ac15e1f95cab1270717095e725d04161c Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Wed, 30 Oct 2024 14:17:41 -0400 Subject: [PATCH] GH-9614: `SimpleJsonSerializer`: escape only `\` Fixes: #9614 Issue link: https://github.com/spring-projects/spring-integration/issues/9614 The `Matcher.quoteReplacement()` escapes `\` as well as `$`. However, Jackson tries to resolve special symbol from the escaped `$` and fails as `Unrecognized character escape '$' (code 36)` * Fix `SimpleJsonSerializer.toElement()` to escape only `\` (cherry picked from commit 8dad15bac227da671b2471bd62da9b6fcf22754e) --- .../json/SimpleJsonSerializer.java | 5 ++- .../json/SimpleJsonSerializerTests.java | 31 +++++++++++++------ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/json/SimpleJsonSerializer.java b/spring-integration-core/src/main/java/org/springframework/integration/json/SimpleJsonSerializer.java index 1266de8b033..63a911915ed 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/json/SimpleJsonSerializer.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/json/SimpleJsonSerializer.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2020 the original author or authors. + * Copyright 2017-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +22,6 @@ import java.util.Arrays; import java.util.HashSet; import java.util.Set; -import java.util.regex.Matcher; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -100,7 +99,7 @@ private static String toElement(Object result) { return result.toString(); } else { - return "\"" + (result == null ? "null" : Matcher.quoteReplacement(result.toString())) + "\""; + return "\"" + (result == null ? "null" : result.toString().replace("\\", "\\\\")) + "\""; } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/json/SimpleJsonSerializerTests.java b/spring-integration-core/src/test/java/org/springframework/integration/json/SimpleJsonSerializerTests.java index b040775622f..6ea1f1bac81 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/json/SimpleJsonSerializerTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/json/SimpleJsonSerializerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2022 the original author or authors. + * Copyright 2017-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,7 @@ package org.springframework.integration.json; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.integration.support.json.JsonObjectMapperProvider; @@ -29,10 +29,10 @@ * @since 5.0 * */ -public class SimpleJsonSerializerTests { +class SimpleJsonSerializerTests { @Test - public void test() throws Exception { + void verifySimpleJsonSerializerAgainstSimpleContent() throws Exception { Foo foo = new Foo("foo"); String json = SimpleJsonSerializer.toJson(foo, "fileInfo"); Foo fooOut = JsonObjectMapperProvider.newInstance().fromJson(json, Foo.class); @@ -43,7 +43,15 @@ public void test() throws Exception { assertThat(fooOut.fileInfo).isNull(); } - public static class Foo { + @Test + void verifySimpleJsonSerializerAgainstDollarContent() throws Exception { + Foo foo = new Foo("some content with $"); + String json = SimpleJsonSerializer.toJson(foo); + Foo fooOut = JsonObjectMapperProvider.newInstance().fromJson(json, Foo.class); + assertThat(fooOut.fileInfo).isEqualTo("some content with $"); + } + + static class Foo { private final String foo = "bar"; @@ -55,12 +63,11 @@ public static class Foo { private String fileInfo; - public Foo() { - super(); + Foo() { } - public Foo(String info) { - this.fileInfo = "foo"; + Foo(String info) { + this.fileInfo = info; } public String getFoo() { @@ -79,7 +86,11 @@ public boolean isBool() { return this.bool; } - public String fileInfo() { + public void setFileInfo(String fileInfo) { + this.fileInfo = fileInfo; + } + + public String getFileInfo() { return this.fileInfo; }