Bug Report Checklist
Description
Running into a typing issue when using uniqueItems and an Angular service.
When I have a schema that contains an array property with uniqueItems set to true, the generated typescript model is typed to a Set.
Schema
TestDto:
required:
- a
type: object
properties:
a:
uniqueItems: true
type: array
items:
type: string
Generated Model
export interface TestDto {
a: Set<string>;
}
However, the corresponding generated Angular service does not do anything to convert the array that comes back from the API to a Set, so the result is a value that is typed to a set but actually contains an array. This breaks any consuming code that is using the correct type.
Return from the Angular Service
return this.httpClient.request<TestDto>('get', `${this.configuration.basePath}${localVarPath}`,
{
context: localVarHttpContext,
responseType: <any>responseType_,
withCredentials: this.configuration.withCredentials,
headers: localVarHeaders,
observe: observe,
reportProgress: reportProgress
}
);
Example where this would break
import { TestDto } from "./output";
const jsonApiResponse = {a: ['foo', 'bar']};
const receivingVariable: TestDto = jsonApiResponse;
// Results in error:
// Type '{ a: string[]; }' is not assignable to type 'TestDto'.
// Types of property 'a' are incompatible.
// Type 'string[]' is missing the following properties from type 'Set<string>': add, clear, delete, has, and 2 more.
We are currently converting the response to unknown and then to our own model to get around this issue.
openapi-generator version
6.2.1
OpenAPI declaration file content or url
https://github.com/jdgarvey/openapi-unique-items-bug/blob/main/spec.yml
Generation Details
npx openapi-generator-cli generate -g typescript-angular -i spec.yml -o output
Steps to reproduce
Reproduction repository: https://github.com/jdgarvey/openapi-unique-items-bug
You can view the spec there, then run npx openapi-generator-cli generate -g typescript-angular -i spec.yml -o output to update output if you want.
Related issues/PRs
#11746
Suggest a fix
As far as I can tell, we would need to recursively traverse the httpClient response JSON and somehow convert arrays to sets if that corresponding property has uniqueItems: true.
Bug Report Checklist
Description
Running into a typing issue when using
uniqueItemsand an Angular service.When I have a schema that contains an array property with
uniqueItemsset to true, the generated typescript model is typed to aSet.Schema
Generated Model
However, the corresponding generated Angular service does not do anything to convert the array that comes back from the API to a Set, so the result is a value that is typed to a set but actually contains an array. This breaks any consuming code that is using the correct type.
Return from the Angular Service
Example where this would break
We are currently converting the response to unknown and then to our own model to get around this issue.
openapi-generator version
6.2.1
OpenAPI declaration file content or url
https://github.com/jdgarvey/openapi-unique-items-bug/blob/main/spec.yml
Generation Details
npx openapi-generator-cli generate -g typescript-angular -i spec.yml -o outputSteps to reproduce
Reproduction repository: https://github.com/jdgarvey/openapi-unique-items-bug
You can view the spec there, then run
npx openapi-generator-cli generate -g typescript-angular -i spec.yml -o outputto update output if you want.Related issues/PRs
#11746
Suggest a fix
As far as I can tell, we would need to recursively traverse the httpClient response JSON and somehow convert arrays to sets if that corresponding property has uniqueItems: true.