OpenAPITools / OpenAPITools/openapi-generator

[Question] [typescript-axios] How to avoid manually passing the discriminator/mapping values

Open
#15,323 2 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hey!

After generating the typescript-axios client using the following scheme which includes a discriminator, propertyName ($type) and mapping

scheme
{
  "openapi": "3.0.1",
  "info": {
    "title": "Example API",
    "version": "1.0.0"
  },
  "components": {
    "schemas": {
      "Pet": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "$type": {
            "type": "string"
          }
        },
        "required": [
          "name",
          "$type"
        ],
        "discriminator": {
          "propertyName": "$type",
          "mapping": {
            "dog": "#/components/schemas/Dog",
            "cat": "#/components/schemas/Cat"
          }
        }
      },
      "Dog": {
        "allOf": [
          {
            "$ref": "#/components/schemas/Pet"
          },
          {
            "type": "object",
            "properties": {
              "breed": {
                "type": "string"
              }
            },
            "required": [
              "breed"
            ]
          }
        ]
      },
      "Cat": {
        "allOf": [
          {
            "$ref": "#/components/schemas/Pet"
          },
          {
            "type": "object",
            "properties": {
              "clawed": {
                "type": "boolean"
              }
            },
            "required": [
              "clawed"
            ]
          }
        ]
      }
    }
  },
  "paths": {
    "/pets": {
      "get": {
        "summary": "Get a list of pets",
        "responses": {
          "200": {
            "description": "A list of pets",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Pet"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

the generated code includes the discriminator propertyName ($type) as any other property of the model:

/**
 * 
 * @export
 * @interface Pet
 */
export interface Pet {
    /**
     * 
     * @type {string}
     * @memberof Pet
     */
    'name': string;
    /**
     * 
     * @type {string}
     * @memberof Pet
     */
    '$type': string;
}

With this code, we will have to manually pass on $type the corresponding value of the mapping to any instance of Cat or Dog that we create before sending it to the api.

Is there a way to avoid doing that with this client?

Looking at the generated code of the same scheme but using the typescript-fetch client we found that there is a function that solve the problem:

export function PetFromJSONTyped(json: any, ignoreDiscriminator: boolean): Pet {
    if ((json === undefined) || (json === null)) {
        return json;
    }
    if (!ignoreDiscriminator) {
        if (json['$type'] === 'cat') {
            return CatFromJSONTyped(json, true);
        }
        if (json['$type'] === 'dog') {
            return DogFromJSONTyped(json, true);
        }
    }
    return {
        
        'name': json['name'],
        '$type': json['$type'],
    };
}

Is there a way to generate something similar on typescript-axios?

Here is the repo with the typescript-axios client code used on this question: https://github.com/johanbv/typescript-axios-client

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 comparing the generated typescript-axios client with the typescript-fetch output, especially PetFromJSONTyped and its discriminator handling. The change is complete when typescript-axios can generate equivalent discriminator behavior so Cat and Dog instances do not require manually supplied mapping values.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.