From f78f2d57e1c582dbea76c2f11ef82d743b1fd1bc Mon Sep 17 00:00:00 2001 From: Jon Schneider Date: Mon, 11 May 2020 10:40:36 -0400 Subject: [PATCH 1/2] Fix uri tag for empty path See gh-21392 --- .../actuate/metrics/web/reactive/server/WebFluxTags.java | 3 +++ .../boot/actuate/metrics/web/servlet/WebMvcTags.java | 3 +++ .../actuate/endpoint/web/servlet/WebMvcTagsTests.java | 8 ++++++++ .../metrics/web/reactive/server/WebFluxTagsTests.java | 9 +++++++++ 4 files changed, 23 insertions(+) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTags.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTags.java index b0412d3bd9..12fba3e481 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTags.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTags.java @@ -110,6 +110,9 @@ public final class WebFluxTags { if (ignoreTrailingSlash && patternString.length() > 1) { patternString = TRAILING_SLASH_PATTERN.matcher(patternString).replaceAll(""); } + if (patternString.isEmpty()) { + return URI_ROOT; + } return Tag.of("uri", patternString); } HttpStatus status = exchange.getResponse().getStatusCode(); diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java index 4b734847ed..8ca5f3e97f 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java @@ -115,6 +115,9 @@ public final class WebMvcTags { if (ignoreTrailingSlash && pattern.length() > 1) { pattern = TRAILING_SLASH_PATTERN.matcher(pattern).replaceAll(""); } + if (pattern.isEmpty()) { + return URI_ROOT; + } return Tag.of("uri", pattern); } if (response != null) { diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/servlet/WebMvcTagsTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/servlet/WebMvcTagsTests.java index 44f9f1a5a2..36ba42db68 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/servlet/WebMvcTagsTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/servlet/WebMvcTagsTests.java @@ -58,6 +58,14 @@ class WebMvcTagsTests { assertThat(tag.getValue()).isEqualTo("/spring/"); } + @Test + void uriTagValueIsRootWhenBestMatchingPatternIsEmpty() { + this.request.setAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, ""); + this.response.setStatus(301); + Tag tag = WebMvcTags.uri(this.request, this.response); + assertThat(tag.getValue()).isEqualTo("root"); + } + @Test void uriTagValueWithBestMatchingPatternAndIgnoreTrailingSlashRemoveTrailingSlash() { this.request.setAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, "/spring/"); diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java index 033d4d7932..f5b19e5d43 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java @@ -61,6 +61,15 @@ class WebFluxTagsTests { assertThat(tag.getValue()).isEqualTo("/spring/"); } + @Test + void uriTagValueIsRootWhenBestMatchingPatternIsEmpty() { + this.exchange.getAttributes().put(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, + this.parser.parse("")); + this.exchange.getResponse().setStatusCode(HttpStatus.MOVED_PERMANENTLY); + Tag tag = WebFluxTags.uri(this.exchange); + assertThat(tag.getValue()).isEqualTo("root"); + } + @Test void uriTagValueWithBestMatchingPatternAndIgnoreTrailingSlashRemoveTrailingSlash() { this.exchange.getAttributes().put(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, From afcb5d54b2dddfe9429d6234daf15c9a16a4fe5c Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 25 May 2020 13:50:39 +0200 Subject: [PATCH 2/2] Polish "Fix uri tag for empty path" See gh-21392 --- .../actuate/metrics/web/reactive/server/WebFluxTagsTests.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java index f5b19e5d43..90c6b36504 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java @@ -63,8 +63,7 @@ class WebFluxTagsTests { @Test void uriTagValueIsRootWhenBestMatchingPatternIsEmpty() { - this.exchange.getAttributes().put(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, - this.parser.parse("")); + this.exchange.getAttributes().put(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, this.parser.parse("")); this.exchange.getResponse().setStatusCode(HttpStatus.MOVED_PERMANENTLY); Tag tag = WebFluxTags.uri(this.exchange); assertThat(tag.getValue()).isEqualTo("root");