-
Notifications
You must be signed in to change notification settings - Fork 121
fix: URL Path Parameter Encoding Issue #1516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matheusandre1
wants to merge
2
commits into
quarkiverse:main
Choose a base branch
from
matheusandre1:issue1460
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
client/deployment/src/main/resources/templates/libraries/microprofile/pathParams.qute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| {#if param.isPathParam}@io.quarkiverse.openapi.generator.annotations.GeneratedParam("{param.baseName}") @jakarta.ws.rs.PathParam("{param.baseName}"){param.dataType} {param.paramName}{/if} | ||
| {#if param.isPathParam}@io.quarkiverse.openapi.generator.annotations.GeneratedParam("{param.baseName}"){#if param.dataType == 'String'} @io.quarkiverse.openapi.generator.annotations.EncodedPathParam{/if} @jakarta.ws.rs.PathParam("{param.baseName}"){param.dataType} {param.paramName}{/if} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
.../runtime/src/main/java/io/quarkiverse/openapi/generator/annotations/EncodedPathParam.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package io.quarkiverse.openapi.generator.annotations; | ||
|
|
||
| import static java.lang.annotation.ElementType.PARAMETER; | ||
| import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
|
||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * Marks a generated path parameter so the client can safely encode path segments. | ||
| */ | ||
| @Retention(RUNTIME) | ||
| @Target(PARAMETER) | ||
| public @interface EncodedPathParam { | ||
| } |
122 changes: 122 additions & 0 deletions
122
...a/io/quarkiverse/openapi/generator/providers/PathParamEncodingParamConverterProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| package io.quarkiverse.openapi.generator.providers; | ||
|
|
||
| import java.lang.annotation.Annotation; | ||
| import java.lang.reflect.Type; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Locale; | ||
|
|
||
| import jakarta.ws.rs.ext.ParamConverter; | ||
| import jakarta.ws.rs.ext.ParamConverterProvider; | ||
| import jakarta.ws.rs.ext.Provider; | ||
|
|
||
| import io.quarkiverse.openapi.generator.annotations.EncodedPathParam; | ||
|
|
||
| /** | ||
| * JAX-RS param converter provider used by generated REST clients for path parameters. | ||
| * <p> | ||
| * It percent-encodes reserved characters for path parameter values, while preserving already-encoded | ||
| * percent triplets such as {@code %2F}. That allows callers to pass either raw paths like | ||
| * {@code mygroup/myproject/backend} or already-encoded values like {@code mygroup%2Fmyproject%2Fbackend} | ||
| * without producing invalid double-encoded URLs. | ||
| */ | ||
| @Provider | ||
| public class PathParamEncodingParamConverterProvider implements ParamConverterProvider { | ||
|
|
||
| private static final ParamConverter<String> STRING_PATH_PARAM_CONVERTER = new ParamConverter<>() { | ||
| @Override | ||
| public String fromString(String value) { | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(String value) { | ||
| return encodePathParamValuePreservingEscapes(value); | ||
| } | ||
| }; | ||
|
|
||
| @Override | ||
| @SuppressWarnings("unchecked") | ||
| public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { | ||
| if (rawType != String.class || !hasEncodedPathParam(annotations)) { | ||
| return null; | ||
| } | ||
| return (ParamConverter<T>) STRING_PATH_PARAM_CONVERTER; | ||
| } | ||
|
|
||
| /** | ||
| * Encodes a path-parameter value using UTF-8 percent encoding, preserving existing escape sequences. | ||
| * <p> | ||
| * This treats {@code /} as data and encodes it to {@code %2F}, which is what generated clients need for | ||
| * path-parameter values that may span multiple raw segments. | ||
| * | ||
| * @param value the raw or already-encoded path-parameter value | ||
| * @return the encoded path-parameter value, or {@code null} if the input was {@code null} | ||
| */ | ||
| static String encodePathParamValuePreservingEscapes(String value) { | ||
| if (value == null) { | ||
| return null; | ||
| } | ||
|
|
||
| StringBuilder encoded = new StringBuilder(value.length()); | ||
| for (int i = 0; i < value.length();) { | ||
| char ch = value.charAt(i); | ||
| if (ch == '%' && i + 2 < value.length() && isHexDigit(value.charAt(i + 1)) && isHexDigit(value.charAt(i + 2))) { | ||
| encoded.append(ch).append(value.charAt(i + 1)).append(value.charAt(i + 2)); | ||
| i += 3; | ||
| continue; | ||
| } | ||
|
|
||
| int codePoint = value.codePointAt(i); | ||
| if (isUnreserved(codePoint)) { | ||
| encoded.appendCodePoint(codePoint); | ||
| } else { | ||
| byte[] bytes = new String(Character.toChars(codePoint)).getBytes(StandardCharsets.UTF_8); | ||
| for (byte b : bytes) { | ||
| encoded.append('%'); | ||
| int unsigned = b & 0xFF; | ||
| if (unsigned < 0x10) { | ||
| encoded.append('0'); | ||
| } | ||
| encoded.append(Integer.toHexString(unsigned).toUpperCase(Locale.ROOT)); | ||
| } | ||
| } | ||
| i += Character.charCount(codePoint); | ||
| } | ||
|
|
||
| return encoded.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns {@code true} when the parameter was generated as a path parameter that should be encoded. | ||
| */ | ||
| private static boolean hasEncodedPathParam(Annotation[] annotations) { | ||
| if (annotations == null) { | ||
| return false; | ||
| } | ||
| for (Annotation annotation : annotations) { | ||
| if (annotation.annotationType() == EncodedPathParam.class) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * RFC 3986 unreserved characters can remain unchanged in a path segment. | ||
| */ | ||
| private static boolean isUnreserved(int codePoint) { | ||
| return codePoint >= 'a' && codePoint <= 'z' | ||
| || codePoint >= 'A' && codePoint <= 'Z' | ||
| || codePoint >= '0' && codePoint <= '9' | ||
| || codePoint == '-' || codePoint == '.' || codePoint == '_' || codePoint == '~'; | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether a character is a hexadecimal digit. | ||
| */ | ||
| private static boolean isHexDigit(char ch) { | ||
| return (ch >= '0' && ch <= '9') | ||
| || (ch >= 'a' && ch <= 'f') | ||
| || (ch >= 'A' && ch <= 'F'); | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
.../quarkiverse/openapi/generator/providers/PathParamEncodingParamConverterProviderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package io.quarkiverse.openapi.generator.providers; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
|
|
||
| import java.lang.annotation.Annotation; | ||
| import java.lang.reflect.Method; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import io.quarkiverse.openapi.generator.annotations.EncodedPathParam; | ||
|
|
||
| class PathParamEncodingParamConverterProviderTest { | ||
|
|
||
| private final PathParamEncodingParamConverterProvider provider = new PathParamEncodingParamConverterProvider(); | ||
|
|
||
| @Test | ||
| void encodesRawPathSegmentsWithoutDoubleEncodingEscapes() throws Exception { | ||
| Annotation[] annotations = encodedPathParamAnnotations(); | ||
|
|
||
| var converter = provider.getConverter(String.class, String.class, annotations); | ||
| assertEquals("mygroup%2Fmyproject%2Fbackend", converter.toString("mygroup/myproject/backend")); | ||
| assertEquals("mygroup%2Fmyproject%2Fbackend", converter.toString("mygroup%2Fmyproject%2Fbackend")); | ||
| assertEquals("space%20and%2Bplus", converter.toString("space and+plus")); | ||
| assertEquals("caf%C3%A9", converter.toString("café")); | ||
| } | ||
|
|
||
| @Test | ||
| void doesNotApplyToRegularParameters() { | ||
| assertNull(provider.getConverter(String.class, String.class, new Annotation[0])); | ||
| } | ||
|
|
||
| @Test | ||
| void reusesTheSameConverterInstance() throws Exception { | ||
| Annotation[] annotations = encodedPathParamAnnotations(); | ||
|
|
||
| var converter = provider.getConverter(String.class, String.class, annotations); | ||
| assertSame(converter, provider.getConverter(String.class, String.class, annotations)); | ||
| } | ||
|
|
||
| private Annotation[] encodedPathParamAnnotations() throws Exception { | ||
| Method method = getClass().getDeclaredMethod("sample", String.class); | ||
| return method.getParameters()[0].getAnnotations(); | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| private void sample(@EncodedPathParam String value) { | ||
| value.length(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.