[BUG][typescript-fetch] Set of Primitives not serialized as Array
Created by: FlorianWege
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
Have you tested with the latest master to confirm the issue still exists? -
Have you searched for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
uniqueItems arrays from openapi specifications are treated as TypeScript Sets but, in contrast to uniqueItems arrays of object types, uniqueItems arrays of primitive types like string are not converted from Set to Array in the generated ToJSON functions.
Expected output for the example:
export function ArrayOfPrimitivesToJSON(value?: ArrayOfObjects | null): any {
if (value === undefined) {
return undefined;
}
if (value === null) {
return null;
}
return {
'tagIds': value.tagIds === undefined ? undefined : Array.from(value.tagIds as Set<any>),
};
}
Actual output:
export function ArrayOfPrimitivesToJSON(value?: ArrayOfPrimitives | null): any {
if (value === undefined) {
return undefined;
}
if (value === null) {
return null;
}
return {
'tagIds': value.tagIds,
};
}
openapi-generator version
5.3.0
OpenAPI declaration file content or url
https://gist.github.com/FlorianWege/d27e0114791e89c29e06d1aa881a82d7
Generation Details
openapi-generator-cli generate -i https://gist.githubusercontent.com/FlorianWege/d27e0114791e89c29e06d1aa881a82d7/raw/2b6b6dc8620a393d6d2788b111c486059a44cc03/api.json -g typescript-fetch -o ./generated
Steps to reproduce
Just invoke
openapi-generator-cli generate -i https://gist.githubusercontent.com/FlorianWege/d27e0114791e89c29e06d1aa881a82d7/raw/2b6b6dc8620a393d6d2788b111c486059a44cc03/api.json -g typescript-fetch -o ./generated
Then look at ./generated/src/models/ArrayOfPrimitives.ts
and ./generated/src/models/ArrayOfObjects.ts
Related issues/PRs
https://github.com/OpenAPITools/openapi-generator/issues/8258 https://github.com/OpenAPITools/openapi-generator/pull/8695
Suggest a fix
Since JSON.stringify
serializes Sets as empty JSON objects regardless of the elements contained, there seems to be little point in serializing Sets. Also, there appears to be a compatibility issue with Java's Jackson, which wants to have JSON arrays when deserializing into Java Sets (actually I generated the openapi specification from the Java backend with springdoc-openapi-maven-plugin, which yielded those uniqueItems arrays).
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache#L109: A problem seems to be that the array/uniqueItems handling here is only done in the case that isPrimitiveType
is falsy OR isPrimitiveType
should refer to the total type rather than the type of the elements in the array.