Skip to content

GH-1537 Pass empty input to Kotlin functions and harden KotlinFunctionWrapper - #1549

Open
seonwooj0810 wants to merge 1 commit into
spring-cloud:mainfrom
seonwooj0810:fix/issue-1537-kotlin-empty-input
Open

seonwooj0810 wants to merge 1 commit into
spring-cloud:mainfrom
seonwooj0810:fix/issue-1537-kotlin-empty-input

Conversation

@seonwooj0810

Copy link
Copy Markdown

Fixes #1537

KotlinFunctionWrapper.apply(..) used ObjectUtils.isEmpty(input) to decide whether to call the target with or without an argument. As a result, "", an empty collection, map, array or Optional all counted as "no input". For a (String) -> String this goes to invoke(). Because isValidSuspendingSupplier is true for any Function1, the lambda is then invoked as a suspending supplier, and the caller gets a Flux back instead of the function result (e.g. catalog.lookup(Function.class, "kotlinFunction").apply("") returned a FluxMap). The check is now input == null. get() still calls apply(null), so suppliers are unaffected.

The other points from the issue, in the same class:

  • The registration suffix is stripped only when it is trailing (substring), not with replace(..).
  • If the generic type of the function can't be determined, getFunctionRegistration() throws an IllegalStateException that names the function. Previously it threw a NullPointerException or ClassCastException.
  • isTypeRepresentedByClass compares the raw type (for a wildcard, its bound) instead of doing a contains on the type name. Besides Function1/Function10, the name check also matched nested type arguments. For example, Function1<Function0<String>, String> was registered as a Supplier<Function0<String>> because the name contains Function0. It is now registered as a Function. The Function10-Function19 case was already masked by the types.length checks, so for that case this only makes the check robust.

Tests:

  • KotlinFunctionWrapperTests (new, context module) has 6 tests: empty string, empty list, map, array and Optional are passed to the function; null still invokes a supplier; trailing-suffix stripping; the non-parameterized type error; and the Function1<Function0<String>, String> classification. On main, 5 of them fail: apply("") returns MonoFlatMapMany instead of the result, the suffix test hits an NPE, the type-error test gets an NPE, and the nested type is resolved as Supplier.
  • ContextFunctionCatalogAutoConfigurationKotlinTests.kotlinFunctionWithEmptyInput looks up a real Kotlin (String) -> String bean from the catalog and applies "". It fails on main (FluxMap) and passes with this change.

Verification done: ./mvnw -pl spring-cloud-function-context clean install (254 tests, 0 failures, 0 checkstyle violations) and ./mvnw -pl spring-cloud-function-kotlin clean test (all 9 tests, including suspend, web and AWS Kotlin tests, pass) on JDK 25.

…otlinFunctionWrapper

KotlinFunctionWrapper.apply(..) used ObjectUtils.isEmpty(input) to decide
whether to invoke the target without an argument, so an empty String,
collection, map, array or Optional was treated as no input. A Kotlin
(String) -> String called with "" then went down the suspending supplier
path and returned a Flux instead of the function result. Only null now
means no input.

Also in the same class:
- strip only the trailing registration suffix from the bean name
- fail with an IllegalStateException naming the function when its generic
  type can not be determined, instead of a NullPointerException or
  ClassCastException
- compare raw types in isTypeRepresentedByClass instead of searching the
  type name, so Function1 no longer matches Function10 and a nested type
  argument such as Function1<Function0<String>, String> is no longer
  registered as a Supplier

Resolves spring-cloud#1537

Signed-off-by: seonwoo_jung <79202163+seonwooj0810@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Kotlin function wrapper treats empty input as no input

1 participant