diff --git a/docs/modules/ROOT/pages/spring-cloud-function/programming-model.adoc b/docs/modules/ROOT/pages/spring-cloud-function/programming-model.adoc index 0bf61b98d..7e0a94abb 100644 --- a/docs/modules/ROOT/pages/spring-cloud-function/programming-model.adoc +++ b/docs/modules/ROOT/pages/spring-cloud-function/programming-model.adoc @@ -63,8 +63,8 @@ In summary, Spring Cloud Function instruments Java functions with additional fea While the previous example shows you how to lookup a function in `FunctionCatalog` programmatically, in a typical integration case where Spring Cloud Function is used as the programming model by another framework (e.g. https://spring.io/projects/spring-cloud-stream[Spring Cloud Stream]), you can declare which functions to use via the `spring.cloud.function.definition` property. It is important to know and understand the default behaviour when it comes to discovering functions in `FunctionCatalog`. -For instance, if you only have one Functional bean in your `ApplicationContext`, the `spring.cloud.function.definition` property typically will not be required since a single function in `FunctionCatalog` -can be looked up by an empty name, or any name. +For instance, if you only have one eligible Functional bean in your `ApplicationContext`, the `spring.cloud.function.definition` property typically will not be required since a single function in `FunctionCatalog` +can be looked up by an empty name, or any name that is not explicitly excluded. For example, assuming that `uppercase` is the only function in your catalog, it can be looked up as `catalog.lookup(null)`, `catalog.lookup(“”)`, `catalog.lookup(“foo”)`. That said, for cases where you are using a framework such as Spring Cloud Stream, which uses `spring.cloud.function.definition`, it is recommended to always use the `spring.cloud.function.definition` property. @@ -86,6 +86,10 @@ A typical `ApplicationContext` may include beans that are valid Java functions, Such beans could be auto-configurations from other projects or any other bean that qualifies as a Java function. The framework provides default filtering of known beans that should not be candidates for registration with `FunctionCatalog`. +For example, Spring WebFlux's `ForwardedHeaderTransformer` is excluded by type, regardless of its bean name. +Excluded beans are filtered before auto-discovery selects a default function, so they do not prevent a single eligible function from being discovered. +These exclusions only affect the function catalog; the beans remain available in the `ApplicationContext` for their intended use, such as HTTP forwarded-header processing. + You can also add additional beans to this list by providing a comma-delimited list of bean definition names using the `spring.cloud.function.ineligible-definitions` property. For example, @@ -95,6 +99,8 @@ For example, spring.cloud.function.ineligible-definitions=foo,bar ---- +Looking up an explicitly excluded function name returns no function, rather than falling back to the single eligible function. + [[supplier]] === Supplier Supplier can be _reactive_ - `Supplier>` or _imperative_ - `Supplier`. diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/FunctionProperties.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/FunctionProperties.java index f02e5de70..31abb1bf1 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/FunctionProperties.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/FunctionProperties.java @@ -105,6 +105,7 @@ public FunctionProperties() { ineligibleDefinitions = new ArrayList<>(); String[] definitions = new String[] { "org.springframework.boot", + "org.springframework.web.server.adapter.ForwardedHeaderTransformer", "org.springframework.cloud.function.cloudevent.CloudEventsFunctionExtensionConfiguration", "org.springframework.cloud.function.context.config.FunctionsEndpointAutoConfiguration", "classLoaderMetrics", diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/BeanFactoryAwareFunctionRegistry.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/BeanFactoryAwareFunctionRegistry.java index 10e688c80..cbb13c013 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/BeanFactoryAwareFunctionRegistry.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/BeanFactoryAwareFunctionRegistry.java @@ -123,15 +123,34 @@ public Set getNames(Class type) { else { registeredNames.addAll(Arrays.asList(this.applicationContext.getBeanNamesForType(type))); } + // Filter before default-function selection, not just during registration. + registeredNames.removeIf(name -> !isFunctionDefinitionEligible(name)); return registeredNames; } + @Override + boolean isFunctionDefinitionEligible(String functionDefinition) { + if (!super.isFunctionDefinitionEligible(functionDefinition)) { + return false; + } + for (String name : StringUtils.tokenizeToStringArray(functionDefinition, ",|")) { + if (this.applicationContext.containsBean(name) + && !isFunctionTypeEligible(this.applicationContext.getBeanFactory().getType(name, false))) { + return false; + } + } + return true; + } + @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public T lookup(Class type, String functionDefinition, String... expectedOutputMimeTypes) { functionDefinition = StringUtils.hasText(functionDefinition) ? functionDefinition : this.applicationContext.getEnvironment().getProperty(FunctionProperties.FUNCTION_DEFINITION, ""); + if (!isFunctionDefinitionEligible(functionDefinition)) { + return null; + } if (!this.applicationContext.containsBean(functionDefinition) || !KotlinUtils.isKotlinType(this.applicationContext.getBean(functionDefinition))) { functionDefinition = this.normalizeFunctionDefinition(functionDefinition); } diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistry.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistry.java index 6bd02ee36..ce0ded0df 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistry.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistry.java @@ -197,10 +197,18 @@ public void register(FunctionRegistration registration) { private boolean isRegistrationEligible(FunctionRegistration registration) { if (this.functionProperties != null) { for (String definition : this.functionProperties.getIneligibleDefinitions()) { - if (registration.getTarget().getClass().getName().equals(definition)) { + if (registration.getNames().contains(definition)) { return false; } - else if (registration.getNames().contains(definition) || registration.getTarget().getClass().getName().contains(definition)) { + } + } + return isFunctionTypeEligible(registration.getTarget().getClass()); + } + + boolean isFunctionTypeEligible(@Nullable Class functionType) { + if (this.functionProperties != null && functionType != null) { + for (String definition : this.functionProperties.getIneligibleDefinitions()) { + if (functionType.getName().contains(definition)) { return false; } } diff --git a/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/catalog/FunctionDiscoveryTests.java b/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/catalog/FunctionDiscoveryTests.java new file mode 100644 index 000000000..c5528863e --- /dev/null +++ b/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/catalog/FunctionDiscoveryTests.java @@ -0,0 +1,198 @@ +/* + * Copyright 2026-present 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.function.context.catalog; + +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; +import java.util.function.Function; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.cloud.function.context.FunctionCatalog; +import org.springframework.cloud.function.context.FunctionRegistration; +import org.springframework.cloud.function.context.FunctionRegistry; +import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper; +import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.mock.http.server.reactive.MockServerHttpRequest; +import org.springframework.web.server.adapter.ForwardedHeaderTransformer; + +import static org.assertj.core.api.Assertions.assertThat; + +class FunctionDiscoveryTests { + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(ContextFunctionCatalogAutoConfiguration.class)); + + @Test + void forwardedHeaderTransformerIsNotDiscoveredAsDefaultFunction() { + this.contextRunner.withBean("forwardedHeaderTransformer", ForwardedHeaderTransformer.class).run(context -> { + FunctionCatalog catalog = context.getBean(FunctionCatalog.class); + assertThat(catalog.getNames(null)).doesNotContain("forwardedHeaderTransformer"); + assertThat(catalog.getNames(Function.class)).doesNotContain("forwardedHeaderTransformer"); + assertThat((Object) catalog.lookup("")).isNull(); + assertThat((Object) catalog.lookup("forwardedHeaderTransformer")).isNull(); + }); + } + + @Test + void consumerIsDiscoveredAlongsideRenamedForwardedHeaderTransformer() { + this.contextRunner.withBean("customTransformer", ForwardedHeaderTransformer.class) + .withUserConfiguration(ConsumerConfiguration.class).run(context -> { + FunctionCatalog catalog = context.getBean(FunctionCatalog.class); + assertThat(catalog.getNames(null)).doesNotContain("customTransformer"); + assertThat((Object) catalog.lookup("customTransformer")).isNull(); + assertThat((Object) catalog.lookup("customTransformer|myConsumer")).isNull(); + FunctionInvocationWrapper function = catalog.lookup(""); + assertThat(function).isNotNull(); + assertThat(function.getFunctionDefinition()).isEqualTo("myConsumer"); + function.apply("hello"); + assertThat(context.getBean(ConsumerConfiguration.class).received).hasValue("hello"); + }); + } + + @Test + void forwardedHeaderTransformerStillProcessesHttpRequests() { + this.contextRunner.withBean(ForwardedHeaderTransformer.class).run(context -> { + context.getBean(FunctionCatalog.class).getNames(null); + ForwardedHeaderTransformer transformer = context.getBean(ForwardedHeaderTransformer.class); + MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost/path") + .header("Forwarded", "proto=https;host=example.org").build(); + assertThat(transformer.apply(request).getURI().toString()).isEqualTo("https://example.org/path"); + }); + } + + @Test + void discoveryDoesNotInstantiateLazyTransformer() { + AtomicBoolean instantiated = new AtomicBoolean(); + this.contextRunner.withBean("transformer", ForwardedHeaderTransformer.class, () -> { + instantiated.set(true); + return new ForwardedHeaderTransformer(); + }, definition -> definition.setLazyInit(true)).run(context -> { + FunctionCatalog catalog = context.getBean(FunctionCatalog.class); + assertThat(catalog.getNames(null)).doesNotContain("transformer"); + assertThat((Object) catalog.lookup("")).isNull(); + assertThat(instantiated).isFalse(); + }); + } + + @Test + void excludedBeanNameDoesNotPreventConsumerDiscovery() { + this.contextRunner.withUserConfiguration(ConsumerConfiguration.class, OtherFunctionConfiguration.class) + .withPropertyValues("spring.cloud.function.ineligible-definitions=otherFunction").run(context -> { + FunctionCatalog catalog = context.getBean(FunctionCatalog.class); + assertThat(catalog.getNames(null)).doesNotContain("otherFunction"); + assertThat((Object) catalog.lookup("otherFunction")).isNull(); + FunctionInvocationWrapper function = catalog.lookup(""); + assertThat(function).isNotNull(); + assertThat(function.getFunctionDefinition()).isEqualTo("myConsumer"); + }); + } + + @Test + void excludedBeanTypeDoesNotPreventConsumerDiscovery() { + this.contextRunner.withUserConfiguration(ConsumerConfiguration.class) + .withBean("otherFunction", OtherFunction.class) + .withPropertyValues("spring.cloud.function.ineligible-definitions=" + OtherFunction.class.getName()) + .run(context -> { + FunctionCatalog catalog = context.getBean(FunctionCatalog.class); + assertThat(catalog.getNames(null)).doesNotContain("otherFunction"); + FunctionInvocationWrapper function = catalog.lookup(""); + assertThat(function).isNotNull(); + assertThat(function.getFunctionDefinition()).isEqualTo("myConsumer"); + }); + } + + @Test + void explicitDefinitionStillSelectsFunctionAmongMultipleCandidates() { + this.contextRunner.withBean(ForwardedHeaderTransformer.class) + .withUserConfiguration(ConsumerConfiguration.class, OtherFunctionConfiguration.class) + .withPropertyValues("spring.cloud.function.definition=otherFunction").run(context -> { + FunctionCatalog catalog = context.getBean(FunctionCatalog.class); + Function function = catalog.lookup(""); + assertThat(function).isNotNull(); + assertThat(function.apply("hello")).isEqualTo("hello"); + }); + } + + @Test + void forwardedHeaderTransformerCannotBeRegisteredDirectly() { + this.contextRunner.run(context -> { + FunctionRegistry registry = context.getBean(FunctionRegistry.class); + registry.register(new FunctionRegistration<>(new ForwardedHeaderTransformer(), "transformer")); + assertThat(registry.getNames(null)).doesNotContain("transformer"); + assertThat((Object) registry.lookup("")).isNull(); + }); + } + + @Test + void multipleEligibleFunctionsStillRequireExplicitSelection() { + this.contextRunner.withBean(ForwardedHeaderTransformer.class) + .withUserConfiguration(ConsumerConfiguration.class, OtherFunctionConfiguration.class).run(context -> { + FunctionCatalog catalog = context.getBean(FunctionCatalog.class); + assertThat((Object) catalog.lookup("")).isNull(); + assertThat(catalog.getNames(null)).contains("myConsumer", "otherFunction"); + }); + } + + @Test + void ordinarySingleConsumerDiscoveryIsUnchanged() { + this.contextRunner.withUserConfiguration(ConsumerConfiguration.class).run(context -> { + FunctionInvocationWrapper function = context.getBean(FunctionCatalog.class).lookup(""); + assertThat(function).isNotNull(); + assertThat(function.getFunctionDefinition()).isEqualTo("myConsumer"); + function.apply("hello"); + assertThat(context.getBean(ConsumerConfiguration.class).received).hasValue("hello"); + }); + } + + @Configuration(proxyBeanMethods = false) + static class ConsumerConfiguration { + + private final AtomicReference received = new AtomicReference<>(); + + @Bean + Consumer myConsumer() { + return this.received::set; + } + + } + + @Configuration(proxyBeanMethods = false) + static class OtherFunctionConfiguration { + + @Bean + Function otherFunction() { + return Function.identity(); + } + + } + + static class OtherFunction implements Function { + + @Override + public String apply(String value) { + return value; + } + + } + +}