Skip to content

Commit

Permalink
Avoids NPE in MvcUtils
Browse files Browse the repository at this point in the history
This was caused if vanilla RouterFunctions.route() was used rather than GatewayRouterFunctions

Fixes gh-3265
  • Loading branch information
spencergibb committed Mar 13, 2024
1 parent d76a48d commit 35c6384
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -157,7 +158,9 @@ public static <T> T getAttribute(ServerRequest request, String key) {
public static Map<String, Object> getGatewayAttributes(ServerRequest request) {
// This map is made in GatewayDelegatingRouterFunction.route() and persists across
// attribute resetting in RequestPredicates
Map<String, Object> attributes = (Map<String, Object>) request.attributes().get(GATEWAY_ATTRIBUTES_ATTR);
// computeIfAbsent if the used vanilla RouterFunctions.route()
Map<String, Object> attributes = (Map<String, Object>) request.attributes()
.computeIfAbsent(GATEWAY_ATTRIBUTES_ATTR, s -> new HashMap<String, Object>());
return attributes;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2013-2023 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.gateway.server.mvc;

import java.util.Map;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.cloud.gateway.server.mvc.test.HttpbinTestcontainers;
import org.springframework.cloud.gateway.server.mvc.test.HttpbinUriResolver;
import org.springframework.cloud.gateway.server.mvc.test.TestLoadBalancerConfig;
import org.springframework.cloud.gateway.server.mvc.test.client.TestRestClient;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.RouterFunctions;
import org.springframework.web.servlet.function.ServerResponse;

import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.modifyRequestBody;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.host;

@SuppressWarnings("unchecked")
@SpringBootTest(properties = { "spring.cloud.gateway.mvc.http-client.type=jdk" },
webEnvironment = WebEnvironment.RANDOM_PORT)
@ContextConfiguration(initializers = HttpbinTestcontainers.class)
public class VanillaRouterFunctionTests {

@LocalServerPort
int port;

@Autowired
TestRestClient restClient;

@SuppressWarnings("rawtypes")
@Test
public void routerFunctionsRouteWorks() {
restClient.post().uri("/anything/routerfunctionsroute").header("Host", "www.routerfunctionsroute.org")
.bodyValue("hello").exchange().expectStatus().isOk().expectBody(Map.class).consumeWith(result -> {
System.out.println();
});
}

@SpringBootConfiguration
@EnableAutoConfiguration
@LoadBalancerClient(name = "httpbin", configuration = TestLoadBalancerConfig.Httpbin.class)
protected static class TestConfiguration {

@Bean
public RouterFunction<ServerResponse> routerFunctionsRoute() {
// @formatter:off
return RouterFunctions.route()
.POST("/anything/routerfunctionsroute", host("**.routerfunctionsroute.org"), http())
.before(modifyRequestBody(String.class, String.class, null, (request, s) -> s.toUpperCase()))
.before(new HttpbinUriResolver())
.build();
// @formatter:on
}

}

}

0 comments on commit 35c6384

Please sign in to comment.