OpenAPITools / OpenAPITools/openapi-generator

[BUG][typescript] parameter with integer enum fails when values include null

Open
#19,774 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)?
  • 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

With a query parameter of type nullable integer with enumerated allowed values including explicit null the typescript generator fails with a nasty exception.

I'm new to OpenAPI spec and may have misinterpreted what's allowed.

I'd have thought this was a legal spec based on running it through a spec validator.

openapi-generator version

v7.8.0 and latest on main

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Bug Repro
  version: 1.0.0
paths:
  /path:
    description: Bug repro
    get:
      description: Bug repro
      parameters:
        - in: query
          name: bug
          description: Bug repro
          schema:
            type: integer
            nullable: true
            enum: [null,1,2]
          example: 2
      responses:
        "200":
          description: OK
Generation Details

openapi-generator generate -I spec.yaml -g typescript -o ../bug

I have run this with a brew installed tool and then later with java debugger on a checkout of main.

Steps to reproduce

Run the generator using any typescript variation with the minimal config above to yield a fatal error whose cause is a NullPointerExpection.

Exception in thread "main" java.lang.RuntimeException: Could not process operation:
  Tag: class Tag {
    name: default
    description: null
    externalDocs: null
}
  Operation: null
  Resource: get /path
  Schemas: {}
  Exception: null
	at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1609)
	at org.openapitools.codegen.DefaultGenerator.processPaths(DefaultGenerator.java:1474)
	at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:673)
	at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:1306)
	at org.openapitools.codegen.cmd.Generate.execute(Generate.java:535)
	at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
	at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.NullPointerException
	at org.openapitools.codegen.languages.AbstractTypeScriptClientCodegen.numericEnumValuesToEnumTypeUnion(AbstractTypeScriptClientCodegen.java:727)
	at org.openapitools.codegen.languages.AbstractTypeScriptClientCodegen.getParameterDataType(AbstractTypeScriptClientCodegen.java:671)
	at org.openapitools.codegen.DefaultCodegen.fromParameter(DefaultCodegen.java:5138)
	at org.openapitools.codegen.DefaultCodegen.fromOperation(DefaultCodegen.java:4648)
	at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1577)
	... 6 more
Related issues/PRs

N/A

Suggest a fix

numericEnumValuesToEnumTypeUnion in AbstractTypescriptClientCodegen.java (line 727) is where the problem emnates.

This method is expecting the list of values to all be non-null as it calls toString() on each value. However this fails when there is an explicit null in the list.

My naive suggestion is a null check and in the case where value == null add "null" to the stringValues list.

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 in AbstractTypeScriptClientCodegen.java at numericEnumValuesToEnumTypeUnion, then trace its use through getParameterDataType for the reported query parameter. Run the provided openapi-generator command with the minimal spec and verify generation no longer fails with a NullPointerException when the integer enum includes null.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.