Prevent openapi-generator renaming enums in JavaScript/TypeScript
Asked Answered
H

1

10

We're using the openapi generator to generate our REST client. Works really well, until we started using enums in the UPERCASE_UNDERSCORE format. It strips out the underscore. The main problem with this is it makes casting this enum a real pain in Typescript as the property name doesn't match the definition.

Here's an example,m the YAML:

  properties:
    boostId:
      type: string
      maxLength: 50
    type:
      type: string
      enum:
        - BOOST_UNIQUE_ALERT_TYPE_COUNTER
        - BOOST_UNIQUE_SOURCE_SYSTEM_COUNTER

Produces:

var BoostConfiguration;
(function (BoostConfiguration) {
    /**
     * @export
     * @enum {string}
     */
    var TypeEnum;
    (function (TypeEnum) {
        TypeEnum["UNIQUEALERTTYPECOUNTER"] = "BOOST_UNIQUE_ALERT_TYPE_COUNTER";
        TypeEnum["UNIQUESOURCESYSTEMCOUNTER"] = "BOOST_UNIQUE_SOURCE_SYSTEM_COUNTER";
    })(TypeEnum = BoostConfiguration.TypeEnum || (BoostConfiguration.TypeEnum = {}));
})(BoostConfiguration = exports.BoostConfiguration || (exports.BoostConfiguration = {}));

It's stripped out the "BOOST" for some reason, and removed underscores. I've tried both enumPropertyNaming=original and modelPropertyNaming=original, neither seem to change anything (in the enums). This is our generator script:

openapi-generator generate -i api/socosApi.yaml -g typescript-axios -o generated-sources/api --additional-properties=enumPropertyNaming=original,modelPropertyNaming=original
Harhay answered 2/12, 2019 at 8:57 Comment(1)
--additional-properties=enumPropertyNaming=original works for me in v4.3.1. It stops stripping out unsderscoresShrive
R
7

Stripping out "BOOST" can be prevented by setting "removeEnumValuePrefix" to "false" (See https://github.com/OpenAPITools/openapi-generator/pull/5166)

Rachealrachel answered 26/10, 2020 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.