Swagger-codegen: Underscore prefix dropped from property names in generated Node Typescript classes

Created on 4 May 2016  Â·  7Comments  Â·  Source: swagger-api/swagger-codegen

_From @saikatguha on April 29, 2016 22:9_

"Foo": {
  "type": "object",
  "properties": {
    "_links": { "$ref": "#/definitions/Bar" }
  }
}

becomes

class Foo {
  "links": Bar;
}

_Copied from original issue: swagger-api/swagger-editor#905_

TypeScript Bug help wanted

Most helpful comment

I am having problems when accessing to camel case properties, when they are snake case in the definition (using typescript-node). When I print the object, I get the JSON with properties in snake case, but the classes are in camel case, and when I access the properties, they are undefined (but when they have only one word, as snake case and camel case are equals).

I tried modelPropertyNaming=original, and the generated typescript code have snake case properties now, and I can access properties correctly. Is there any way to access the values but with camel case? I prefer to code using camel case, but now I can only access to values with one word :sob:

All 7 comments

@saikatguha to resolve it, you need to set modelPropertyNaming to original:

swagger-codegen|master ⇒ java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar config-help -l typescript-node


CONFIG OPTIONS
    sortParamsByRequiredFlag
        Sort method arguments to place required parameters before optional parameters. (Default: true)

    ensureUniqueParams
        Whether to ensure parameter names are unique in an operation (rename parameters that are not). (Default: true)

    modelPropertyNaming
        Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name (Default: camelCase)

    npmName
        The name under which you want to publish generated npm package

    npmVersion
        The version of your npm package

    npmRepository
        Use this property to set an url your private npmRepo in the package.json

    snapshot
        When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm (Default: false)

I am using editor.swagger.io and don't have the option to change the codegen settings used there. If they are using the default options, original naming should probably be the default.

Also changing property names without also using a custom serializer/deserializer to map the changed names back to the original schema names in API operations breaks the client.

Please refer to https://github.com/swagger-api/swagger-codegen#online-generators on how to customize the SDK generated by generator.swagger.io, which is used by editor.swagger.io to generate SDKs.

I agree with you that ideally there should be a mapping between JSON key and object attributes but I'm not aware of any JS/TS lib to do that.

For PHP, Ruby, Python API clients, we end up implementing our own object serializer/deserializer to handle the mapping between JSON keys and object attributes.

May I know if you've cycle to contribute the enhancement (mapping between JSON keys and object attributes)?

The replacer parameter in JSON.stringify and reviver parameter in JSON.parse might come in handy. Some sort of a property map and a generic replacer/reviver might work. I don't have the cycles at the moment, but perhaps someone can make use of these.

@saikatguha thanks for the tips about replacer and reviver. Let's see if anyone from the community has cycle to contribute the enhancement.

I am having problems when accessing to camel case properties, when they are snake case in the definition (using typescript-node). When I print the object, I get the JSON with properties in snake case, but the classes are in camel case, and when I access the properties, they are undefined (but when they have only one word, as snake case and camel case are equals).

I tried modelPropertyNaming=original, and the generated typescript code have snake case properties now, and I can access properties correctly. Is there any way to access the values but with camel case? I prefer to code using camel case, but now I can only access to values with one word :sob:

Wups. TIL github actually resolves issues in forked repositories. Sorry for the mess.

Was this page helpful?
0 / 5 - 0 ratings