From a354046acf5a06806f2e2c7052edda336d73dc6f Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Wed, 2 Apr 2025 14:48:04 +0200 Subject: [PATCH] GH-1236 Fix actuator's FunctionsEndpoint to handle ineligible functions Given that we have ineligible function catalog.lookup(..) may return null if function is ineligible. Resolves #1236 --- .../function/actuator/FunctionsEndpoint.java | 31 ++++---- .../actuator/FunctionsEndpointTests.java | 74 +++++++++++++++++++ 2 files changed, 90 insertions(+), 15 deletions(-) create mode 100644 spring-cloud-function-context/src/test/java/org/springframework/cloud/function/actuator/FunctionsEndpointTests.java diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/actuator/FunctionsEndpoint.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/actuator/FunctionsEndpoint.java index 4d0ad3883..77682df26 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/actuator/FunctionsEndpoint.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/actuator/FunctionsEndpoint.java @@ -50,24 +50,25 @@ public class FunctionsEndpoint { Set names = functionCatalog.getNames(null); for (String name : names) { FunctionInvocationWrapper function = functionCatalog.lookup(name); - Map functionMap = new LinkedHashMap<>(); - if (function.isFunction()) { - functionMap.put("type", "FUNCTION"); - functionMap.put("input-type", this.toSimplePolyIn(function)); - functionMap.put("output-type", this.toSimplePolyOut(function)); + if (function != null) { + Map functionMap = new LinkedHashMap<>(); + if (function.isFunction()) { + functionMap.put("type", "FUNCTION"); + functionMap.put("input-type", this.toSimplePolyIn(function)); + functionMap.put("output-type", this.toSimplePolyOut(function)); + } + else if (function.isConsumer()) { + functionMap.put("type", "CONSUMER"); + functionMap.put("input-type", this.toSimplePolyIn(function)); + } + else { + functionMap.put("type", "SUPPLIER"); + functionMap.put("output-type", this.toSimplePolyOut(function)); + } + allFunctions.put(name, functionMap); } - else if (function.isConsumer()) { - functionMap.put("type", "CONSUMER"); - functionMap.put("input-type", this.toSimplePolyIn(function)); - } - else { - functionMap.put("type", "SUPPLIER"); - functionMap.put("output-type", this.toSimplePolyOut(function)); - } - allFunctions.put(name, functionMap); } - return allFunctions; } diff --git a/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/actuator/FunctionsEndpointTests.java b/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/actuator/FunctionsEndpointTests.java new file mode 100644 index 000000000..f9e9b4794 --- /dev/null +++ b/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/actuator/FunctionsEndpointTests.java @@ -0,0 +1,74 @@ +/* + * Copyright 2021-2025 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.actuator; + +import java.util.Locale; +import java.util.Map; +import java.util.function.Function; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.function.context.FunctionCatalog; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * + * @author Oleg Zhurakousky + */ + +public class FunctionsEndpointTests { + + @Test + public void ensureIneligibleFunctionWontCauseNPE() { + ApplicationContext context = new SpringApplicationBuilder(SampleConfiguration.class) + .run("--spring.cloud.function.ineligible-definitions=echo,uppercase", + "--spring.main.lazy-initialization=true"); + FunctionCatalog catalog = context.getBean(FunctionCatalog.class); + FunctionsEndpoint endpoint = new FunctionsEndpoint(catalog); + Map> allFunctionsinCatalog = endpoint.listAll(); + // implicit assertion - no NPE + assertThat(allFunctionsinCatalog.size()).isEqualTo(2); + assertThat(allFunctionsinCatalog.containsKey("functionRouter")); + assertThat(allFunctionsinCatalog.containsKey("reverse")); + } + + @EnableAutoConfiguration + @Configuration + public static class SampleConfiguration { + + @Bean + public Function echo() { + return v -> v; + } + + @Bean + public Function uppercase() { + return v -> v.toUpperCase(Locale.ROOT); + } + + @Bean + public Function reverse() { + return v -> new StringBuilder(v).reverse().toString(); + } + } +}