From ba50099adf40b8d2aa743593db97a7b2931533f4 Mon Sep 17 00:00:00 2001 From: TharmiganK Date: Fri, 6 Dec 2024 06:47:51 +0530 Subject: [PATCH 1/3] Fix rest path generation from decoded value --- .../http/api/service/signature/AllPathParams.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/service/signature/AllPathParams.java b/native/src/main/java/io/ballerina/stdlib/http/api/service/signature/AllPathParams.java index 3e5f94045c..bf0e5cdd36 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/service/signature/AllPathParams.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/service/signature/AllPathParams.java @@ -33,6 +33,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.stream.Stream; import static io.ballerina.stdlib.http.api.HttpConstants.EXTRA_PATH_INDEX; import static io.ballerina.stdlib.http.api.HttpConstants.PERCENTAGE; @@ -79,17 +80,16 @@ public void populateFeed(Object[] paramFeed, HttpCarbonMessage httpCarbonMessage if (argumentValue.endsWith(PERCENTAGE)) { argumentValue = argumentValue.replaceAll(PERCENTAGE, PERCENTAGE_ENCODED); } - argumentValue = URLDecoder.decode(argumentValue.replaceAll(PLUS_SIGN, PLUS_SIGN_ENCODED), - StandardCharsets.UTF_8); Object castedPathValue; try { Object parsedValue; if (pathParam.isArray()) { - String[] segments = argumentValue.substring(1).split(HttpConstants.SINGLE_SLASH); + String[] segments = Stream.of(argumentValue.substring(1).split(HttpConstants.SINGLE_SLASH)) + .map(AllPathParams::decodePathSegment).toArray(String[]::new); parsedValue = castParamArray(paramTypeTag, segments); } else { - parsedValue = castParam(paramTypeTag, argumentValue); + parsedValue = castParam(paramTypeTag, decodePathSegment(argumentValue)); } castedPathValue = ValueUtils.convert(parsedValue, paramType); } catch (Exception ex) { @@ -110,6 +110,10 @@ public void populateFeed(Object[] paramFeed, HttpCarbonMessage httpCarbonMessage } } + private static String decodePathSegment(String pathSegment) { + return URLDecoder.decode(pathSegment.replaceAll(PLUS_SIGN, PLUS_SIGN_ENCODED), StandardCharsets.UTF_8); + } + private static void updateWildcardToken(String wildcardToken, int wildCardIndex, Map> arguments) { if (wildcardToken == null) { From 75e4b4b43880ca7d930b886d78c2c56be6e76a1b Mon Sep 17 00:00:00 2001 From: TharmiganK Date: Thu, 5 Dec 2024 21:15:54 +0530 Subject: [PATCH 2/3] Add test case --- .../service_dispatching_uri_template_matching.bal | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ballerina-tests/http-dispatching-tests/tests/service_dispatching_uri_template_matching.bal b/ballerina-tests/http-dispatching-tests/tests/service_dispatching_uri_template_matching.bal index bd119d06de..36be85d62e 100644 --- a/ballerina-tests/http-dispatching-tests/tests/service_dispatching_uri_template_matching.bal +++ b/ballerina-tests/http-dispatching-tests/tests/service_dispatching_uri_template_matching.bal @@ -383,6 +383,11 @@ service /encodedUri on utmTestEP { } service /restParam on utmTestEP { + + resource function 'default 'string/[string... aaa]() returns json { + return {aaa: aaa}; + } + resource function 'default 'int/[int... aaa](http:Caller caller) returns error? { http:Response res = new; json responseJson = {aaa: aaa}; @@ -1025,6 +1030,16 @@ function testEncodedPathParams() { } } +@test:Config {} +function testRestParamWithEncodedPathSegments() { +http:Response|error response = utmClient->get("/restParam/string/path%2Fseg/path%20seg/path%2Fseg+123"); + if response is http:Response { + common:assertJsonValue(response.getJsonPayload(), "aaa", ["path/seg", "path seg", "path/seg+123"]); + } else { + test:assertFail(msg = "Found unexpected output type: " + response.message()); + } +} + @test:Config {} function testMultipleIntTypedRestParams() { http:Response|error response = utmClient->get("/restParam/int/345/234/123"); From 7daf2ca29b23e98c80f46cee28f7238f91c882b4 Mon Sep 17 00:00:00 2001 From: TharmiganK Date: Fri, 6 Dec 2024 07:58:17 +0530 Subject: [PATCH 3/3] Update change log --- changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.md b/changelog.md index 02faa8a40f..7ee05447f5 100644 --- a/changelog.md +++ b/changelog.md @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Fixed - [Fix the issue of not being able to configure only server name in the secureSocket config](https://github.com/ballerina-platform/ballerina-library/issues/7443) +- [Fix rest path parameter generation with decoded path segments](https://github.com/ballerina-platform/ballerina-library/issues/7430) ## [2.10.17] - 2024-10-21