Skip to content

Commit a5d20ff

Browse files
committed
Avoid NPE when creating method tag for WebFlux req with non-standard method
Previously, a NullPointerException would occur when WebFluxTags attempted to create a method Tag for a request with a non-standard method. This commit updates WebFluxTags to use getMethodValue(), which will never return null, rather than getMethod(), which may return null, when determining the tag's value for the given request. Closes gh-13596
1 parent 5fd30a9 commit a5d20ff

File tree

2 files changed

+15
-1
lines changed
  • spring-boot-project/spring-boot-actuator/src

2 files changed

+15
-1
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTags.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private WebFluxTags() {
5555
* @return the method tag whose value is a capitalized method (e.g. GET).
5656
*/
5757
public static Tag method(ServerWebExchange exchange) {
58-
return Tag.of("method", exchange.getRequest().getMethod().toString());
58+
return Tag.of("method", exchange.getRequest().getMethodValue());
5959
}
6060

6161
/**

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@
2121
import org.junit.Test;
2222

2323
import org.springframework.http.HttpStatus;
24+
import org.springframework.http.server.reactive.ServerHttpRequest;
2425
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
2526
import org.springframework.mock.web.server.MockServerWebExchange;
2627
import org.springframework.web.reactive.HandlerMapping;
28+
import org.springframework.web.server.ServerWebExchange;
2729
import org.springframework.web.util.pattern.PathPatternParser;
2830

2931
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.mockito.BDDMockito.given;
33+
import static org.mockito.Mockito.mock;
3034

3135
/**
3236
* Tests for {@link WebFluxTags}.
@@ -80,4 +84,14 @@ public void uriTagIsUnknownWhenRequestIsNull() {
8084
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
8185
}
8286

87+
@Test
88+
public void methodTagToleratesNonStandardHttpMethods() {
89+
ServerWebExchange exchange = mock(ServerWebExchange.class);
90+
ServerHttpRequest request = mock(ServerHttpRequest.class);
91+
given(exchange.getRequest()).willReturn(request);
92+
given(request.getMethodValue()).willReturn("CUSTOM");
93+
Tag tag = WebFluxTags.method(exchange);
94+
assertThat(tag.getValue()).isEqualTo("CUSTOM");
95+
}
96+
8397
}

0 commit comments

Comments
 (0)