Enhance FunctionBindingEnvironmentPostProcessor to handle quotes in function definition
This commit is contained in:
@@ -75,6 +75,7 @@ $$metadata.store.zookeeper.root$$:: $$Root node - store entries are children of
|
||||
$$s3.common.endpoint-url$$:: $$Optional endpoint url to connect to s3 compatible storage.$$ *($$String$$, default: `$$<none>$$`)*
|
||||
$$s3.common.path-style-access$$:: $$Use path style access.$$ *($$Boolean$$, default: `$$false$$`)*
|
||||
$$s3.supplier.auto-create-local-dir$$:: $$Create or not the local directory.$$ *($$Boolean$$, default: `$$true$$`)*
|
||||
$$s3.supplier.delay-when-empty$$:: $$Duration of delay when no new files are detected.$$ *($$Duration$$, default: `$$1s$$`)*
|
||||
$$s3.supplier.delete-remote-files$$:: $$Delete or not remote files after processing.$$ *($$Boolean$$, default: `$$false$$`)*
|
||||
$$s3.supplier.filename-pattern$$:: $$The pattern to filter remote files.$$ *($$String$$, default: `$$<none>$$`)*
|
||||
$$s3.supplier.filename-regex$$:: $$The regexp to filter remote files.$$ *($$Pattern$$, default: `$$<none>$$`)*
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.cloud.stream.app.composite.function.common;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -28,7 +29,6 @@ import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
|
||||
import org.springframework.boot.env.EnvironmentPostProcessor;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -60,7 +60,7 @@ public class FunctionBindingEnvironmentPostProcessor implements EnvironmentPostP
|
||||
|
||||
@Override
|
||||
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
|
||||
String functionDefinition = functionDefinition(environment);
|
||||
String functionDefinition = normalizeFunctionDefinition(environment);
|
||||
if (StringUtils.isEmpty(functionDefinition)) {
|
||||
return;
|
||||
}
|
||||
@@ -95,18 +95,32 @@ public class FunctionBindingEnvironmentPostProcessor implements EnvironmentPostP
|
||||
}
|
||||
|
||||
private String functionDefinitionToChannelName(String functionDefinition) {
|
||||
return functionDefinition.replaceAll("\\||,", "");
|
||||
return functionDefinition.replace("|", "");
|
||||
}
|
||||
|
||||
private String functionDefinition(Environment environment) {
|
||||
private String normalizeFunctionDefinition(ConfigurableEnvironment environment) {
|
||||
String functionDefinition = null;
|
||||
if (environment.containsProperty(SPRING_CLOUD_FUNCTION_DEFINITION)) {
|
||||
functionDefinition = sanitizeActualFunctionDefinition(
|
||||
environment.getProperty(SPRING_CLOUD_FUNCTION_DEFINITION));
|
||||
}
|
||||
if (environment.containsProperty(SPRING_CLOUD_STREAM_FUNCTION_DEFINITION)) {
|
||||
log.warn("The property '" + SPRING_CLOUD_STREAM_FUNCTION_DEFINITION + "' is deprecated. Please use '"
|
||||
+ SPRING_CLOUD_FUNCTION_DEFINITION + "'");
|
||||
return environment.getProperty(SPRING_CLOUD_STREAM_FUNCTION_DEFINITION);
|
||||
functionDefinition = sanitizeActualFunctionDefinition(
|
||||
environment.getProperty(SPRING_CLOUD_STREAM_FUNCTION_DEFINITION));
|
||||
}
|
||||
else if (environment.containsProperty(SPRING_CLOUD_FUNCTION_DEFINITION)) {
|
||||
return environment.getProperty(SPRING_CLOUD_FUNCTION_DEFINITION);
|
||||
|
||||
if (StringUtils.hasText(functionDefinition)) {
|
||||
if (!functionDefinition.equals(environment.getProperty(SPRING_CLOUD_FUNCTION_DEFINITION))) {
|
||||
environment.getPropertySources().addFirst(new MapPropertySource("spring-cloud-function-definition",
|
||||
Collections.singletonMap(SPRING_CLOUD_FUNCTION_DEFINITION, functionDefinition)));
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return environment.getProperty(SPRING_CLOUD_FUNCTION_DEFINITION);
|
||||
}
|
||||
|
||||
private String sanitizeActualFunctionDefinition(String functionDefinition) {
|
||||
return functionDefinition.replaceAll("[\'\"]", "").replace(",", "|");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,22 @@ public class FunctionBindingsEnvironmentPostProcessorTests {
|
||||
.isEqualTo("output");
|
||||
assertThat(context.getEnvironment().getProperty("spring.cloud.stream.function.bindings.firstFunctionsecondFunction-in-0"))
|
||||
.isEqualTo("input");
|
||||
assertThat(context.getEnvironment().getProperty("spring.cloud.function.definition")).isEqualTo("firstFunction|secondFunction");
|
||||
}
|
||||
|
||||
@Test
|
||||
void destinationBindingsWithSingleQuotes() {
|
||||
ApplicationContext context = new SpringApplication(TestApp.class).run(
|
||||
"--spring.cloud.stream.bindings.output.destination=foo",
|
||||
"--spring.cloud.stream.bindings.input.destination=bar",
|
||||
"--spring.cloud.function.definition='firstFunction|secondFunction'");
|
||||
assertThat(context.getEnvironment().getProperty("spring.cloud.stream.function.bindings.firstFunctionsecondFunction-out-0"))
|
||||
.isEqualTo("output");
|
||||
assertThat(context.getEnvironment().getProperty("spring.cloud.stream.function.bindings.firstFunctionsecondFunction-in-0"))
|
||||
.isEqualTo("input");
|
||||
assertThat(context.getEnvironment().getProperty("spring.cloud.function.definition")).isEqualTo("firstFunction|secondFunction");
|
||||
}
|
||||
|
||||
@Test
|
||||
void destinationBindingsWithCommaDelimiter() {
|
||||
ApplicationContext context = new SpringApplication(TestApp.class).run(
|
||||
@@ -47,6 +62,7 @@ public class FunctionBindingsEnvironmentPostProcessorTests {
|
||||
.isEqualTo("output");
|
||||
assertThat(context.getEnvironment().getProperty("spring.cloud.stream.function.bindings.firstFunctionsecondFunction-in-0"))
|
||||
.isEqualTo("input");
|
||||
assertThat(context.getEnvironment().getProperty("spring.cloud.function.definition")).isEqualTo("firstFunction|secondFunction");
|
||||
}
|
||||
|
||||
@SpringBootApplication
|
||||
|
||||
Reference in New Issue
Block a user