ballerina-platform / ballerina-library

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

Getting an error when `get` operation has request body #5077

Open kalaiyarasiganeshalingam opened 11 months ago

kalaiyarasiganeshalingam commented 11 months ago

Description: Task: https://github.com/ballerina-platform/ballerina-standard-library/issues/4952

Some Microsoft Excel get APIs have a request body. Eg: https://learn.microsoft.com/en-us/graph/api/range-boundingrect?view=graph-rest-1.0

OpenAPI Spec for the above API:

/me/drive/items/{itemId}/workbook/names/{namedItemName}/range/boundingRect:
    get:
      summary: Gets the smallest range that encompasses the given ranges.
      operationId: getBoundingRect
      tags:
        - range
      parameters:
        - $ref: '#/components/parameters/itemId'
        - $ref: '#/components/parameters/namedItemName'
        - $ref: '#/components/parameters/sessionId'
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AnotherRange'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Range'

Getting this error when converting this spec to Ballerina:

OpenAPI definition has errors: 
GET operation cannot have a requestBody. Error at operationId: getBoundingRect
ayeshLK commented 2 months ago

In Ballerina there is a restriction not allowing request-body for an HTTP GET request. But, in the OpenAPI specification there is no restriction for this and hence, there can be OpenAPI specifications which contains a requestBody definition for HTTP GET endpoints. In such cases the developer has to manually edit the OpenAPI spec and remove requestBody definition from the relevant endpoints and continue the code-generation.

But since not-allowing a request-body for an HTTP GET is a Ballerina level restriction we should provide a better experience in such cases rather than stopping the code-generation.

An example OpenAPI spec can be found here [1]

[1] - https://github.com/stripe/openapi/blob/master/openapi/spec3.json