Skip to content

Commit 443c34c

Browse files
committed
CorsRegistry implements combine correctly
Closes gh-26877
1 parent 959e6d1 commit 443c34c

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -475,7 +475,6 @@ public void validateAllowCredentials() {
475475
* @return the combined {@code CorsConfiguration}, or {@code this}
476476
* configuration if the supplied configuration is {@code null}
477477
*/
478-
@Nullable
479478
public CorsConfiguration combine(@Nullable CorsConfiguration other) {
480479
if (other == null) {
481480
return this;

spring-webflux/src/main/java/org/springframework/web/reactive/config/CorsRegistration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class CorsRegistration {
3535

3636
private final String pathPattern;
3737

38-
private final CorsConfiguration config;
38+
private CorsConfiguration config;
3939

4040

4141
public CorsRegistration(String pathPattern) {
@@ -149,7 +149,7 @@ public CorsRegistration maxAge(long maxAge) {
149149
* @since 5.3
150150
*/
151151
public CorsRegistration combine(CorsConfiguration other) {
152-
this.config.combine(other);
152+
this.config = this.config.combine(other);
153153
return this;
154154
}
155155

spring-webflux/src/test/java/org/springframework/web/reactive/config/CorsRegistryTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -73,4 +73,24 @@ public void allowCredentials() {
7373
.containsExactly("*");
7474
}
7575

76+
@Test
77+
void combine() {
78+
CorsConfiguration otherConfig = new CorsConfiguration();
79+
otherConfig.addAllowedOrigin("http://localhost:3000");
80+
otherConfig.addAllowedMethod("*");
81+
otherConfig.applyPermitDefaultValues();
82+
83+
this.registry.addMapping("/api/**").combine(otherConfig);
84+
85+
Map<String, CorsConfiguration> configs = this.registry.getCorsConfigurations();
86+
assertThat(configs.size()).isEqualTo(1);
87+
CorsConfiguration config = configs.get("/api/**");
88+
assertThat(config.getAllowedOrigins()).isEqualTo(Collections.singletonList("http://localhost:3000"));
89+
assertThat(config.getAllowedMethods()).isEqualTo(Collections.singletonList("*"));
90+
assertThat(config.getAllowedHeaders()).isEqualTo(Collections.singletonList("*"));
91+
assertThat(config.getExposedHeaders()).isEmpty();
92+
assertThat(config.getAllowCredentials()).isNull();
93+
assertThat(config.getMaxAge()).isEqualTo(Long.valueOf(1800));
94+
}
95+
7696
}

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/CorsRegistration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class CorsRegistration {
3636

3737
private final String pathPattern;
3838

39-
private final CorsConfiguration config;
39+
private CorsConfiguration config;
4040

4141

4242
public CorsRegistration(String pathPattern) {
@@ -150,7 +150,7 @@ public CorsRegistration maxAge(long maxAge) {
150150
* @since 5.3
151151
*/
152152
public CorsRegistration combine(CorsConfiguration other) {
153-
this.config.combine(other);
153+
this.config = this.config.combine(other);
154154
return this;
155155
}
156156

spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/CorsRegistryTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -77,4 +77,24 @@ public void allowCredentials() {
7777
.as("Globally origins=\"*\" and allowCredentials=true should be possible")
7878
.containsExactly("*");
7979
}
80+
81+
@Test
82+
void combine() {
83+
CorsConfiguration otherConfig = new CorsConfiguration();
84+
otherConfig.addAllowedOrigin("http://localhost:3000");
85+
otherConfig.addAllowedMethod("*");
86+
otherConfig.applyPermitDefaultValues();
87+
88+
this.registry.addMapping("/api/**").combine(otherConfig);
89+
90+
Map<String, CorsConfiguration> configs = this.registry.getCorsConfigurations();
91+
assertThat(configs.size()).isEqualTo(1);
92+
CorsConfiguration config = configs.get("/api/**");
93+
assertThat(config.getAllowedOrigins()).isEqualTo(Collections.singletonList("http://localhost:3000"));
94+
assertThat(config.getAllowedMethods()).isEqualTo(Collections.singletonList("*"));
95+
assertThat(config.getAllowedHeaders()).isEqualTo(Collections.singletonList("*"));
96+
assertThat(config.getExposedHeaders()).isEmpty();
97+
assertThat(config.getAllowCredentials()).isNull();
98+
assertThat(config.getMaxAge()).isEqualTo(Long.valueOf(1800));
99+
}
80100
}

0 commit comments

Comments
 (0)