OpenAPITools / OpenAPITools/openapi-generator
[BUG][typescript-angular] Angular service doesn't convert uniqueItems to set
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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
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
https://github.com/OpenAPITools/openapi-generator/issues/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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked spec.yml and reproduce the issue using the typescript-angular generator command in the report. Inspect the generated Angular service response handling and compare it with the generated TestDto type. Done means an API array for a uniqueItems property is converted to the corresponding Set without breaking normal responses.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- angular, typescript
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100