Skip to content

[BUG][typescript-fetch] Serializers in oneOf check instanceOf against unserialized object #22198

@PhilippGuertlerKeen

Description

@PhilippGuertlerKeen

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 ...FromJSONTyped(json, ignoreDiscriminator) functions use an instanceCheck for oneOf properties, but they pass the unserialized json object into that instance check.
The consequence is that the serialization returns an empty object every time if for example camelCase is chosen for the modelPropertyNames, while the API spec uses PascalCase.

openapi-generator version

7.16.0

OpenAPI declaration file content or url

OpenAPI declaration file looks like this (let's call it openapi.yaml)

openapi: 3.0.0
info:
  title: Minimal Example API
  version: 1.0.0
servers:
  - url: /api/v1
paths:
  /myendpoint:
    get:
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/DynamicValueItem"
components:
  schemas:
    DynamicValueItem:
      properties:
        Value:
          oneOf:
            - $ref: "#/components/schemas/Foo"
            - $ref: "#/components/schemas/Bar"
      type: object
      required:
        - Value
    Foo:
      properties:
        Jimmy:
          type: string
      type: object
      required:
        - Jimmy
    Bar:
      properties:
        Nelson:
          type: string
      type: object
      required:
        - Nelson
Generation Details

Configuration file looks like this (let's call it config.yaml)

modelPropertyNaming: camelCase

The broken part of the generated code looks like this:

// DynamicValueItemValue.ts

export function DynamicValueItemValueFromJSONTyped(json: any, ignoreDiscriminator: boolean): DynamicValueItemValue {
    if (json == null) {
        return json;
    }
    if (typeof json !== 'object') {
        return json;
    }
    if (instanceOfBar(json)) {
        return BarFromJSONTyped(json, true);
    }
    if (instanceOfFoo(json)) {
        return FooFromJSONTyped(json, true);
    }
    return {} as any;
}
Steps to reproduce

Run

npx @openapitools/openapi-generator-cli generate -i openapi.yaml -c config.yaml -o ./out -g typescript-fetch
Related issues/PRs
Suggest a fix

Change the template so that the generated code looks like this:

// DynamicValueItemValue.ts

export function DynamicValueItemValueFromJSONTyped(json: any, ignoreDiscriminator: boolean): DynamicValueItemValue {
    if (json == null) {
        return json;
    }
    if (typeof json !== 'object') {
        return json;
    }
    if (instanceOfBar(BarFromJSONTyped(json, true))) { // <- Pass the serialized json here
        return BarFromJSONTyped(json, true);
    }
    if (instanceOfFoo(FooFromJSONTyped(json, true))) { // <- Pass the serialized json here
        return FooFromJSONTyped(json, true);
    }
    return {} as any;
}

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