From 4b6fb4c0fbfb4a770715ccf2826359977c2a15f7 Mon Sep 17 00:00:00 2001 From: spencergibb Date: Fri, 30 May 2025 11:52:09 -0400 Subject: [PATCH] Adds support for migrating default-filters. Also adds migration tests for streaming-media-types. Removes unused migration key in server webmvc that is faulty anyway. Fixes gh-3817 --- ...wayServerWebMvcPropertiesMigrationListener.java | 4 ---- .../mvc/GatewayMvcPropertiesMigrationTests.java | 7 +++++++ .../application-propertiesmigrationtests.yml | 2 ++ ...ayServerWebfluxPropertiesMigrationListener.java | 6 ++++++ .../config/GatewayPropertiesMigrationTests.java | 14 ++++++++++++++ .../application-propertiesmigrationtests.yml | 4 ++++ 6 files changed, 33 insertions(+), 4 deletions(-) diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/config/GatewayServerWebMvcPropertiesMigrationListener.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/config/GatewayServerWebMvcPropertiesMigrationListener.java index 8fdf6de4..9c50225d 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/config/GatewayServerWebMvcPropertiesMigrationListener.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/config/GatewayServerWebMvcPropertiesMigrationListener.java @@ -56,8 +56,6 @@ class GatewayServerWebMvcPropertiesMigrationListener implements ApplicationListe private static final String DEPRECATED_ROUTES_MAP_KEY = DEPRECATED_ROOT + ".routes-map"; - private static final String DEPRECATED_ROUTESMAP_KEY = DEPRECATED_ROOT + ".routesMap"; - private static final String GATEWAY_PROPERTY_SOURCE_PREFIX = "migrategatewaymvc"; private static final String NEW_ROUTES_LIST_KEY = GatewayMvcProperties.PREFIX + ".routes"; @@ -89,8 +87,6 @@ class GatewayServerWebMvcPropertiesMigrationListener implements ApplicationListe DEPRECATED_ROUTES_LIST_KEY, NEW_ROUTES_LIST_KEY)); routesMigrations.addAll(migrate(env, propertySource, GATEWAY_PROPERTY_SOURCE_PREFIX + "routes-map-", DEPRECATED_ROUTES_MAP_KEY, NEW_ROUTES_MAP_KEY)); - routesMigrations.addAll(migrate(env, propertySource, GATEWAY_PROPERTY_SOURCE_PREFIX + "routesMap-", - DEPRECATED_ROUTES_MAP_KEY, NEW_ROUTES_MAP_KEY)); }); } diff --git a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/GatewayMvcPropertiesMigrationTests.java b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/GatewayMvcPropertiesMigrationTests.java index 2043fc73..49105fe3 100644 --- a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/GatewayMvcPropertiesMigrationTests.java +++ b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/GatewayMvcPropertiesMigrationTests.java @@ -34,6 +34,7 @@ import org.springframework.cloud.gateway.server.mvc.filter.TransferEncodingNorma import org.springframework.cloud.gateway.server.mvc.filter.WeightCalculatorFilter; import org.springframework.cloud.gateway.server.mvc.filter.XForwardedRequestHeadersFilter; import org.springframework.context.ApplicationContext; +import org.springframework.http.MediaType; import org.springframework.test.context.ActiveProfiles; import static org.assertj.core.api.Assertions.assertThat; @@ -69,6 +70,12 @@ public class GatewayMvcPropertiesMigrationTests { assertThat(properties.getRoutesMap()).hasSize(2); } + @Test + public void deprecatedStreamingMediaTypesWork() { + assertThat(properties.getStreamingMediaTypes()).hasSize(1) + .containsOnly(new MediaType("application", "activemessage")); + } + private void assertBeanDoesNotExist(Class type) { assertThat(context.getBeanNamesForType(type)).isEmpty(); } diff --git a/spring-cloud-gateway-server-mvc/src/test/resources/application-propertiesmigrationtests.yml b/spring-cloud-gateway-server-mvc/src/test/resources/application-propertiesmigrationtests.yml index 32dc4133..0ddd7605 100644 --- a/spring-cloud-gateway-server-mvc/src/test/resources/application-propertiesmigrationtests.yml +++ b/spring-cloud-gateway-server-mvc/src/test/resources/application-propertiesmigrationtests.yml @@ -42,6 +42,8 @@ spring.cloud.gateway.mvc: uri: https://example2.com predicates: - Path=/anything/example2 + streaming-media-types: application/activemessage + logging: level: org.springframework.cloud.gateway.server.mvc: TRACE diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayServerWebfluxPropertiesMigrationListener.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayServerWebfluxPropertiesMigrationListener.java index eb7ef6d2..d7040fc4 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayServerWebfluxPropertiesMigrationListener.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayServerWebfluxPropertiesMigrationListener.java @@ -54,10 +54,14 @@ class GatewayServerWebfluxPropertiesMigrationListener implements ApplicationList private static final String DEPRECATED_ROUTES_LIST_KEY = DEPRECATED_ROOT + ".routes"; + private static final String DEPRECATED_DEFAULT_FILTERS_KEY = DEPRECATED_ROOT + ".default-filters"; + private static final String GATEWAY_PROPERTY_SOURCE_PREFIX = "migrategatewayflux"; private static final String NEW_ROUTES_LIST_KEY = GatewayProperties.PREFIX + ".routes"; + private static final String NEW_DEFAULT_FILTERS_KEY = GatewayProperties.PREFIX + ".default-filters"; + private final List routesMigrations = new ArrayList<>(); @Override @@ -81,6 +85,8 @@ class GatewayServerWebfluxPropertiesMigrationListener implements ApplicationList ConfigurationPropertySources.get(env).forEach(propertySource -> { routesMigrations.addAll(migrate(env, propertySource, GATEWAY_PROPERTY_SOURCE_PREFIX + "routes-", DEPRECATED_ROUTES_LIST_KEY, NEW_ROUTES_LIST_KEY)); + routesMigrations.addAll(migrate(env, propertySource, GATEWAY_PROPERTY_SOURCE_PREFIX + "default-filters-", + DEPRECATED_DEFAULT_FILTERS_KEY, NEW_DEFAULT_FILTERS_KEY)); }); } diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/config/GatewayPropertiesMigrationTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/config/GatewayPropertiesMigrationTests.java index 9cb9c4d0..b1ec4a8a 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/config/GatewayPropertiesMigrationTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/config/GatewayPropertiesMigrationTests.java @@ -29,6 +29,7 @@ import org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter; import org.springframework.cloud.gateway.handler.predicate.AfterRoutePredicateFactory; import org.springframework.cloud.gateway.route.RouteRefreshListener; import org.springframework.context.ApplicationContext; +import org.springframework.http.MediaType; import org.springframework.test.context.ActiveProfiles; import static org.assertj.core.api.Assertions.assertThat; @@ -58,6 +59,19 @@ public class GatewayPropertiesMigrationTests { assertThat(properties.getRoutes()).hasSize(2); } + @Test + public void deprecatedDefaultFiltersPropertiesWork() { + assertThat(properties.getDefaultFilters()).hasSize(2); + assertThat(properties.getDefaultFilters().get(0).getName()).isEqualTo("AddRequestHeader"); + assertThat(properties.getDefaultFilters().get(1).getName()).isEqualTo("AddRequestHeader"); + } + + @Test + public void deprecatedStreamingMediaTypesWork() { + assertThat(properties.getStreamingMediaTypes()).hasSize(1) + .containsOnly(new MediaType("application", "activemessage")); + } + private void assertBeanDoesNotExist(Class type) { assertThat(context.getBeanNamesForType(type)).isEmpty(); } diff --git a/spring-cloud-gateway-server/src/test/resources/application-propertiesmigrationtests.yml b/spring-cloud-gateway-server/src/test/resources/application-propertiesmigrationtests.yml index 2f781c6a..6829481b 100644 --- a/spring-cloud-gateway-server/src/test/resources/application-propertiesmigrationtests.yml +++ b/spring-cloud-gateway-server/src/test/resources/application-propertiesmigrationtests.yml @@ -27,6 +27,10 @@ spring.cloud.gateway: pattern: /anything/listRoute2 filters: - AddRequestHeader=X-Test,listRoute2 + defaultFilters: + - AddRequestHeader=X-Default1 + - AddRequestHeader=X-Default2 + streaming-media-types: application/activemessage logging: level: org.springframework.cloud.gateway: TRACE