Openapi-generator: [BUG] typescript-fetch should not use namespaces (SyntaxError: Namespaces are not supported.)

Created on 20 Jan 2019  路  8Comments  路  Source: OpenAPITools/openapi-generator

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] What's the version of OpenAPI Generator used?
  • [x] Have you search for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Bounty to sponsor the fix (example)
Description

Generated files result in error: (e.g. in create-react-app with TypeScript support)

./src/api/profile/models/Culture.ts
SyntaxError: C:\Users\ceefour\git\heartenly-pwa\src\api\profile\models\Culture.ts: Namespaces are not supported.
  110 |  * @namespace Culture
  111 |  */
> 112 | export namespace Culture {
      |        ^
  113 |     /**
  114 |      * @export
  115 |      * @enum {string}

Adding configuration supportsES6: true doesn't help.

Via @DanielRosenwasser - https://github.com/facebook/create-react-app/pull/4837#issuecomment-430107471 :

TypeScript PM here. Broadly speaking, I feel that

namespaces are not a huge loss; the React community is firmly on-board with ES modules anyway, and that's the direction the TypeScript world is taking.
const enums are often misused. Sure, they can be great for the internals of a library to get some speed improvements, but they generally shouldn't be used outside of that scope.
Merging behavior is marginally useful now that TypeScript 3.1 has the ability to tack properties onto functions. The fact that enums merge is also something I've never seen used out in the wild.

via @Timer - https://github.com/facebook/create-react-app/issues/5681#issuecomment-435372634 :

I'm sorry and understand that this is likely frustrating, but namespaces are a proprietary, legacy feature. They have been replaced by specification behavior: ES Modules.
Namespaces are deprecated. The TypeScript documentation is going to remove all references to them in examples and stop usage. In other words, you should not be using namespaces anymore.
Const Enums are rarely needed and often misused.

openapi-generator version

0.0.7-4.0.0-beta

OpenAPI declaration file content or url

anything that uses enum

Command line used for generation
openapi-generator generate -i E:\tmp\lovia-heartenly-profile-4.0-swagger.yaml -g typescript-fetch -o src/api/profile
Steps to reproduce
  1. generate using typescript-fetch
  2. @babel/preset-typescript won't compile, and the generated files use deprecated namespace feature
Related issues/PRs
Suggest a fix

Remove the namespace and use the enum directly in the module.

TypeScript Bug

Most helpful comment

@ceefour your suggestion

Remove the namespace and use the enum directly in the module.

works as long as their isn't another enum with the same name.

For instance with the pet store examples that's the case and you end up with the following error within the models barrel:

[ts] Module './Order' has already exported a member named 'StatusEnum'. Consider explicitly re-exporting to resolve the ambiguity. [2308]

So to prevent this I'd suggest to prefix the enum with the namespace.

All 8 comments

馃憤 Thanks for opening this issue!
馃彿 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@ceefour sounds good. do you want to file a PR for that?

@ceefour your suggestion

Remove the namespace and use the enum directly in the module.

works as long as their isn't another enum with the same name.

For instance with the pet store examples that's the case and you end up with the following error within the models barrel:

[ts] Module './Order' has already exported a member named 'StatusEnum'. Consider explicitly re-exporting to resolve the ambiguity. [2308]

So to prevent this I'd suggest to prefix the enum with the namespace.

I agree with @denyo, that would work for all cases. 馃憤馃従

Optionally there could be a configuration option to disable namespace prefixing if the API author is sure their enum names never conflict. But generally I agree with @denyo's solution.

@macjohnny
Having the same problem, could file a PR if someone guide me through it, I'm not familiar with the code base. Can you point me in the right direction? Module, files to check etc... ?

@willianscfa the starting point for the code generation template is here:
https://github.com/OpenAPITools/openapi-generator/blob/0a2fb8db8fb0d573b3cf4727943ada8cabddbb99/modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache#L93-L112

the controller is here https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java

after running

mvn clean
mvn package

you can test the result with

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g typescript-fetch -i samples/my-namespace-test.yml -o samples/typescript-fetch-namespace-test/

typescript-axios client has same problem.Is there any update plan for that client?

@Rassilion you could add this to your plans and support us fixing it ;-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

renskesterrenburg picture renskesterrenburg  路  27Comments

wing328 picture wing328  路  25Comments

t2y picture t2y  路  27Comments

dkliban picture dkliban  路  24Comments

jonrimmer picture jonrimmer  路  80Comments