swagger-api / swagger-api/swagger-codegen

[TypeScript] Any typescript codegen removes underscores and forces enum value naming to camelCase (thus breaking snake_case API's, see Zalando API guidelines) [with fix, 1 LOC]

Open
#9,255 6 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

The abstract TypeScript codegen implementation has a bug that breaks any Enumeration described in any swagger.json file basically turning enum values like "TYPE_A" into "TYPEA":

https://github.com/swagger-api/swagger-codegen/blob/4607a90d7b69463a0ae8fc94fac68fc95d80965e/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractTypeScriptClientCodegen.java#L466

Also, camelCase is forced, ignoring modelPropertyNaming option:

https://github.com/swagger-api/swagger-codegen/blob/4607a90d7b69463a0ae8fc94fac68fc95d80965e/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractTypeScriptClientCodegen.java#L471

Bildschirmfoto 2019-03-11 um 17 23 18
Swagger-codegen version

master, rev. e11d7646091ab75780fc69be938218ef5da15715

Swagger declaration file content or url

Any swagger.json that specifies Enum values in format as: A_B_C
https://raw.githubusercontent.com/logzio/public-api/master/alerts/swagger.json

Command line used for generation
Steps to reproduce
java -jar ./swagger-codegen-cli.jar generate \
   -i https://raw.githubusercontent.com/logzio/public-api/master/alerts/swagger.json \
   -l typescript-fetch \
   -D modelPropertyNaming=original \
   -o ./src

Or:

  1. https://editor.swagger.io/#
  2. Import from URL: https://raw.githubusercontent.com/logzio/public-api/master/alerts/swagger.json
  3. Generate client: typescript-fetch
  4. Extract zip -> open api.ts -> Line 323, OperationEnum
Related issues/PRs

Unknown

Suggest a fix/enhancement

Instead of manually replacing special characters and forcing camelCase, call:

    String enumName = sanitizeName(name);

It fixes the issue for me but (expectedly) breaks the tests.
Unfortunately, I have no time for a PR process atm.

For anyone running into the same issue, I hope this helps.

btw: Namespaces are deprecated and to be replaced by ES6 modules syntax. If you try to compile swagger-codegen based TypeScript libraries with Babel 7 instead of tsc, this is already impossible and the code will break once TypeScript removes support for namespaces officially.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractTypeScriptClientCodegen.java at the referenced lines, then reproduce with the typescript-fetch command and logzio swagger.json. Verify that generated api.ts preserves enum underscores and respects modelPropertyNaming=original, while updating the relevant tests that currently fail with the proposed sanitization change.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, typescript
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.