Skip to content

Commit

Permalink
fix(sdk): unable to use path variables in cloud.Api (#3931)
Browse files Browse the repository at this point in the history
Fixes #3919

## Checklist

- [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [ ] Description explains motivation and solution
- [x] Tests added (always)
- [ ] Docs updated (only required for features)
- [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
0018akhil authored Sep 3, 2023
1 parent 7f28718 commit f123739
Show file tree
Hide file tree
Showing 13 changed files with 2,336 additions and 38 deletions.
64 changes: 64 additions & 0 deletions examples/tests/sdk_tests/api/path_vars.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
bring cloud;
bring http;

let api = new cloud.Api();


let handler = inflight (req: cloud.ApiRequest): cloud.ApiResponse => {
return cloud.ApiResponse {
body: Json.stringify({ user: req.vars.get("name") }),
headers: { "content-type" => "application/json" },
status: 200
};
};

let handler_two = inflight (req: cloud.ApiRequest): cloud.ApiResponse => {
return cloud.ApiResponse {
body: Json.stringify({ user: req.vars.get("name"), age: req.vars.get("age") }),
headers: { "content-type" => "application/json" },
status: 200
};
};

api.get("/users/{name}", handler);
api.get("/{name}", handler);
api.get("/users/permission/{name}", handler);
api.get("/{name}/{age}", handler_two);

test "test" {
let username = "tsuf";
let res: http.Response = http.get("${api.url}/users/${username}");


assert(res.status == 200);
assert(Json.parse(res.body ?? "").get("user") == username);
}

test "test2" {
let username = "akhil";
let res: http.Response = http.get("${api.url}/${username}");


assert(res.status == 200);
assert(Json.parse(res.body ?? "").get("user") == username);
}

test "test3" {
let username = "akhil";
let res: http.Response = http.get("${api.url}/users/permission/${username}");


assert(res.status == 200);
assert(Json.parse(res.body ?? "").get("user") == username);
}

test "test4" {
let username = "akhil";
let age = "23";
let res: http.Response = http.get("${api.url}/${username}/${age}");


assert(res.status == 200);
assert(Json.parse(res.body ?? "").get("user") == username);
assert(Json.parse(res.body ?? "").get("age") == age);
}
25 changes: 0 additions & 25 deletions examples/tests/valid/api_path_vars.w

This file was deleted.

18 changes: 17 additions & 1 deletion examples/tests/valid/api_valid_path.w
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,27 @@ testInvalidPath("/test/path/{unclosed");
testInvalidPath("/test/m{issplaced}");
testInvalidPath("/test/{misspla}ced");
testInvalidPath("/test/{}/empty");

testInvalidPath("/{sup:er/:annoying//path}");
testInvalidPath("/{::another:annoying:path}");
testInvalidPath("/n0t_alphanumer1cPa:th");
testInvalidPath("/{with}/{two:invali4d#}/variables");
testInvalidPath("/{unclosed");
testInvalidPath("/m{issplaced}");
testInvalidPath("/{misspla}ced");
testInvalidPath("test");
testInvalidPath("/{}/empty");
testInvalidPath("/{}");


// valid paths
testValidPath("/test");
testValidPath("/test/alphanumer1cPa_th");
testValidPath("/test/regular/path");
testValidPath("/test/pa-th/{with}/two/{variable_s}/f?bla=5&b=6");
testValidPath("/test/param/is/{last}");
testValidPath("/test/{param}");
testValidPath("/{param}");
testValidPath("/t/{param}");
testValidPath("/test/regular/path/{param}");
testValidPath("/test/segment1/{param1}/segment2?query1=value1?query2=value2");
testValidPath("/test/segment1/segment2?query=value1&query2=value2");
6 changes: 5 additions & 1 deletion libs/wingsdk/src/cloud/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ export abstract class Api extends Resource {
* @internal
*/
protected _validatePath(path: string) {
if (!/^([^\{\}\:\n]|.+\/\{\w+\}(\/|$))*$/g.test(path)) {
if (
!/^(\/[a-zA-Z0-9_\-]+(\/\{[a-zA-Z0-9_\-]+\}|\/[a-zA-Z0-9_\-]+)*(?:\?[^#]*)?)?$|^(\/\{[a-zA-Z0-9_\-]+\})*\/?$/g.test(
path
)
) {
throw new Error(
`Invalid path ${path}. Url cannot contain ":", params contains only alpha-numeric chars or "_".`
);
Expand Down
Loading

0 comments on commit f123739

Please sign in to comment.