Skip to content

Commit

Permalink
Upgrade to Spring Cloud 2023.0.0-M1
Browse files Browse the repository at this point in the history
Closes gh-1277
  • Loading branch information
snicoll committed Aug 14, 2023
1 parent b4377b3 commit b6221fa
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.spring.initializr.generator.buildsystem.maven.MavenBuild;
import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem;
import io.spring.initializr.generator.condition.ConditionalOnBuildSystem;
import io.spring.initializr.generator.condition.ConditionalOnPlatformVersion;
import io.spring.initializr.generator.condition.ConditionalOnRequestedDependency;
import io.spring.initializr.generator.io.template.MustacheTemplateRenderer;
import io.spring.initializr.generator.project.ProjectDescription;
Expand Down Expand Up @@ -83,6 +84,7 @@ public SpringCloudCircuitBreakerBuildCustomizer springCloudCircuitBreakerBuildCu

@Bean
@ConditionalOnRequestedDependency("cloud-gateway")
@ConditionalOnPlatformVersion("[2.6.0,3.2.0-M1)")
public SpringCloudGatewayHelpDocumentCustomizer springCloudGatewayHelpDocumentCustomizer(
ProjectDescriptionDiff diff) {
return new SpringCloudGatewayHelpDocumentCustomizer(diff);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-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.
Expand All @@ -21,19 +21,30 @@
import io.spring.initializr.generator.buildsystem.Dependency;
import io.spring.initializr.generator.project.MutableProjectDescription;
import io.spring.initializr.generator.project.ProjectDescriptionCustomizer;
import io.spring.initializr.generator.version.VersionParser;
import io.spring.initializr.generator.version.VersionRange;

/**
* A {@link ProjectDescriptionCustomizer} that checks that Spring Cloud Gateway is not
* used with Spring MVC as only Spring WebFlux is supported.
* A {@link ProjectDescriptionCustomizer} that checks the web stack that is used with
* Spring Cloud Gateway as it requires a dedicated starter. Before Spring Boot 3.2, MVC
* was not supported.
*
* @author Stephane Nicoll
*/
public class SpringCloudGatewayProjectDescriptionCustomizer implements ProjectDescriptionCustomizer {

private static final VersionRange SPRING_BOOT_3_2_0_OR_LATER = VersionParser.DEFAULT.parseRange("3.2.0-M1");

@Override
public void customize(MutableProjectDescription description) {
Collection<String> dependencyIds = description.getRequestedDependencies().keySet();
if (dependencyIds.contains("cloud-gateway") && dependencyIds.contains("web")) {
if (!dependencyIds.contains("cloud-gateway")) {
return;
}
if (SPRING_BOOT_3_2_0_OR_LATER.match(description.getPlatformVersion())) {
swapStarterIfNecessary(description, dependencyIds);
}
else if (dependencyIds.contains("web")) {
description.removeDependency("web");
if (!description.getRequestedDependencies().containsKey("webflux")) {
description.addDependency("webflux",
Expand All @@ -42,4 +53,13 @@ public void customize(MutableProjectDescription description) {
}
}

private void swapStarterIfNecessary(MutableProjectDescription description, Collection<String> dependencyIds) {
if (dependencyIds.contains("web")) {
description.addDependency("cloud-gateway",
Dependency.from(description.getRequestedDependencies().get("cloud-gateway"))
.artifactId("spring-cloud-starter-gateway-mvc"));
}

}

}
14 changes: 6 additions & 8 deletions start-site/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ initializr:
version: 2021.0.8
- compatibilityRange: "[3.0.0,3.2.0-M1)"
version: 2022.0.4
- compatibilityRange: "[3.2.0-M1,3.2.x-SNAPSHOT)"
version: 2023.0.0-M1
repositories: spring-milestones
- compatibilityRange: "3.2.x-SNAPSHOT"
version: 2023.0.0-SNAPSHOT
repositories: spring-snapshots
spring-cloud-azure:
groupId: com.azure.spring
artifactId: spring-cloud-azure-dependencies
Expand Down Expand Up @@ -1089,7 +1095,6 @@ initializr:
- name: Contract Verifier
bom: spring-cloud
id: cloud-contract-verifier
compatibilityRange: "[2.6.0,3.2.0-M1)"
description: Moves TDD to the level of software architecture by enabling Consumer Driven Contract (CDC) development.
groupId: org.springframework.cloud
artifactId: spring-cloud-starter-contract-verifier
Expand All @@ -1100,7 +1105,6 @@ initializr:
- name: Contract Stub Runner
bom: spring-cloud
id: cloud-contract-stub-runner
compatibilityRange: "[2.6.0,3.2.0-M1)"
description: Stub Runner for HTTP/Messaging based communication. Allows creating WireMock stubs from RestDocs tests.
groupId: org.springframework.cloud
artifactId: spring-cloud-starter-contract-stub-runner
Expand Down Expand Up @@ -1131,7 +1135,6 @@ initializr:
href: https://docs.spring.io/spring-boot/docs/{bootVersion}/reference/htmlsingle/index.html#data.nosql.mongodb.embedded
- name: Spring Cloud
bom: spring-cloud
compatibilityRange: "[2.6.0,3.2.0-M1)"
content:
- name: Cloud Bootstrap
id: cloud-starter
Expand Down Expand Up @@ -1163,7 +1166,6 @@ initializr:
href: https://docs.spring.io/spring-cloud-task/docs/current/reference/html/
- name: Spring Cloud Config
bom: spring-cloud
compatibilityRange: "[2.6.0,3.2.0-M1)"
content:
- name: Config Client
id: cloud-config-client
Expand Down Expand Up @@ -1214,7 +1216,6 @@ initializr:
description: Spring Cloud Consul Quick Start
- name: Spring Cloud Discovery
bom: spring-cloud
compatibilityRange: "[2.6.0,3.2.0-M1)"
content:
- name: Eureka Discovery Client
id: cloud-eureka
Expand Down Expand Up @@ -1265,7 +1266,6 @@ initializr:
href: https://docs.spring.io/spring-cloud-consul/docs/current/reference/html/#spring-cloud-consul-discovery
- name: Spring Cloud Routing
bom: spring-cloud
compatibilityRange: "[2.6.0,3.2.0-M1)"
content:
- name: Gateway
id: cloud-gateway
Expand Down Expand Up @@ -1302,7 +1302,6 @@ initializr:
href: https://docs.spring.io/spring-cloud-commons/docs/current/reference/html/#spring-cloud-loadbalancer
- name: Spring Cloud Circuit Breaker
bom: spring-cloud
compatibilityRange: "[2.6.0,3.2.0-M1)"
content:
- name: Resilience4J
id: cloud-resilience4j
Expand All @@ -1314,7 +1313,6 @@ initializr:
href: https://docs.spring.io/spring-cloud-circuitbreaker/docs/current/reference/html/#configuring-resilience4j-circuit-breakers
- name: Spring Cloud Messaging
bom: spring-cloud
compatibilityRange: "[2.6.0,3.2.0-M1)"
content:
- name: Cloud Bus
id: cloud-bus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

package io.spring.start.site.project.dependency.springcloud;

import java.util.Collections;
import java.util.Arrays;
import java.util.function.Function;
import java.util.stream.Collectors;

import io.spring.initializr.generator.buildsystem.Dependency;
import io.spring.initializr.generator.project.MutableProjectDescription;
import io.spring.initializr.generator.version.Version;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -36,35 +39,54 @@
class SpringCloudGatewayProjectDescriptionCustomizerTests {

@Test
void customizeWithSpringCloudGatewayAndSpringMvcMigratesToSpringWebFlux() {
void customizeWithSpringCloudGatewayAndSpringMvcPre32MigratesToSpringWebFlux() {
MutableProjectDescription description = new MutableProjectDescription();
description.addDependency("cloud-gateway", mock(Dependency.class));
description.addDependency("web", mock(Dependency.class));
description.setPlatformVersion(Version.parse("3.1.0"));
new SpringCloudGatewayProjectDescriptionCustomizer().customize(description);
assertThat(description.getRequestedDependencies()).containsOnlyKeys("cloud-gateway", "webflux");
Dependency webflux = description.getRequestedDependencies().get("webflux");
assertThat(webflux.getGroupId()).isEqualTo("org.springframework.boot");
assertThat(webflux.getArtifactId()).isEqualTo("spring-boot-starter-webflux");
}

@Test
void customizeWithSpringCloudGatewayAndSpringMvcAs32UsesMvcStarter() {
MutableProjectDescription description = new MutableProjectDescription();
description.addDependency("cloud-gateway",
Dependency.withCoordinates("org.springframework.cloud", "spring-cloud-starter-gateway"));
description.addDependency("web", mock(Dependency.class));
description.setPlatformVersion(Version.parse("3.2.0-M1"));
new SpringCloudGatewayProjectDescriptionCustomizer().customize(description);
assertThat(description.getRequestedDependencies()).containsOnlyKeys("cloud-gateway", "web");
Dependency cloudGateway = description.getRequestedDependencies().get("cloud-gateway");
assertThat(cloudGateway.getArtifactId()).isEqualTo("spring-cloud-starter-gateway-mvc");
}

@Test
void customizeWithSpringCloudGatewayDoesNotAddSpringWebFlux() {
MutableProjectDescription description = mock(MutableProjectDescription.class);
given(description.getRequestedDependencies())
.willReturn(Collections.singletonMap("cloud-gateway", mock(Dependency.class)));
MutableProjectDescription description = mockProjectDescription("3.1.0", "cloud-gateway");
new SpringCloudGatewayProjectDescriptionCustomizer().customize(description);
verify(description).getRequestedDependencies();
verify(description).getPlatformVersion();
verifyNoMoreInteractions(description);
}

@Test
void customizeWithoutSpringCloudGatewayDoesNotRemoveSpringMvc() {
MutableProjectDescription description = mock(MutableProjectDescription.class);
given(description.getRequestedDependencies())
.willReturn(Collections.singletonMap("web", mock(Dependency.class)));
MutableProjectDescription description = mockProjectDescription("3.1.0", "web");
new SpringCloudGatewayProjectDescriptionCustomizer().customize(description);
verify(description).getRequestedDependencies();
verifyNoMoreInteractions(description);
}

MutableProjectDescription mockProjectDescription(String platformVersion, String... dependencies) {
MutableProjectDescription description = mock(MutableProjectDescription.class);
given(description.getPlatformVersion()).willReturn(Version.parse(platformVersion));
given(description.getRequestedDependencies()).willReturn(Arrays.stream(dependencies)
.collect(Collectors.toMap(Function.identity(), (t) -> mock(Dependency.class))));
return description;
}

}

0 comments on commit b6221fa

Please sign in to comment.