OpenAPITools / OpenAPITools/openapi-generator

[BUG] Example responses return undefined

Open
#6,702 0 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 (example)?

  • What's the version of OpenAPI Generator used?

  • Have you search for related issues/PRs?

  • What's the actual output vs expected output?

  • [Optional] Bounty to sponsor the fix (example)

Description

There are two related issues:

  1. When generating code that is nullable: true, the example response is undefined
  2. If an optional parameter is left out of an example response, it has an undefined value.
openapi-generator version

"@openapitools/openapi-generator-cli": "^1.0.13-4.3.1",

OpenAPI declaration file content or url
components:
  schemas:
    Animals:
      description: 'Returns cat and/or dog'
      type: object
      properties:
        id:
          type: string
          format: uuid
        dog:
          type: string
          nullable: true
        cat:
          type: string
          nullable: true
      required: ['id']


responses:
  RetrievedAnimals:
    description: 'Retrieved Animals'
    content:
      application/json:
        schema:
          $ref: '#/components/schemas/Animals'
        example:
          id: '64acc332-382e-46f3-b060-737928686f60'
          dog: null
 

Current output:

{
  id: '64acc332-382e-46f3-b060-737928686f60',
  dog: undefined,
  cat: undefined
}

Expected output:

{
  id: '64acc332-382e-46f3-b060-737928686f60',
  dog: null,

}
Suggest a fix

#isNullable does not seem to be utilized.
I suggest returning response.json(); in all instances.

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache#L59-L90

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 and reproduce the issue with the Animals schema in the report. Check how nullable and omitted optional properties are rendered in the example response. Done means nullable values remain null and omitted optional properties are not emitted as undefined.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.