diff --git a/spring-web/src/main/java/org/springframework/web/util/RfcUriParser.java b/spring-web/src/main/java/org/springframework/web/util/RfcUriParser.java index cf4e2848157..adda43d129a 100644 --- a/spring-web/src/main/java/org/springframework/web/util/RfcUriParser.java +++ b/spring-web/src/main/java/org/springframework/web/util/RfcUriParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -503,8 +503,7 @@ abstract class RfcUriParser { // Component capture public InternalParser resolveIfOpaque() { - boolean hasSlash = (this.uri.indexOf('/', this.index + 1) == -1); - this.isOpaque = (hasSlash && !hierarchicalSchemes.contains(this.scheme)); + this.isOpaque = (this.uri.charAt(this.index) != '/' && !hierarchicalSchemes.contains(this.scheme)); return this; } diff --git a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java index ce22e8e4c88..f31c71bb1a8 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -155,6 +155,19 @@ class UriComponentsBuilderTests { assertThat(result.toUri()).as("Invalid result URI").isEqualTo(uri); } + @ParameterizedTest // see gh-34588 + @EnumSource + void fromOpaqueUriWithUrnScheme(ParserType parserType) { + URI uri = UriComponentsBuilder + .fromUriString("urn:text:service-{region}:{prefix}/{id}", parserType).build() + .expand("US", "prefix1", "Id-2") + .toUri(); + + assertThat(uri.getScheme()).isEqualTo("urn"); + assertThat(uri.isOpaque()).isTrue(); + assertThat(uri.getSchemeSpecificPart()).isEqualTo("text:service-US:prefix1/Id-2"); + } + @ParameterizedTest // see gh-9317 @EnumSource void fromUriEncodedQuery(ParserType parserType) {