Skip to content

Commit

Permalink
[2201.9.x] Fix rest path parameter generation with decoded value (#2247)
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK authored Dec 6, 2024
1 parent ca430bb commit 693029f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.11.6] - 2024-11-26

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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<String, Map<Integer, String>> arguments) {
if (wildcardToken == null) {
Expand Down

0 comments on commit 693029f

Please sign in to comment.