OpenAPITools / OpenAPITools/openapi-generator

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

Open
#22,198 0 comments 1 reaction 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 ...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;
}

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 running the npx @openapitools/openapi-generator-cli command with openapi.yaml and config.yaml, then inspect the template that produces DynamicValueItemValueFromJSONTyped in the generated typescript-fetch output. Verify the oneOf instance checks against the deserialized model values and confirm the generated serializer no longer returns an empty object for the PascalCase example.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.