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
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 },
) => { /* ... */ }
// 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:
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)
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 thequeryParameter
becomes a required function parameter. In the generate code this looks like followed:becomes
Expected behavior
Generate valid TS code.
Used Versions:
20.11.1
10.2.4
3.18.1
Code Examples
will generate this code
which has this error:
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):