Skip to content

[BUG][Typescript-Fetch] Incorrect toJSON & fromJSONTyped functions when using oneOf #5202

@jamesopti

Description

@jamesopti

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
Description

For a property defined using oneOf, invalid methods are generated for <Model>FromJSONTyped and for <Model>ToJSON:

See the nextUrl field below:

/**
 * 
 * @export
 * @interface Todos
 */
export interface Todos {
    /**
     * List of links to next pages of results in a series.   The first element in the array is the exact next page after the current record, etc. 
     * @type {string | UrlList}
     * @memberof Todos
     */
    nextUrl?: string | UrlList;
}

export function TodosFromJSON(json: any): Todos {
    return TodosFromJSONTyped(json, false);
}

export function TodosFromJSONTyped(json: any, ignoreDiscriminator: boolean): Todos {
    if ((json === undefined) || (json === null)) {
        return json;
    }
    return {
        // THIS IS INVALID TYPESCRIPT
        'nextUrl': !exists(json, 'next_url') ? undefined : string | UrlListFromJSON(json['next_url']),
    };
}

export function TodosToJSON(value?: Todos | null): any {
    if (value === undefined) {
        return undefined;
    }
    if (value === null) {
        return null;
    }
    return {
        // THIS IS INVALID TYPESCRIPT
        'next_url': string | UrlListToJSON(value.nextUrl),
    };
}

Expected

For both of those fields, I would expect a typeof check to decide what to do:

'next_url': typeof value.nextUrl === 'string' ? value.nextUrl : UrlListToJSON(value.nextUrl),
openapi-generator version

4.2.3 via

yarn list v1.21.0
└─ @openapitools/[email protected]
OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Todo API
  description: A reference API description for a Todo service
  version: 1.0.0
servers:
  - url: http://localhost:8000/v1
    description: Generic HTTP
paths:
  /todos:
    get:
      summary: List the available todo tasks
      operationId: ListTodos
      responses:
        '200':
          description: a list of Todos
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Todos'
components:
  schemas:
    urlValue:
      type: string
      description: A single url.
    urlList:
      type: array
      description: A list of urls.
      items:
        type: string
    Todos:
      type: object
      properties:
        next_url:
          description: |
            List of links to next pages of results in a series.

             The first element in the array is the exact next page after the current record, etc.
          oneOf:
            - $ref: '#/components/schemas/urlValue'
            - $ref: '#/components/schemas/urlList'
Command line used for generation

./node_modules/@openapitools/openapi-generator-cli/bin/openapi-generator generate -g typescript-fetch -i ./openapi.yaml -o .

CONFIG: - "generateAliasAsModel": true

If generateAliasAsModel is false (default) then the generated Todos model is also incorrect:

export interface Todos {
    /**
     * List of links to next pages of results in a series.   The first element in the array is the exact next page after the current record, etc. 
     * @type {string | Array}
     * @memberof Todos
     */
    nextUrl?: string | Array;  // THIS IS INVALID TYPESCRIPT
}

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