OpenAPITools / OpenAPITools/openapi-generator

[BUG][typescript-fetch] Dictionaries with arrays of objects with custom types are not deserialized using custom deserializer

Open
#21,730 1 comment 2 reactions 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
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • 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

With dictionaries of strings to values, and the value being an array of objects of another custom type, these arrays of objects aren't run through the generated custom deserializer. This results in JSON not being turned into instances of objects properly.

With the typescript-fetch and the below OpenAPI specification, FooResponse.ts is generated and contains this:

return {

    'dictionaryToArrayOfTypes': json['dictionaryToArrayOfTypes'] == null ? undefined : json['dictionaryToArrayOfTypes'],
};

However, json['dictionaryToArrayOfTypes'] is taken directly and put into dictionaryToArrayOfTypes instead of looping over the values and calling BarFromJSON on them. If Bar in turn contains other types such as Date, they aren't deserialized into a Date object.

openapi-generator version

7.14.0

OpenAPI declaration file content or url
{
  "openapi" : "3.0.1",
  "info": {
    "title": "Test",
    "description": "Test.",
    "contact": {
      "name": "Foo",
      "url": "https://www.foo.com",
      "email": "support@foo.com"
    },
    "version": "v1"
  },
  "paths" : {
    "/api/Foo" : {
      "post" : {
        "operationId" : "Foo",
        "requestBody" : {
          "content" : {
            "application/json" : {
              "schema" : {
                "$ref" : "#/components/schemas/FooRequest"
              }
            }
          }
        },
        "responses" : {
          "default" : {
            "description" : "default response",
            "content" : {
              "application/json" : {
                "schema" : {
                  "$ref" : "#/components/schemas/FooResponse"
                }
              }
            }
          }
        }
      }
    }
  },
  "components" : {
    "schemas" : {
        "FooRequest" : {
          "type" : "object",
          "properties" : {
            "version" : {
              "type" : "integer",
              "format" : "int64"
            }
          }
          },
      "FooResponse" : {
        "type" : "object",
        "properties" : {
          "dictionaryToArrayOfTypes" : {
            "type" : "object",
            "additionalProperties" : {
              "type" : "array",
              "items" : {
                "$ref" : "#/components/schemas/Bar"
              }
            }
          }
        }
      },
      "Bar" : {
        "type" : "object",
        "properties" : {
          "someString" : {
            "type" : "string"
          }
        }
      }
    }
  }
}
Generation Details
docker run \
    --rm \
    -eJAVA_OPTS="-Dio.swagger.parser.util.RemoteUrl.trustAll=true -Dio.swagger.v3.parser.util.RemoteUrl.trustAll=true" \
    -v "${PWD}":/local \
    openapitools/openapi-generator-cli:v7.14.0 \
    generate \
    -i "swagger-spec.json" \
    -g typescript-fetch \
    --additional-properties=disallowAdditionalPropertiesIfNotPresent=false,enumPropertyNaming=UPPERCASE,supportsES6=true,validationAttributes=false,withInterfaces=true \
    -o "/local/generated-client"
Steps to reproduce
  1. Run the above script with the specified specification.
Suggest a fix

The following happens for arrays of objects of a certain type when they aren't part of a dictionary, and something similar should probably also happen here:

'arrayOfTypes': json['arrayOfTypes'] == null ? undefined : ((json['arrayOfTypes'] as Array<any>).map(BarFromJSON)),

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

Start by reproducing the issue with the provided swagger-spec.json and typescript-fetch Docker command, then inspect the generated FooResponse.ts. Compare dictionaryToArrayOfTypes with the shown arrayOfTypes mapping. Done means dictionary values containing arrays of Bar objects invoke BarFromJSON for each item, including nested fields such as Date.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.