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

[Phase 02] Support resource functions in OpenAPI Client Generation tool #5158

Closed
lnash94 opened this issue Aug 3, 2022 · 2 comments
Closed

Comments

@lnash94
Copy link
Member

lnash94 commented Aug 3, 2022

Description:
Parent issue: #5366

During this 2nd phase, try to improve the client resource method body with the client resource method call in below way

string response = check httpClient->/some/endpoint(payload, headers, "application/json", name = "foo", id = 123);

Current Generated code

resource isolated function get api/v2/opportunities(string? embed = ()) returns OpportunityObject|error {
        string resourcePath = string `/api/v2/opportunities`;
        map<anydata> queryParam = {"embed": embed};
        resourcePath = resourcePath + check getPathForQueryParam(queryParam);
        OpportunityObject response = check self.clientEp->get(resourcePath);
        return response;
    }

With Improvement

resource isolated function get api/v2/opportunities(string? embed = ()) returns OpportunityObject|error {
        OpportunityObject response = check self.clientEp->/api/v2/opportunities/.get(embed);
        return response;
    }
@lnash94
Copy link
Member Author

lnash94 commented Aug 16, 2022

Once we are going to check the feasibility of this improvement in the client resource function we encounter this ,

  • Given query parameters has parameter serialization details and the resource path should be included the details of the serialization.
    Example can be found here.
    For Stripe API there are scenarios where we have to send data structures like objects as query parameters.
    In such cases we need to serialize the parameter in one of the below ways
    Screenshot from 2021-08-16 15-08-59

Other than query parameters, object can be send as a path parameter or header according to the OpenAPI 3.0 . Please find more information on related to parameter serialization in OpenAPI 3.0 here.

This scenario was handled previously by introducing a util function to generate the path according to the serialization.

resourcePath = resourcePath + check getPathForQueryParam(queryParam);
------------
# functions to generate query parameter path
isolated function  getPathForQueryParam(map<anydata> queryParam)  returns  string|error {
    string[] param = [];
    param[param.length()] = "?";
    foreach  var [key, value] in  queryParam.entries() {
        if  value  is  () {
            _ = queryParam.remove(key);
        } else {
            if  string:startsWith( key, "'") {
                 param[param.length()] = string:substring(key, 1, key.length());
            } else {
                param[param.length()] = key;
            }
            param[param.length()] = "=";
            if  value  is  string {
                string updateV =  check url:encode(value, "UTF-8");
                param[param.length()] = updateV;
            } else {
                param[param.length()] = value.toString();
            }
            param[param.length()] = "&";
        }
    }
    _ = param.remove(param.length()-1);
    if  param.length() ==  1 {
        _ = param.remove(0);
    }
    string restOfPath = string:'join("", ...param);
    return restOfPath;
}

but given the improvement resource method

OpportunityObject response = check self.clientEp->/api/v2/opportunities.get(embed);

we can't handle the parameter serialization. Therefore we have to move with the current implementation [1]
until we receive some support from http to handle parameter serialization-related issues #1747
[1]

resource isolated function get api/v2/opportunities(string? embed = ()) returns OpportunityObject|error {
        string resourcePath = string `/api/v2/opportunities`;
        map<anydata> queryParam = {"embed": embed};
        resourcePath = resourcePath + check getPathForQueryParam(queryParam);
        OpportunityObject response = check self.clientEp->get(resourcePath);
        return response;
    }

@lnash94 lnash94 added the Reason/MultipleComponentInteraction Issue occured due to interactions in multiple components. label Sep 28, 2022
@lnash94 lnash94 transferred this issue from ballerina-platform/openapi-tools Nov 1, 2023
@lnash94 lnash94 added the IceBox label Oct 1, 2024
@lnash94
Copy link
Member Author

lnash94 commented Oct 1, 2024

Due to above mentioned limitation, we will close this issue, once there is a way to capture those details, this will be opened

@lnash94 lnash94 closed this as completed Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

No branches or pull requests

1 participant