Skip to content

Commit 3858a33

Browse files
committed
Merge branch '3.2.x'
Closes gh-40869
2 parents da4c2db + 8392a7e commit 3858a33

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapper.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,25 @@ private String getAuthenticationParamsJson(Map<String, String> params) {
137137
try {
138138
return sortedParams.entrySet()
139139
.stream()
140-
.map((entry) -> "\"%s\":\"%s\"".formatted(entry.getKey(), entry.getValue()))
140+
.map((entry) -> "\"%s\":\"%s\"".formatted(entry.getKey(), escapeJson(entry.getValue())))
141141
.collect(Collectors.joining(",", "{", "}"));
142142
}
143143
catch (Exception ex) {
144144
throw new IllegalStateException("Could not convert auth parameters to encoded string", ex);
145145
}
146146
}
147147

148+
private String escapeJson(String raw) {
149+
return raw.replace("\\", "\\\\")
150+
.replace("\"", "\\\"")
151+
.replace("/", "\\/")
152+
.replace("\b", "\\b")
153+
.replace("\t", "\\t")
154+
.replace("\n", "\\n")
155+
.replace("\f", "\\f")
156+
.replace("\r", "\\r");
157+
}
158+
148159
<T> void customizeProducerBuilder(ProducerBuilder<T> producerBuilder) {
149160
PulsarProperties.Producer properties = this.properties.getProducer();
150161
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapperTests.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ void customizeClientBuilderWhenHasNoAuthentication() {
8181
@Test
8282
void customizeClientBuilderWhenHasAuthentication() throws UnsupportedAuthenticationException {
8383
PulsarProperties properties = new PulsarProperties();
84-
Map<String, String> params = Map.of("param", "name");
85-
String authParamString = "{\"param\":\"name\"}";
84+
Map<String, String> params = Map.of("simpleParam", "foo", "complexParam",
85+
"{\n\t\"k1\" : \"v1\",\n\t\"k2\":\"v2\"\n}");
86+
String authParamString = "{\"complexParam\":\"{\\n\\t\\\"k1\\\" : \\\"v1\\\",\\n\\t\\\"k2\\\":\\\"v2\\\"\\n}\""
87+
+ ",\"simpleParam\":\"foo\"}";
8688
properties.getClient().getAuthentication().setPluginClassName("myclass");
8789
properties.getClient().getAuthentication().setParam(params);
8890
ClientBuilder builder = mock(ClientBuilder.class);
@@ -166,8 +168,10 @@ void customizeAdminBuilderWhenHasNoAuthentication() {
166168
@Test
167169
void customizeAdminBuilderWhenHasAuthentication() throws UnsupportedAuthenticationException {
168170
PulsarProperties properties = new PulsarProperties();
169-
Map<String, String> params = Map.of("param", "name");
170-
String authParamString = "{\"param\":\"name\"}";
171+
Map<String, String> params = Map.of("simpleParam", "foo", "complexParam",
172+
"{\n\t\"k1\" : \"v1\",\n\t\"k2\":\"v2\"\n}");
173+
String authParamString = "{\"complexParam\":\"{\\n\\t\\\"k1\\\" : \\\"v1\\\",\\n\\t\\\"k2\\\":\\\"v2\\\"\\n}\""
174+
+ ",\"simpleParam\":\"foo\"}";
171175
properties.getAdmin().getAuthentication().setPluginClassName("myclass");
172176
properties.getAdmin().getAuthentication().setParam(params);
173177
PulsarAdminBuilder builder = mock(PulsarAdminBuilder.class);

0 commit comments

Comments
 (0)