From 7f444a3dc8f15d945d6dc7592b3b4e5ade5b5fb9 Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Wed, 25 Nov 2026 04:51:56 -0800 Subject: [PATCH] fix: isolate per-lookup output content type from the cached function wrapper A singleton lookup that specifies output MIME types now returns a separate FunctionInvocationWrapper with those types. The wrapper reuses the same target, preserves composition, conversion, and header settings, and has its own invocation state. Lookups without MIME types still return the cached singleton wrapper. Regression tests cover concurrent lookups, isolation of conversion settings, and JSON output from a composed function. Fixes #1534 Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> --- .../catalog/SimpleFunctionRegistry.java | 28 ++++- .../catalog/SimpleFunctionRegistryTests.java | 115 +++++++++++++++++- 2 files changed, 138 insertions(+), 5 deletions(-) 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 646982713..6ccbb65c8 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 @@ -251,7 +251,13 @@ T doLookup(Class type, String functionDefinition, String[] expectedOutput } if (function != null) { if (!ObjectUtils.isEmpty(expectedOutputMimeTypes)) { - function.expectedOutputContentType = expectedOutputMimeTypes; + // GH-1534: a cached singleton is shared, so this lookup gets its own view. + if (function.isSingleton) { + function = new FunctionInvocationWrapper(function, expectedOutputMimeTypes); + } + else { + function.expectedOutputContentType = expectedOutputMimeTypes; + } } } else if (logger.isDebugEnabled()) { @@ -536,6 +542,26 @@ public class FunctionInvocationWrapper implements Function, Cons } } + /* + * Per-lookup view of a cached singleton. Thread-locals and {@code message} come from + * the constructor above so this view does not share them with the cached wrapper. + */ + private FunctionInvocationWrapper(FunctionInvocationWrapper source, String[] expectedOutputMimeTypes) { + this(source.functionDefinition, source.target, source.inputType, source.outputType); + this.inputType = source.inputType; + this.composed = source.composed; + this.postProcessor = source.postProcessor; + this.skipInputConversion = source.skipInputConversion; + this.skipOutputConversion = source.skipOutputConversion; + this.skipInputConversionCallback = source.skipInputConversionCallback; + this.enhancer = source.enhancer; + this.propagateInputHeaders = source.propagateInputHeaders; + this.isSingleton = source.isSingleton; + this.wrappedBiConsumer = source.wrappedBiConsumer; + this.isPojoFunction = source.isPojoFunction; + this.expectedOutputContentType = expectedOutputMimeTypes; + } + public int hashCode() { return this.functionDefinition.hashCode(); } diff --git a/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistryTests.java b/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistryTests.java index f6b56af0d..6248e968c 100644 --- a/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistryTests.java +++ b/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistryTests.java @@ -401,11 +401,118 @@ public void testCachingOfFunction() { new JacksonMapper(new ObjectMapper())); catalog.register(registration); - FunctionInvocationWrapper instanceA = catalog.lookup("echo", "application/json"); - FunctionInvocationWrapper instanceb = catalog.lookup("echo", "text/plain"); - FunctionInvocationWrapper instanceC = catalog.lookup("echo", "foo/bar"); + FunctionInvocationWrapper json = catalog.lookup("echo", "application/json"); + FunctionInvocationWrapper text = catalog.lookup("echo", "text/plain"); + FunctionInvocationWrapper other = catalog.lookup("echo", "foo/bar"); + FunctionInvocationWrapper cached = catalog.lookup("echo"); + + assertThat(json).isNotSameAs(text).isNotSameAs(other).isNotSameAs(cached); + assertThat(text).isNotSameAs(other); + assertThat((String[]) ReflectionTestUtils.getField(json, "expectedOutputContentType")) + .containsExactly("application/json"); + assertThat((String[]) ReflectionTestUtils.getField(text, "expectedOutputContentType")) + .containsExactly("text/plain"); + assertThat((String[]) ReflectionTestUtils.getField(other, "expectedOutputContentType")) + .containsExactly("foo/bar"); + assertThat(ReflectionTestUtils.getField(cached, "expectedOutputContentType")).isNull(); + + @SuppressWarnings("unchecked") + Map definitions = (Map) ReflectionTestUtils + .getField(catalog, "wrappedFunctionDefinitions"); + assertThat(definitions).hasSize(1); + assertThat(definitions.get("echo")).isSameAs(cached); + + json.setSkipOutputConversion(true); + assertThat(json.isSkipOutputConversion()).isTrue(); + assertThat(cached.isSkipOutputConversion()).isFalse(); + } + + @SuppressWarnings("unchecked") + @Test + public void testConcurrentLookupsKeepExpectedOutputContentType() throws Exception { + SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter, + new JacksonMapper(new ObjectMapper())); + catalog.register(new FunctionRegistration<>(new UpperCase(), "uppercase").type(UpperCase.class)); + + ExecutorService executor = Executors.newFixedThreadPool(2); + CountDownLatch startGate = new CountDownLatch(1); + try { + CompletableFuture jsonLookup = CompletableFuture.supplyAsync(() -> { + try { + assertThat(startGate.await(5, TimeUnit.SECONDS)).isTrue(); + } + catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + throw new IllegalStateException(ex); + } + return catalog.lookup("uppercase", "application/json"); + }, executor); + CompletableFuture textLookup = CompletableFuture.supplyAsync(() -> { + try { + assertThat(startGate.await(5, TimeUnit.SECONDS)).isTrue(); + } + catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + throw new IllegalStateException(ex); + } + return catalog.lookup("uppercase", "text/plain"); + }, executor); + startGate.countDown(); + FunctionInvocationWrapper json = jsonLookup.get(5, TimeUnit.SECONDS); + FunctionInvocationWrapper text = textLookup.get(5, TimeUnit.SECONDS); + + assertThat(json).isNotSameAs(text); + assertThat((String[]) ReflectionTestUtils.getField(json, "expectedOutputContentType")) + .containsExactly("application/json"); + assertThat((String[]) ReflectionTestUtils.getField(text, "expectedOutputContentType")) + .containsExactly("text/plain"); + + Message singleValueMessage = MessageBuilder.withPayload("\"ricky\"").build(); + Object result = json.apply(singleValueMessage); + assertThat(result).isInstanceOf(Message.class); + assertThat(((Message) result).getPayload()).isEqualTo("\"RICKY\"".getBytes()); + + FunctionInvocationWrapper cached = catalog.lookup("uppercase"); + assertThat(cached).isNotSameAs(json).isNotSameAs(text); + assertThat(ReflectionTestUtils.getField(cached, "expectedOutputContentType")).isNull(); + Map definitions = (Map) ReflectionTestUtils + .getField(catalog, "wrappedFunctionDefinitions"); + assertThat(definitions).hasSize(1); + assertThat(definitions.get("uppercase")).isSameAs(cached); + } + finally { + executor.shutdownNow(); + } + } - assertThat(instanceA).isSameAs(instanceb).isSameAs(instanceC); + @SuppressWarnings("unchecked") + @Test + public void testComposedLookupWithMimeTypeKeepsComposition() { + SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter, + new JacksonMapper(new ObjectMapper())); + catalog.register(new FunctionRegistration<>(new UpperCase(), "uppercase").type(UpperCase.class)); + catalog.register(new FunctionRegistration<>(new Reverse(), "reverse").type(Reverse.class)); + + FunctionInvocationWrapper composed = catalog.lookup("uppercase|reverse", "application/json"); + FunctionInvocationWrapper cached = catalog.lookup("uppercase|reverse"); + + assertThat(composed.isComposed()).isTrue(); + assertThat(composed).isNotSameAs(cached); + assertThat(composed.getTarget()).isSameAs(cached.getTarget()); + assertThat((String[]) ReflectionTestUtils.getField(composed, "expectedOutputContentType")) + .containsExactly("application/json"); + assertThat(ReflectionTestUtils.getField(cached, "expectedOutputContentType")).isNull(); + + Object result = composed.apply("ab"); + assertThat(result).isInstanceOf(Message.class); + assertThat(((Message) result).getPayload()).isEqualTo("\"BA\"".getBytes()); + + assertThat((Object) catalog.lookup("not-a-function", "application/json")).isNull(); + Map definitions = (Map) ReflectionTestUtils + .getField(catalog, "wrappedFunctionDefinitions"); + assertThat(definitions).hasSize(1); + assertThat(definitions).doesNotContainKey("not-a-function"); + assertThat(definitions.get("uppercase|reverse")).isSameAs(cached); } @Test