SAP / cloud-sdk-js

Use the SAP Cloud SDK for JavaScript / TypeScript to reduce development effort when building applications on SAP Business Technology Platform that communicate with SAP solutions and services such as SAP S/4HANA Cloud, SAP SuccessFactors, and many others.
Apache License 2.0
162 stars 55 forks source link

OpenAPI generator generates invalid code in specific combinations of required/optional header and query parameters #4928

Closed sven-petersen closed 3 weeks ago

sven-petersen commented 1 month ago

Describe the bug

The OpenAPI generator generates invalid JavaScript/TypeScript code when having both, optional and required, header parameters as well as only optional query parameters. The resulting typescript error is: A required parameter cannot follow an optional parameter.ts(1016)

To Reproduce

Generate an OpenAPI client using the OpenAPI schema from below. Check the generated code using an IDE or try to build it with tsc to see the error I mentioned above.

Interestingly correct code will be generated when the OptionalHeaderParam in the header is removed. In this case the queryParameter becomes a required function parameter. In the generate code this looks like followed:

// with the "OptionalHeaderParam"
sampelOperation: (
    queryParameters?: { optionalQueryParam?: number },
    headerParameters: {
      "required-header-param": string;
      "optional-header-param"?: string;
    },
  ) => { /* ... */ }

becomes

// without the OptionalHeaderParam
sampelOperation: (
    queryParameters: { optionalQueryParam?: number },
    headerParameters: { "required-header-param": string },
  ) => { /* ... */ }

Expected behavior

Generate valid TS code.

Used Versions:

Code Examples

openapi: "3.0.3"
info:
  version: "0.1.1"
  title: "Minimal example"
security:
  - mtls: []
paths:
  /samplePath:
    get:
      operationId: sampelOperation
      parameters:
        - $ref: "#/components/parameters/RequiredHeaderParam"
        - $ref: "#/components/parameters/OptionalHeaderParam"
        - $ref: "#/components/parameters/OptionalQueryParam"
      responses:
        "200":
          description: "A response"
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SampleResponseSchema"
components:
  parameters:
    RequiredHeaderParam:
      name: required-header-param
      in: header
      required: true
      schema:
        type: string
    OptionalHeaderParam:
      name: optional-header-param
      in: header
      required: false
      schema:
        type: string
    OptionalQueryParam:
      name: optionalQueryParam
      in: query
      required: false
      schema:
        type: integer
  schemas:
    SampleResponseSchema:
      type: object
      properties:
        content:
          type: string

will generate this code

// removed comments for better readability
import { OpenApiRequestBuilder } from "@sap-cloud-sdk/openapi";
import type { SampleResponseSchema } from "./schema/index.js";

export const DefaultApi = {
  sampelOperation: (
    queryParameters?: { pageSize?: number },
    headerParameters: { // this is not possible has queryParameters is already optional
      "required-header-param": string;
      "optional-header-param"?: string;
    },
  ) =>
    new OpenApiRequestBuilder<SampleResponseSchema>("get", "/samplePath", {
      queryParameters,
      headerParameters,
    }),
};

which has this error: image

Impact / Priority

Impact: Cannot upgrade to current OpenAPI generator version

Timeline: N/A

Additional context

The feature to consider parameters in headers was recently added (see release notes for 3.16):

[openapi-generator] Generate OpenAPI clients with header parameters. (aa0b849)

marikaner commented 1 month ago

Hey @sven-petersen, we are looking into this. Apparently a mistake in a new feature we recently released.

deekshas8 commented 3 weeks ago

hi @sven-petersen ,

We just released v3.19.0 that fixes this issue. Please try it out.