Skip to content

Commit 0a22b38

Browse files
committed
#983 - Switch to Spring's ServerWebExchangeContextFilter.
Replace HypermediaWebFilter with ServerWebExchangeContextFilter. Related issues: spring-projects/spring-framework#21746
1 parent 950a3a7 commit 0a22b38

File tree

5 files changed

+13
-59
lines changed

5 files changed

+13
-59
lines changed

src/main/java/org/springframework/hateoas/config/WebFluxHateoasConfiguration.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
import org.springframework.context.annotation.Lazy;
2828
import org.springframework.core.codec.CharSequenceEncoder;
2929
import org.springframework.core.codec.StringDecoder;
30-
import org.springframework.hateoas.server.reactive.HypermediaWebFilter;
3130
import org.springframework.http.codec.CodecConfigurer;
3231
import org.springframework.http.codec.ServerCodecConfigurer;
3332
import org.springframework.http.codec.json.Jackson2JsonDecoder;
3433
import org.springframework.http.codec.json.Jackson2JsonEncoder;
3534
import org.springframework.lang.NonNull;
3635
import org.springframework.util.MimeType;
36+
import org.springframework.web.filter.reactive.ServerWebExchangeContextFilter;
3737
import org.springframework.web.reactive.config.WebFluxConfigurer;
3838
import org.springframework.web.reactive.function.client.WebClient;
3939

@@ -67,14 +67,10 @@ HypermediaWebFluxConfigurer hypermediaWebFluxConfigurer(ObjectProvider<ObjectMap
6767
return new HypermediaWebFluxConfigurer(mapper.getIfAvailable(ObjectMapper::new), hypermediaTypes);
6868
}
6969

70-
/**
71-
* TODO: Replace with Spring Framework filter when https://github.com/spring-projects/spring-framework/issues/21746 is
72-
* completed.
73-
*/
7470
@Bean
75-
@Lazy // To avoid creation on a WebMVC app using WebClient only
76-
HypermediaWebFilter hypermediaWebFilter() {
77-
return new HypermediaWebFilter();
71+
@Lazy
72+
ServerWebExchangeContextFilter serverWebExchangeContextFilter() {
73+
return new ServerWebExchangeContextFilter();
7874
}
7975

8076
/**

src/main/java/org/springframework/hateoas/server/reactive/HypermediaWebFilter.java

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/main/java/org/springframework/hateoas/server/reactive/WebFluxLinkBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.hateoas.server.reactive;
1717

18-
import static org.springframework.hateoas.server.reactive.HypermediaWebFilter.*;
18+
import static org.springframework.web.filter.reactive.ServerWebExchangeContextFilter.*;
1919

2020
import lombok.RequiredArgsConstructor;
2121

@@ -241,7 +241,7 @@ private static UriComponentsBuilder getBuilder(@Nullable ServerWebExchange excha
241241
private static Mono<WebFluxLinkBuilder> linkToInternal(Object invocation) {
242242

243243
return linkToInternal(invocation,
244-
Mono.subscriberContext().map(context -> getBuilder(context.getOrDefault(SERVER_WEB_EXCHANGE, null))));
244+
Mono.subscriberContext().map(context -> getBuilder(context.getOrDefault(EXCHANGE_CONTEXT_ATTRIBUTE, null))));
245245
}
246246

247247
private static Mono<WebFluxLinkBuilder> linkToInternal(Object invocation, Mono<UriComponentsBuilder> exchange) {

src/test/java/org/springframework/hateoas/server/reactive/HypermediaWebFilterTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import org.springframework.web.reactive.config.EnableWebFlux;
4141

4242
/**
43+
* Verify WebFlux properly activates the {@link org.springframework.web.filter.reactive.ServerWebExchangeContextFilter}.
44+
*
4345
* @author Greg Turnquist
4446
*/
4547
class HypermediaWebFilterTest {

src/test/java/org/springframework/hateoas/server/reactive/WebFluxLinkBuilderTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919
import static org.mockito.Mockito.*;
20-
import static org.springframework.hateoas.server.reactive.HypermediaWebFilter.*;
2120
import static org.springframework.hateoas.server.reactive.WebFluxLinkBuilder.*;
21+
import static org.springframework.web.filter.reactive.ServerWebExchangeContextFilter.*;
2222

2323
import reactor.core.publisher.Mono;
2424
import reactor.test.StepVerifier;
@@ -82,7 +82,7 @@ void linkAtSameLevelAsContextProvidedServerExchangeShouldWork() {
8282
when(this.request.getHeaders()).thenReturn(new HttpHeaders());
8383

8484
linkTo(methodOn(TestController.class).root()).withSelfRel().toMono() //
85-
.subscriberContext(Context.of(SERVER_WEB_EXCHANGE, this.exchange)) //
85+
.subscriberContext(Context.of(EXCHANGE_CONTEXT_ATTRIBUTE, this.exchange)) //
8686
.as(StepVerifier::create).expectNextMatches(link -> {
8787

8888
assertThat(link.getRel()).isEqualTo(IanaLinkRelations.SELF);
@@ -126,7 +126,7 @@ void shallowLinkFromDeepContextProvidedServerExchangeShouldWork() {
126126
when(this.request.getHeaders()).thenReturn(new HttpHeaders());
127127

128128
linkTo(methodOn(TestController.class).root()).withSelfRel().toMono() //
129-
.subscriberContext(Context.of(SERVER_WEB_EXCHANGE, this.exchange)) //
129+
.subscriberContext(Context.of(EXCHANGE_CONTEXT_ATTRIBUTE, this.exchange)) //
130130
.as(StepVerifier::create).expectNextMatches(link -> {
131131

132132
assertThat(link.getRel()).isEqualTo(IanaLinkRelations.SELF);
@@ -169,7 +169,7 @@ void deepLinkFromShallowContextProvidedServerExchangeShouldWork() {
169169
when(this.request.getHeaders()).thenReturn(new HttpHeaders());
170170

171171
linkTo(methodOn(TestController.class).deep()).withSelfRel().toMono() //
172-
.subscriberContext(Context.of(SERVER_WEB_EXCHANGE, this.exchange)) //
172+
.subscriberContext(Context.of(EXCHANGE_CONTEXT_ATTRIBUTE, this.exchange)) //
173173
.as(StepVerifier::create).expectNextMatches(link -> {
174174

175175
assertThat(link.getRel()).isEqualTo(IanaLinkRelations.SELF);
@@ -191,7 +191,7 @@ void linkToRouteWithNoMappingShouldWork() {
191191
when(this.request.getHeaders()).thenReturn(new HttpHeaders());
192192

193193
linkTo(methodOn(TestController2.class).root()).withSelfRel().toMono() //
194-
.subscriberContext(Context.of(SERVER_WEB_EXCHANGE, this.exchange)) //
194+
.subscriberContext(Context.of(EXCHANGE_CONTEXT_ATTRIBUTE, this.exchange)) //
195195
.as(StepVerifier::create).expectNextMatches(link -> {
196196

197197
assertThat(link.getRel()).isEqualTo(IanaLinkRelations.SELF);

0 commit comments

Comments
 (0)