Skip to content

[BUG][typescript-angular] Angular service doesn't convert uniqueItems to set #14055

@jdgarvey

Description

@jdgarvey

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

#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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions