From f78f2d57e1c582dbea76c2f11ef82d743b1fd1bc Mon Sep 17 00:00:00 2001 From: Jon Schneider Date: Mon, 11 May 2020 10:40:36 -0400 Subject: [PATCH] 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,