OpenAPITools / OpenAPITools/openapi-generator

[BUG] [typescript-fetch] Named array types don't correctly deserialise elements

Open
#15,404 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
Description

Generating typescript-fetch code for an item which is an named array type produces code which doesn't correctly deserialise elements

openapi-generator version

6.5.0

OpenAPI declaration file content or url
openapi: "3.0.2"
info:
  title: Test
  version: "1.0"
servers:
  - url: https://api.server.test/v1
paths:
  /test:
    get:
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/MyArray"

components:
  schemas:
    MyArray:
      type: array
      items:
        type: object
        properties:
          my_prop:
            type: string
        required:
          - my_prop
Generation Details

The code was generated with

npx @openapitools/openapi-generator-cli generate -i test.yml -g typescript-fetch --additional-properties=typescriptThreePlus=true --generate-alias-as-model -o test/

This generates MyArray.ts containing

export function MyArrayFromJSONTyped(json: any, ignoreDiscriminator: boolean): MyArray {
    return json;
}

export function MyArrayToJSON(value?: MyArray | null): any {
    return value;
}

The json is directly cast to MyArray rather than calling MyArrayInnerFromJSONTyped or MyArrayInnerToJSON.

Workaround

Changing the schema to

openapi: "3.0.2"
info:
  title: Test
  version: "1.0"
servers:
  - url: https://api.server.test/v1
paths:
  /test:
    get:
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  properties:
                    my_prop:
                      type: string
                  required:
                    - my_prop

Produces the working typescript

    async testGetRaw(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<Array<TestGet200ResponseInner>>> {
        const queryParameters: any = {};

        const headerParameters: runtime.HTTPHeaders = {};

        const response = await this.request({
            path: `/test`,
            method: 'GET',
            headers: headerParameters,
            query: queryParameters,
        }, initOverrides);

        return new runtime.JSONApiResponse(response, (jsonValue) => jsonValue.map(TestGet200ResponseInnerFromJSON));
    }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the provided OpenAPI YAML and typescript-fetch generation command. Start by inspecting the generated MyArray.ts and compare its named-array serialization with the inline-array workaround shown in the report. Done means named array elements are deserialized and serialized through the generated inner conversion functions.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.