Repository navigation
GH-1537 Pass empty input to Kotlin functions and harden KotlinFunctionWrapper - #1549
Open
seonwooj0810 wants to merge 1 commit into
Open
seonwooj0810 wants to merge 1 commit into
seonwooj0810 wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1537
KotlinFunctionWrapper.apply(..)usedObjectUtils.isEmpty(input)to decide whether to call the target with or without an argument. As a result,"", an empty collection, map, array orOptionalall counted as "no input". For a(String) -> Stringthis goes toinvoke(). BecauseisValidSuspendingSupplieris true for anyFunction1, the lambda is then invoked as a suspending supplier, and the caller gets aFluxback instead of the function result (e.g.catalog.lookup(Function.class, "kotlinFunction").apply("")returned aFluxMap). The check is nowinput == null.get()still callsapply(null), so suppliers are unaffected.The other points from the issue, in the same class:
substring), not withreplace(..).getFunctionRegistration()throws anIllegalStateExceptionthat names the function. Previously it threw aNullPointerExceptionorClassCastException.isTypeRepresentedByClasscompares the raw type (for a wildcard, its bound) instead of doing acontainson the type name. BesidesFunction1/Function10, the name check also matched nested type arguments. For example,Function1<Function0<String>, String>was registered as aSupplier<Function0<String>>because the name containsFunction0. It is now registered as aFunction. TheFunction10-Function19case was already masked by thetypes.lengthchecks, so for that case this only makes the check robust.Tests:
KotlinFunctionWrapperTests(new, context module) has 6 tests: empty string, empty list, map, array andOptionalare passed to the function;nullstill invokes a supplier; trailing-suffix stripping; the non-parameterized type error; and theFunction1<Function0<String>, String>classification. Onmain, 5 of them fail:apply("")returnsMonoFlatMapManyinstead of the result, the suffix test hits an NPE, the type-error test gets an NPE, and the nested type is resolved asSupplier.ContextFunctionCatalogAutoConfigurationKotlinTests.kotlinFunctionWithEmptyInputlooks up a real Kotlin(String) -> Stringbean from the catalog and applies"". It fails onmain(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.