ballerina-platform / ballerina-library

The Ballerina Library
https://ballerina.io/learn/api-docs/ballerina/
Apache License 2.0
137 stars 58 forks source link

Problematic Mapping of Certain OpenAPI Spec Parameters to Ballerina Function Parameters #4882

Closed RDPerera closed 5 months ago

RDPerera commented 11 months ago

Description: The Ballerina OpenAPI tool currently generates conflicting mappings for some OpenAPI Spec parameters to Ballerina function parameters. Specifically, when attempting to map a parameter like 'Param<' to a Ballerina variable, the tool omits the '<' character.However, this mapping becomes problematic when there is another parameter with a similar name, resulting in a compilation error.

For example, consider following OpenAPISpec.yml,

    - name: StartTime
      in: query
      description: 'Only include calls that started on this date.'
      schema:
        type: string
        format: date-time
    - name: StartTime<
      in: query
      description: 'StartTime<=YYYY-MM-DD to read calls that started on or before midnight of this date.'
      schema:
        type: string
        format: date-time
    - name: StartTime>
      in: query
      description: 'StartTime>=YYYY-MM-DD to read calls that started on or after midnight of this date.'
      schema:
        type: string
        format: date-time

Open API tool generate following client code for above yml,

resource isolated function get '2010\-04\-01/Accounts/[string accountSid]/Calls\.json(string? to = (), string? 'from = (), string? parentCallSid = (), Call_enum_status? status = (), 
        string? startTime = (), string? startTime = (), string? startTime = (),
        string? endTime = (), string? endTime = (), string? endTime = (), 
        int? pageSize = (), int? page = (), string? pageToken = ()) returns ListCallResponse|error {

        string resourcePath = string `/2010-04-01/Accounts/${getEncodedUri(accountSid)}/Calls.json`;

        map<anydata> queryParam = {"To": to, "From": 'from, "ParentCallSid": parentCallSid, "Status": status, 
        "StartTime": startTime, "StartTime<": startTime, "StartTime>": startTime,
        "EndTime": endTime, "EndTime<": endTime, "EndTime>": endTime,
         "PageSize": pageSize, "Page": page, "PageToken": pageToken};

        resourcePath = resourcePath + check getPathForQueryParam(queryParam);
        ListCallResponse response = check self.clientEp->get(resourcePath);
        return response;
    }

The tool omits the '<' character resulting following compilation errors,

Compiling source
        ballerinax/twilio:2.0.0
ERROR [client.bal:(541:219,541:228)] redeclared symbol 'startTime'
ERROR [client.bal:(541:243,541:252)] redeclared symbol 'startTime'

Steps to reproduce: Above example taken from generating a Twillio Connector for this Twilio's OpenAPI Specification.

  1. Obtain the Twilio API specification in YAML format (e.g., twillio.yaml).
  2. Use the Ballerina OpenAPI tool to generate a Ballerina client connector for the Twilio API specification: bal openapi -i twillio.yaml --mode client
  3. Attempt to compile the generated Ballerina code.
github-actions[bot] commented 5 months ago

This issue is NOT closed with a proper Reason/ label. Make sure to add proper reason label before closing. Please add or leave a comment with the proper reason label now.

      - Reason/EngineeringMistake - The issue occurred due to a mistake made in the past.
      - Reason/Regression - The issue has introduced a regression.
      - Reason/MultipleComponentInteraction - Issue occured due to interactions in multiple components.
      - Reason/Complex - Issue occurred due to complex scenario.
      - Reason/Invalid - Issue is invalid.
      - Reason/Other - None of the above cases.