Merge branch '3.2.x'

Closes gh-40869
This commit is contained in:
Moritz Halbritter 2024-05-22 13:49:35 +02:00
commit 3858a33162
2 changed files with 20 additions and 5 deletions

View File

@ -137,7 +137,7 @@ final class PulsarPropertiesMapper {
try {
return sortedParams.entrySet()
.stream()
.map((entry) -> "\"%s\":\"%s\"".formatted(entry.getKey(), entry.getValue()))
.map((entry) -> "\"%s\":\"%s\"".formatted(entry.getKey(), escapeJson(entry.getValue())))
.collect(Collectors.joining(",", "{", "}"));
}
catch (Exception ex) {
@ -145,6 +145,17 @@ final class PulsarPropertiesMapper {
}
}
private String escapeJson(String raw) {
return raw.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("/", "\\/")
.replace("\b", "\\b")
.replace("\t", "\\t")
.replace("\n", "\\n")
.replace("\f", "\\f")
.replace("\r", "\\r");
}
<T> void customizeProducerBuilder(ProducerBuilder<T> producerBuilder) {
PulsarProperties.Producer properties = this.properties.getProducer();
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();

View File

@ -81,8 +81,10 @@ class PulsarPropertiesMapperTests {
@Test
void customizeClientBuilderWhenHasAuthentication() throws UnsupportedAuthenticationException {
PulsarProperties properties = new PulsarProperties();
Map<String, String> params = Map.of("param", "name");
String authParamString = "{\"param\":\"name\"}";
Map<String, String> params = Map.of("simpleParam", "foo", "complexParam",
"{\n\t\"k1\" : \"v1\",\n\t\"k2\":\"v2\"\n}");
String authParamString = "{\"complexParam\":\"{\\n\\t\\\"k1\\\" : \\\"v1\\\",\\n\\t\\\"k2\\\":\\\"v2\\\"\\n}\""
+ ",\"simpleParam\":\"foo\"}";
properties.getClient().getAuthentication().setPluginClassName("myclass");
properties.getClient().getAuthentication().setParam(params);
ClientBuilder builder = mock(ClientBuilder.class);
@ -166,8 +168,10 @@ class PulsarPropertiesMapperTests {
@Test
void customizeAdminBuilderWhenHasAuthentication() throws UnsupportedAuthenticationException {
PulsarProperties properties = new PulsarProperties();
Map<String, String> params = Map.of("param", "name");
String authParamString = "{\"param\":\"name\"}";
Map<String, String> params = Map.of("simpleParam", "foo", "complexParam",
"{\n\t\"k1\" : \"v1\",\n\t\"k2\":\"v2\"\n}");
String authParamString = "{\"complexParam\":\"{\\n\\t\\\"k1\\\" : \\\"v1\\\",\\n\\t\\\"k2\\\":\\\"v2\\\"\\n}\""
+ ",\"simpleParam\":\"foo\"}";
properties.getAdmin().getAuthentication().setPluginClassName("myclass");
properties.getAdmin().getAuthentication().setParam(params);
PulsarAdminBuilder builder = mock(PulsarAdminBuilder.class);