OpenAPITools / OpenAPITools/openapi-generator

[BUG][typescript-fetch] modelGeneric `instanceOf` functions uses case-adjusted name to identify fields

Open
#23,140 1 comment 2 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 uses the case-adjusted property name for the instanceOf... function check. This fails if the case-adjusted name is not the one specified in the schema (baseName).

If any field is specified as e.g. oneOf, the instanceOf... function is called but determines the provided (JSON) object is not an instance and returns the fallback value of {}. Hence, the data from the request is not propagated to the consumer.

openapi-generator version
  • 7.20.0
  • 7.21.0-SNAPSHOT (at the time of writing)
OpenAPI declaration file content
openapi: 3.1.0
info:
  title: api
  version: 1.0.0
paths:
  /api/v1/run:
    get:
      summary: runs
      operationId: runs
      responses:
        "200":
          description: runs
          content:
            application/json:
              schema:
                type: object
                description: job
                required:
                  - details
                properties:
                  details:
                    oneOf:
                      - type: object
                        required:
                          - Foo
                        properties:
                          Foo:
                            type: string
                            enum:
                              - foo
                      - type: object
                        required:
                          - Bar
                        properties:
                          Bar:
                            type: string
                            enum:
                              - bar

Generation Details
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate \
    -i /local/openapi.yaml \
    -g typescript-fetch \
    -o /local/gen
Steps to reproduce

Save the declaration as openapi.yaml and run the generation command provided in generation details.

Expected vs. actual output
--- a/gen/models/Runs200ResponseDetailsOneOf.ts
+++ b/gen/models/Runs200ResponseDetailsOneOf.ts
@@ -41,7 +41,7 @@ export type Runs200ResponseDetailsOneOfFooEnum = typeof Runs200ResponseDetailsOn
  * Check if a given object implements the Runs200ResponseDetailsOneOf interface.
  */
 export function instanceOfRuns200ResponseDetailsOneOf(value: object): value is Runs200ResponseDetailsOneOf {
-    if (!('foo' in value) || value['foo'] === undefined) return false;
+    if (!('Foo' in value) || value['Foo'] === undefined) return false;
     return true;
 }

--- a/gen/models/Runs200ResponseDetailsOneOf1.ts
+++ b/gen/models/Runs200ResponseDetailsOneOf1.ts
@@ -41,7 +41,7 @@ export type Runs200ResponseDetailsOneOf1BarEnum = typeof Runs200ResponseDetailsO
  * Check if a given object implements the Runs200ResponseDetailsOneOf1 interface.
  */
 export function instanceOfRuns200ResponseDetailsOneOf1(value: object): value is Runs200ResponseDetailsOneOf1 {
-    if (!('bar' in value) || value['bar'] === undefined) return false;
+    if (!('Bar' in value) || value['Bar'] === undefined) return false;
     return true;
 }
Related issues/PRs

To the best of my knowledge there is no related issue.

Suggest a fix

Changing name to baseName in the following line does the fix for parsing the JSON.
https://github.com/OpenAPITools/openapi-generator/blob/80f2d7f74de75b387874ec12b7c3260332fe08bb/modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache#L28

However, it may fail the instance of check in the to JSON function. Maybe adding a second function or duplicating the line (with name -> baseName) could be a viable option.

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 modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache at the referenced line, then reproduce the issue using the supplied openapi.yaml and docker generation command. Compare the generated instanceOf checks with the schema property names and verify that parsing and to-JSON behavior remain correct for the 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
Quiet
Clarity
Mostly clear
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.