OpenAPITools / OpenAPITools/openapi-generator

[BUG][typescript-fetch] Generated code drops some values when using oneOf

Open
#21,878 2 comments 0 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

The typescript-fetch generator generates code for models that drops values that should not be dropped when using oneOf with type: object, {type: array, items: {nullable: true}} or {type: array, items: {type: object}}. Take a look at the following OpenAPI declaration:

openapi: "3.0.2"
info:
  title: Foo
  version: 1.0.0

paths:
  /foo:
    post:
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - type: object
                - type: array
                  items:
                    nullable: true
      responses:
        204:
          description: "Ok"

  /bar:
    post:
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - type: object
                - type: array
                  items:
                    type: object
      responses:
        204:
          description: "Ok"

It generates

// models.FooPostRequest.ts
/* tslint:disable */
/* eslint-disable */
/**
 * Foo
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * The version of the OpenAPI document: 1.0.0
 * 
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */

/**
 * @type FooPostRequest
 * 
 * @export
 */
export type FooPostRequest = Array<any | null> | object;

export function FooPostRequestFromJSON(json: any): FooPostRequest {
    return FooPostRequestFromJSONTyped(json, false);
}

export function FooPostRequestFromJSONTyped(json: any, ignoreDiscriminator: boolean): FooPostRequest {
    if (json == null) {
        return json;
    }
    return {} as any;
}

export function FooPostRequestToJSON(json: any): any {
    return FooPostRequestToJSONTyped(json, false);
}

export function FooPostRequestToJSONTyped(value?: FooPostRequest | null, ignoreDiscriminator: boolean = false): any {
    if (value == null) {
        return value;
    }
    return {};
}
// BarPostRequest.ts
/* tslint:disable */
/* eslint-disable */
/**
 * Foo
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * The version of the OpenAPI document: 1.0.0
 * 
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */

/**
 * @type BarPostRequest
 * 
 * @export
 */
export type BarPostRequest = Array<object> | object;

export function BarPostRequestFromJSON(json: any): BarPostRequest {
    return BarPostRequestFromJSONTyped(json, false);
}

export function BarPostRequestFromJSONTyped(json: any, ignoreDiscriminator: boolean): BarPostRequest {
    if (json == null) {
        return json;
    }
    return {} as any;
}

export function BarPostRequestToJSON(json: any): any {
    return BarPostRequestToJSONTyped(json, false);
}

export function BarPostRequestToJSONTyped(value?: BarPostRequest | null, ignoreDiscriminator: boolean = false): any {
    if (value == null) {
        return value;
    }
    return {};
}

The RequestFromJSONTyped and RequestToJSONTyped functions do not return the desired array or object values and instead replace them with an empty object.

openapi-generator version

7.15

Generation Details

typescript-fetch generator with default settings.

Steps to reproduce

Take the OpenAPI definition from above and run the OpenAPI generator with the typescript-fetch generator.

Related issues/PRs
Suggest a fix

I believe the modelOneOf.mustache template is missing some cases. I will try to open a pull request.

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 with the typescript-fetch generator and its modelOneOf.mustache template, then reproduce the issue using the OpenAPI definition and default settings described here. Confirm that the generated RequestFromJSONTyped and RequestToJSONTyped functions preserve the array and object values instead of replacing them with empty objects.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.