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

[BUG][GO] Enums not generated

Open ngodzik opened this issue 3 years ago • 3 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?
  • [X] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Using the petstore.yaml sample file, the provided enum is not reflected in the Go generated files

 Pet:
  title: a Pet
  description: A pet for sale in the pet store
  type: object
  required:
    - name
    - photoUrls
  properties:
    id:
      type: integer
      format: int64
    category:
      $ref: '#/components/schemas/Category'
    name:
      type: string
      example: doggie
    photoUrls:
      type: array
      xml:
        name: photoUrl
        wrapped: true
      items:
        type: string
    tags:
      type: array
      xml:
        name: tag
        wrapped: true
      items:
        $ref: '#/components/schemas/Tag'
    status:
      type: string
      description: pet status in the store
      enum:
        - available
        - pending
        - sold

the Pet structure is generated this way:

// Pet A pet for sale in the pet store type Pet struct { Id *int64 json:"id,omitempty" Category *Category json:"category,omitempty" Name string json:"name" PhotoUrls []string json:"photoUrls" Tags *[]Tag json:"tags,omitempty" // pet status in the store Status *string json:"status,omitempty" }

It implies to add manually files to reflect these enums, this decreases the interested of such generated files, hard to maintain

openapi-generator version

latest master version (does not work with previous versions as well to my knowledge) commit 6c40192706cf95329f03e4c80ea628fb32a8ecab (HEAD -> master, origin/master, origin/HEAD) Date: Mon May 24 07:01:16 2021 +0200

OpenAPI declaration file content or url

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml

Generation Details

./run-in-docker.sh generate -i petstore.yaml -g go -o /gen/out/petstore --skip-validate-spec --package-name=ps -c config.yaml

config.yaml:

additionalProperties: generateInterfaces: "true" enumClassPrefix: "true" isGoSubmodule: "true"

Steps to reproduce

generate the go files using the provided generation details and the petstore.yaml file

Related issues/PRs

This is related to this issue: https://github.com/OpenAPITools/openapi-generator/issues/8937 but for the golang language

Suggest a fix

Instead of using a *string, generate a new dedicated type and set the possible values accordingly

ngodzik avatar May 24 '21 15:05 ngodzik

Any news on this?

manther avatar Jul 22 '21 22:07 manther

@ngodzik @manther I'm facing the same issue. I've decided to work around this by creating a separate schema component and reference that in the property.

E.g.

Pet:
  title: a Pet
  description: A pet for sale in the pet store
  type: object
  required:
    - name
    - photoUrls
  properties:
    id:
      type: integer
      format: int64
    category:
      $ref: '#/components/schemas/Category'
    name:
      type: string
      example: doggie
    photoUrls:
      type: array
      xml:
        name: photoUrl
        wrapped: true
      items:
        type: string
    tags:
      type: array
      xml:
        name: tag
        wrapped: true
      items:
        $ref: '#/components/schemas/Tag'
-  status:
-     type: string
-     description: pet status in the store
-     enum:
-       - available
-       - pending
-       - sold
+   status:
+     $ref: '#/components/schemas/Status'
+components:
+ schemas:
+   Status:
+    enum:
+       - available
+       - pending
+       - sold
+     type: string

joakimhew avatar Sep 09 '21 18:09 joakimhew

Having the same issue with enums not being generated. However, I don't have control over the spec file, so modifying it isn't the solution for me.

Until this issue is fixed I will not be able to use this tool.

😞

oz117 avatar Oct 17 '22 10:10 oz117