OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.51k stars 6.27k forks source link

[BUG] [GO] The Enum field is not generated. #18924

Open jishupei opened 2 weeks ago

jishupei commented 2 weeks ago

Bug Report Checklist

Description

When I use the following JSON to generate code, the fields with enum types in the model do not generate the corresponding enums. The generated code is as follows:

type CommonErrorType struct {
    // 错误码
    Code string `json:"code"`
    // 解决方案链接
    Links *string `json:"links,omitempty"`
    // 错误描述
    Message              string `json:"message"`
    AdditionalProperties map[string]interface{}
}

The enum values for code are not reflected in the model.

openapi-generator version

openapi-generator-cli-6.6.0.jar

OpenAPI declaration file content or url
{
  "openapi": "3.0.3",
  "info": {
    "title": "test",
    "description": "test",
    "contact": {
      "name": "test",
      "url": "https://test/support/home.htm"
    },
    "version": "1.0"
  },
  "servers": [
    {
      "url": "https://openapi.com",
      "description": "Production server"
    },
    {
      "url": "https://openapidev.com",
      "description": "Sandbox server"
    }
  ],
  "paths": {
    "/card/template/query" : {
      "get" : {
        "tags" : [ "card.template" ],
        "summary" : "query",
        "description" : "query",
        "operationId" : "card.template.query",
        "responses" : {
          "200" : {
            "description" : "common response",
            "content" : {
              "application/json" : {
                "schema" : {
                  "$ref" : "#/components/schemas/CardTemplateQueryResponseModel"
                }
              }
            }
          },
          "default" : {
            "description" : "error",
            "content" : {
              "application/json" : {
                "schema" : {
                  "anyOf" : [ {
                    "$ref" : "#/components/schemas/CommonErrorType"
                  }, {
                    "$ref" : "#/components/schemas/CardTemplateQueryErrorResponseModel"
                  } ]
                }
              }
            }
          }
        },
        "parameters" : [ {
          "name" : "template_id",
          "in" : "query",
          "description" : "id",
          "schema" : {
            "type" : "string",
            "example" : "20160718000000001"
          }
        } ]
      }
    }
  },
  "components": {
    "schemas": {
      "CardTemplateQueryErrorResponseModel" : {
        "required" : [ "code", "message" ],
        "type" : "object",
        "properties" : {
          "code" : {
            "type" : "string",
            "description" : "code",
            "enum" : [ "INVALID_PARAMETER", "SYSTEM_ERROR", "TEMPLATE_NOT_EXIT" ]
          },
          "links" : {
            "type" : "string",
            "description" : "links"
          },
          "message" : {
            "type" : "string",
            "description" : "message"
          }
        }
      },
      "CardTemplateQueryResponseModel" : {
        "type" : "object",
        "properties" : {
          "access_version" : {
            "type" : "string",
            "description" : "access_version",
            "example" : "basic"
          },
          "biz_no_prefix" : {
            "type" : "string",
            "description" : "biz_no_prefix",
            "example" : "prex"
          }
        }
      },
      "CommonErrorType" : {
        "required" : [ "code", "message" ],
        "type" : "object",
        "properties" : {
          "code" : {
            "type" : "string",
            "description" : "code",
            "enum" : [ "invalid-parameter", "upload-fail", "invalid-file-extension" ]
          },
          "links" : {
            "type" : "string",
            "description" : "links"
          },
          "message" : {
            "type" : "string",
            "description" : "message"
          }
        }
      }
    }
  }
}
Generation Details
Steps to reproduce

java -jar bin/openapi-generator-cli-6.6.0.jar generate \ -g go\ -i template/oas_full.json\ -o source \ --global-property skipFormModel=false,modelTests=false,apiTests=false,modelDocs=false

Related issues/PRs
Suggest a fix

generate the correct enum classes.

wing328 commented 1 week ago

https://github.com/openapitools/openapi-generator/blob/master/docs/customization.md#inline-schema-naming

RESOLVE_INLINE_ENUMS=true will refactor inline enum definitions into $ref

you can enable this in inline schema mapping option to refactor inline enum into $ref (which will be generated as enum class)

jishupei commented 1 week ago

https://github.com/openapitools/openapi-generator/blob/master/docs/customization.md#inline-schema-naming

RESOLVE_INLINE_ENUMS=true will refactor inline enum definitions into $ref

you can enable this in inline schema mapping option to refactor inline enum into $ref (which will be generated as enum class)

This parameter doesn't seem to work in version 6.6.0. However, I can't upgrade to version 7.6.0 because my system can only use JDK 1.8.

wing328 commented 1 week ago

you can use docker instead: https://hub.docker.com/r/openapitools/openapi-generator-cli