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 56 forks source link

Generated Schema types are not restrictive (not sealed) #4761

Open uroslates opened 3 months ago

uroslates commented 3 months ago

Is your feature request related to a problem? Please describe. Actual: We have a problem with having & Record<string, any> added at each generated schema type definition. This does not seal (restrict additional properties) to the schema.

Describe the solution you'd like Expected: In ideal Scenario there would be a flag (to a generate function - additional generate option) that controls this feature - either makes the generated Schema files strict (what we refer to as sealed) or if set to false (default behaviour) would add the & Record<string, any> type union, allowing the types to be flexible (non restrictive).

Impact / Priority

The problematic code is relevant to serializeObjectSchema function.

Example: Instead of having schema file outputs from openapi generate function like this:

export type AddToCartEntryInputBO = {
  quantity: number;
  ...
} & Record<string, any>;

Would be good to have them (if correct generate options flag is set) like this:

export type AddToCartEntryInputBO = {
  quantity: number;
  ...
};
jjtang1985 commented 3 months ago

Hi @uroslates ,

Thanks for raising up. Before talking about the solution, I would like to understand the root cause of & Record<string, any>, because we are woking on some improvement for this direction.

Maybe @shibeshduw can correct me, it seems this is related to the special keywords of the specification file like allOf, anyOf, oneOf.

@uroslates , please share a minimal example of the spec, so we can reproduce the issue.

Thank you!

Best regards, Junjie

shibeshduw commented 3 months ago

Hey @jjtang1985, this is not related to the special keywords in the spec. I believe this is the default behavior with how OpenAPI interprets schemas. In an object schema, even if additionalProperties is not specified, it is implicitly allowed (defaults to true), which means the object can have additional properties of any type. While parsing the spec, we default to type: any so that this behavior is captured. This in turn, causes the serializer code to append & Record<string, any> to the schema type definition.

jjtang1985 commented 3 months ago

Thank you, @shibeshduw.

@uroslates , I'll keep this feature request open, but I'm not sure whether we will work on this feature requests or not, due to the current capacity.