Skip to content
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

[Improvement]: Support for HTTP resources with trailing slashes #41927

Closed
slahirucd7 opened this issue Jan 3, 2024 · 3 comments
Closed

[Improvement]: Support for HTTP resources with trailing slashes #41927

slahirucd7 opened this issue Jan 3, 2024 · 3 comments
Labels
Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Improvement userCategory/Compilation

Comments

@slahirucd7
Copy link

Description

Considere a scenario where there are two HTTP resources as /abc and /abc/
In ballerina those two resources are considering as the same.
But in other languages those are considered as two different resources.

Describe your problem(s)

import ballerina/http;


service on new http:Listener(9092) {

    resource function get rsc() returns string {
        return "without trailing slash";
    }

    resource function get rsc\/() returns string {
        return "with trailing slash";
    }
    resource function get rsc/[string a]() returns string {
        return "with trailing slash and path param " + a;
    }
    // This is not allowed to have in ballerina
    // resource function get rsc/[string a]\/() returns string {
    //     return "with trailing slash and path param" + a;
    // }
}

image

Describe your solution(s)

Provide the support to consider resource with trailing slash as a separate API resource.

Related area

-> Compilation

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@TharmiganK
Copy link
Contributor

Here the actual requirement is to differentiate the business logic in two different resources for requests coming in paths /abc and /abc/. Even though it is not recommended to have both resources, other languages like spring boot support this.

service /api on new http:Listener(9090) {

    resource function get abc() returns http:Response {
        // Logic for requests with path `/api/abc`
    }

    resource function get abc/() returns http:Response {
        // Logic for requests with path `/api/abc/`
    }
}

This is not supported in Ballerina since the Ballerina specifications does not allow trailing slashes in path segment. The slash should be followed by either a path-segment or a path-rest-parameter.

So to support this, we should have a single resource and then access the raw path to execute the logic

service /api on new http:Listener(9090) {

    resource function get abc(http:Request req) returns http:Response {
        string rawPath = req.rawPath;
        if rawPath.endsWith("/") {
            // Logic for requests with path `/api/abc/`
        } else {
            // Logic for requests with path `/api/abc`
        }
    }
}

Adding @MaryamZi and @shafreenAnfar for their insights

@MaryamZi
Copy link
Member

MaryamZi commented Jan 3, 2024

There is an open spec issue related to this - ballerina-platform/ballerina-spec#677

@MaryamZi MaryamZi changed the title [Improvement]: Support for HTTP resources with tailing slashes [Improvement]: Support for HTTP resources with trailing slashes Jan 3, 2024
@MaryamZi MaryamZi added Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. and removed needTriage The issue has to be inspected and labeled manually labels Jan 3, 2024
@MaryamZi
Copy link
Member

MaryamZi commented Jan 5, 2024

Closing this since there is an open spec issue to track this.

@MaryamZi MaryamZi closed this as completed Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Improvement userCategory/Compilation
Projects
None yet
Development

No branches or pull requests

4 participants