Bug Report Checklist
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
#8258
#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.
Bug Report Checklist
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:
Actual output:
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 ./generatedSteps to reproduce
Just invoke
openapi-generator-cli generate -i https://gist.githubusercontent.com/FlorianWege/d27e0114791e89c29e06d1aa881a82d7/raw/2b6b6dc8620a393d6d2788b111c486059a44cc03/api.json -g typescript-fetch -o ./generatedThen look at
./generated/src/models/ArrayOfPrimitives.tsand./generated/src/models/ArrayOfObjects.tsRelated issues/PRs
#8258
#8695
Suggest a fix
Since
JSON.stringifyserializes 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
isPrimitiveTypeis falsy ORisPrimitiveTypeshould refer to the total type rather than the type of the elements in the array.