OpenAPITools / OpenAPITools/openapi-generator

[BUG][go] Enum of type integer with negative integers results in syntax error

Open
#19,462 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

When using a .component.schema with type: integer, negative integer values are interpreted as null.

Here's a snippet of a generated model_enum_failing.go file (see steps below how to reproduce):

// EnumFailing the model 'EnumFailing'
type EnumFailing int32

// List of EnumFailing
const (
	_0 EnumFailing = 0
	_1 EnumFailing = 1
	_null EnumFailing = null
)

As seen here, null is assigned (in Go, null is not a keyword, so this is a syntax error). What's also worth noticing is that the produced api/openapi.yaml is different from the input OpenAPI yaml:

    EnumFailing:
      enum:
      - 0
      - 1
      - null
      format: int32
      type: integer

As a workaround, I can change from type: integer; format: int32 to type: number, but this may not be a valid workaround in all cases:

// EnumOK the model 'EnumOK'
type EnumOK float32

// List of EnumOK
const (
	_0 EnumOK = 0
	_1 EnumOK = 1
	_MINUS_1 EnumOK = -1
)

The expected behavior for how type: integer enums behave is similar to how type: number enum behaves.

openapi-generator version

latest as of writing (7.8.0) - also able to reproduce on master (aae3ab3154411d27c39b34e6272cfbd16049c177)

OpenAPI declaration file content or url
# test-schema.yaml
openapi: 3.0.1
info:
  title: Minimal API
  version: 1.0.0
paths: {}
components:
  schemas:
    EnumFailing:
      enum:
        - 0
        - 1
        - -1
      type: integer
      format: int32
Generation Details

See CLI options in reproduction steps below.

Steps to reproduce

Run: openapi-generator generate -i test-schema.yaml -g go -o goRepro

See produced goRepro/model_enum_failing.go contains invalid syntax:

const (
   // ...
	_null EnumFailing = null
)

The behavior is similar when trying to generate Python: openapi-generator generate -i test-schema.yaml -g python -o pyRepro

Produced pyRepro/openapi_client/models/enum_failing.py contains invalid syntax:

class EnumFailing(int, Enum):
    # ...
    NUMBER_0 = 0
    NUMBER_1 = 1
    NUMBER_null = null
Related issues/PRs
Suggest a fix

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

Run openapi-generator generate -i test-schema.yaml -g go -o goRepro and the corresponding Python command, then inspect the generated goRepro/model_enum_failing.go and pyRepro/openapi_client/models/enum_failing.py. Trace how the negative integer enum value becomes null and verify that both generated outputs preserve -1 as a valid enum value without syntax errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, openapi, python
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.