Skip to content
Open
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 @@ -82,6 +82,11 @@ public class FunctionProperties implements EnvironmentAware, ApplicationContextA
*/
private final List<String> ineligibleDefinitions;

/**
* Whether a single eligible function can be looked up when the requested name is absent.
*/
private boolean singleFunctionFallbackEnabled = true;

private Map<String, FunctionConfigurationProperties> configuration;

private String expectedContentType;
Expand Down Expand Up @@ -196,6 +201,14 @@ public void setIneligibleDefinitions(List<String> definitions) {
this.ineligibleDefinitions.addAll(definitions);
}

public boolean isSingleFunctionFallbackEnabled() {
return this.singleFunctionFallbackEnabled;
}

public void setSingleFunctionFallbackEnabled(boolean singleFunctionFallbackEnabled) {
this.singleFunctionFallbackEnabled = singleFunctionFallbackEnabled;
}

public static class FunctionConfigurationProperties {

private Map<String, Object> inputHeaderMappingExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ String normalizeFunctionDefinition(String functionDefinition) {
: System.getProperty(FunctionProperties.FUNCTION_DEFINITION, "");

Set<String> names = this.getNames(null);
if (!names.contains(functionDefinition)) {
if (this.isSingleFunctionFallbackEnabled() && !names.contains(functionDefinition)) {
List<String> eligibleFunction = names.stream()
.filter(name -> !RoutingFunction.FUNCTION_NAME.equals(name))
.filter(name -> !RoutingFunction.DEFAULT_ROUTE_HANDLER.equals(name))
Expand All @@ -281,6 +281,10 @@ String normalizeFunctionDefinition(String functionDefinition) {
return functionDefinition;
}

private boolean isSingleFunctionFallbackEnabled() {
return this.functionProperties == null || this.functionProperties.isSingleFunctionFallbackEnabled();
}

/*
*
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
"description": "Name (e.g., 'foo') or composition instruction (e.g., 'foo|bar') used to resolve default function especially for cases where there is more than one function available in catalog.",
"defaultValue": ""
},
{
"name": "spring.cloud.function.single-function-fallback-enabled",
"type": "java.lang.Boolean",
"description": "Whether a single eligible function can be looked up when the requested name is absent.",
"defaultValue": true
},
{
"name": "spring.cloud.function.routing.enabled",
"type": "java.lang.Boolean",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,15 @@ private FunctionCatalog configureCatalog() {
}

private FunctionCatalog configureCatalog(Class<?>... configClass) {
this.context = new SpringApplicationBuilder(configClass)
.run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.main.lazy-initialization=true");
return this.configureCatalogWithProperties(new String[0], configClass);
}

private FunctionCatalog configureCatalogWithProperties(String[] properties, Class<?>... configClass) {
List<String> args = new ArrayList<>();
args.add("--logging.level.org.springframework.cloud.function=DEBUG");
args.add("--spring.main.lazy-initialization=true");
args.addAll(Arrays.asList(properties));
this.context = new SpringApplicationBuilder(configClass).run(args.toArray(new String[0]));
FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
return catalog;
}
Expand Down Expand Up @@ -304,6 +310,23 @@ public void testDefaultLookup() throws Exception {
assertThat(((FunctionInvocationWrapper) function).isComposed()).isTrue();
}

@Test
public void testSingleFunctionFallbackIsEnabledByDefault() {
FunctionCatalog catalog = this.configureCatalog(InputHeaderPropagationConfiguration.class);

assertThat((Object) catalog.lookup("missingFunction")).isNotNull();
}

@Test
public void testSingleFunctionFallbackCanBeDisabled() {
FunctionCatalog catalog = this.configureCatalogWithProperties(
new String[] { "--spring.cloud.function.single-function-fallback-enabled=false" },
InputHeaderPropagationConfiguration.class);

assertThat((Object) catalog.lookup("missingFunction")).isNull();
assertThat((Object) catalog.lookup("uppercase")).isNotNull();
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testBiFunction() {
Expand Down