LoginSignup
0
0

More than 1 year has passed since last update.

OpenAPI Specでハイフン区切りのEnum値をOpenAPI Generatorで出力すると、ハイフン区切りのまま出力される

Posted at

開発環境

  • OpenAPI Generator v4.3.1, v5.4.0
  • python 3.10.2

やりたいこと

以下のOpenAPI Specificationファイルから、LangというEnumクラスを作成したいです。

petstore.yaml
openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/v1
paths:
  /lang:
    get:
      summary: getLang
      operationId: getLang
      responses:
        '200':
          description: foo
          content:
            application/json:    
              schema:
                $ref: "#/components/schemas/Lang"
components:
  schemas:
    Lang:
      type: string
      enum:
        - ja-JP
        - en-US
lang.py
from enum import Enum

class Lang(Enum):
    JA_JP = "ja-JP"
    EN_US = "en-US"

問題

以下のmustacheファイルを用意して、openapi-generator-cliコマンドを実行しました。

template/models.mustache
from enum import Enum
{{#models}}
{{#model}}
{{#isEnum}}
class {{classname}}(Enum):
{{#allowableValues}}
{{#enumVars}}
    {{name}} = {{{value}}}
{{/enumVars}}
{{/allowableValues}}
{{/isEnum}}
{{/model}}
{{/models}}
$ docker run --rm -u `id -u`:`id -g` -v ${PWD}:/local openapitools/openapi-generator-cli:v5.4.0 generate \
 --input-spec /local/petstore.yaml --generator-name python --output /local/out \
 --template-dir /local/template --global-property models,modelTests=false,modelDocs=false

$ tree out/
out
└── openapi_client
    └── model
        └── lang.py

2 directories, 1 file

以下のpythonファイルが出力されましたが、メンバ名にハイフンが含まれているため、文法上正しくありません。

out/openapi_client/model/lang.py
from enum import Enum
class Lang(Enum):
    JA-JP = "ja-JP"
    EN-US = "en-US"

調査

JA_JPのようなメンバを出力するmustacheファイルの変数があるかもしれないので、--global-propertydebugModelsを追加して、デバッグ情報を出力しました。

$ docker run --rm -u `id -u`:`id -g` -v ${PWD}:/local openapitools/openapi-generator-cli:v5.4.0 generate \
 --input-spec /local/petstore.yaml --generator-name python --output /local/out \
 --template-dir /local/template --global-property models,modelTests=false,modelDocs=false,debugModels

[main] INFO  o.o.codegen.DefaultGenerator - ############ Model info ############
[ {
  "importPath" : "from openapi_client.model.lang import Lang",
  "model" : {
    "anyOf" : [ ],
    "oneOf" : [ ],
    "allOf" : [ ],
    "name" : "Lang",
    "classname" : "Lang",
    "classVarName" : "lang",
    "modelJson" : "{\n  \"type\" : \"string\",\n  \"enum\" : [ \"ja-JP\", \"en-US\" ]\n}",
    "dataType" : "str",
    "classFilename" : "lang",
    "isAlias" : false,
    "isString" : true,
    "isInteger" : false,
    "isLong" : false,
    "isNumber" : false,
    "isNumeric" : false,
    "isFloat" : false,
    "isDouble" : false,
    "isDate" : false,
    "isDateTime" : false,
    "isDecimal" : false,
    "isShort" : false,
    "isUnboundedInteger" : false,
    "isBoolean" : false,
    "additionalPropertiesIsAnyType" : false,
    "vars" : [ ],
    "allVars" : [ ],
    "requiredVars" : [ ],
    "optionalVars" : [ ],
    "readOnlyVars" : [ ],
    "readWriteVars" : [ ],
    "parentVars" : [ ],
    "allowableValues" : {
      "values" : [ "ja-JP", "en-US" ],
      "enumVars" : [ {
        "name" : "JA-JP",
        "isString" : false,
        "value" : "\"ja-JP\""
      }, {
        "name" : "EN-US",
        "isString" : false,
        "value" : "\"en-US\""
      } ]
    },
    "mandatory" : [ ],
    "allMandatory" : [ ],
    "imports" : [ ],
    "hasVars" : false,
    "emptyVars" : false,
    "hasMoreModels" : false,
    "hasEnums" : false,
    "isEnum" : true,
    "hasValidation" : false,
    "isNullable" : false,
    "hasRequired" : true,
    "hasOptional" : false,
    "isArray" : false,
    "hasChildren" : false,
    "isMap" : false,
    "isNull" : false,
    "isDeprecated" : false,
    "hasOnlyReadOnly" : true,
    "vendorExtensions" : { },
    "hasMultipleTypes" : false,
    "isAdditionalPropertiesTrue" : false,
    "uniqueItems" : false,
    "exclusiveMinimum" : false,
    "exclusiveMaximum" : false,
    "isModel" : false,
    "hasDiscriminatorWithNonEmptyMapping" : false,
    "isAnyType" : false,
    "isClassnameSanitized" : false,
    "hasItems" : false
  }
} ]

JA_JPのようなメンバを出力できる変数はなかったので、諦めるしかないようです。

OpenAPI Generator v4.3.1で実行する

OpenAPI Generator v4.3.1で実行すると、Enumのメンバ名はスネークケースで出力されました。

$ docker run --rm -u `id -u`:`id -g` -v ${PWD}:/local openapitools/openapi-generator-cli:v4.3.1 generate \
 --input-spec /local/petstore.yaml --generator-name python --output /local/out \
 --template-dir /local/template --global-property models,modelTests=false,modelDocs=false,debugModels


[ {
  "importPath" : "from openapi_client.models.lang import Lang",
  "model" : {
    "anyOf" : [ ],
    "oneOf" : [ ],
    "allOf" : [ ],
    "name" : "Lang",
    "classname" : "Lang",
    "classVarName" : "lang",
    "modelJson" : "{\n  \"type\" : \"string\",\n  \"enum\" : [ \"ja-JP\", \"en-US\" ]\n}",
    "dataType" : "str",
    "classFilename" : "lang",
    "isAlias" : false,
    "isString" : true,
    "isInteger" : false,
    "isLong" : false,
    "isNumber" : false,
    "isNumeric" : false,
    "isFloat" : false,
    "isDouble" : false,
    "vars" : [ ],
    "allVars" : [ ],
    "requiredVars" : [ ],
    "optionalVars" : [ ],
    "readOnlyVars" : [ ],
    "readWriteVars" : [ ],
    "parentVars" : [ ],
    "allowableValues" : {
      "values" : [ "ja-JP", "en-US" ],
      "enumVars" : [ {
        "name" : "JA_JP",
        "isString" : false,
        "value" : "\"ja-JP\""
      }, {
        "name" : "EN_US",
        "isString" : false,
        "value" : "\"en-US\""
      } ]
    },
    "mandatory" : [ ],
    "allMandatory" : [ ],
    "imports" : [ ],
    "hasVars" : false,
    "emptyVars" : true,
    "hasMoreModels" : false,
    "hasEnums" : false,
    "isEnum" : true,
    "isNullable" : false,
    "hasRequired" : false,
    "hasOptional" : false,
    "isArrayModel" : false,
    "hasChildren" : false,
    "isMapModel" : false,
    "isDeprecated" : false,
    "hasOnlyReadOnly" : true,
    "vendorExtensions" : { },
    "uniqueItems" : false,
    "exclusiveMinimum" : false,
    "exclusiveMaximum" : false
  }
} ]
out/openapi_client/models/lang.py
from enum import Enum
class Lang(Enum):
    JA_JP = "ja-JP"
    EN_US = "en-US"

解決方法

以下のいずれかの方法で対応するしかないようです。

  • OpenAPI Generator v4.X.Xを実行する
  • OpenAPI Generator v5.X.Xの場合は、sedコマンドで置換する
sed  -e "s/JA-JP/JA_JP/g" out/openapi_client/model/lang.py --in-place
sed  -e "s/EN-US/EN_US/g" out/openapi_client/model/lang.py --in-place
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0