Skip to content

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

@WIStudent

Description

@WIStudent

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.

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