openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open kbakk opened this issue 1 year ago • 0 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] 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

kbakk avatar Aug 27 '24 10:08 kbakk