swagger-api / swagger-api/swagger-codegen
[JavaScript] Usage with create-react-app + Promises + ES6 unsupported?
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
The generated JavaScript output from passing the usePromises and/or useES6 options don't seem to play nicely with a fresh non-ejected React app created using create-react-app. I realize this sounds more like a create-react-app problem than a Swagger problem, but some initial findings indicate that Swagger's ES6 static property syntax isn't (yet) supported. It's possible I'm just overlooking something simple, but figured I'd float it to the community.
Swagger-codegen version
2.3.1
Swagger declaration file content or url
http://petstore.swagger.io/v2/swagger.json
Command line used for generation
JavaScript, with Promises:
swagger-codegen generate -i http://petstore.swagger.io/v2/swagger.json -l javascript -o . --additional-properties usePromises=true
Yields:
Failed to compile.
../my-app/src/index.js
Module not found: Can't resolve 'ApiClient' in '/Users/jgoldsmith/Sites/my-app/src'
(which, to me, just looks like some missing relative path qualifiers, e.g., ./ApiClient)
JavaScript, with Promises and ES6:
swagger-codegen generate -i http://petstore.swagger.io/v2/swagger.json -l javascript -o . --additional-properties usePromises=true,useES6=true
Yields:
../my-app/src/ApiClient.js
Module parse failed: Unexpected token (231:32)
You may need an appropriate loader to handle this file type.
| * @readonly
| */
| static CollectionFormatEnum = {
| /**
| * Comma-separated values. Value: <code>csv</code>
(ref)
I manually changed all instances of static someProperty in the generated SDK to static get someProperty() and things seemed to work fine (but obviously that's not a real solution).
Steps to reproduce
- Using
swagger-codegen, generate JavaScript SDKs, passing theusePromisesand/oruseES6options - Use
create-react-appto create a new app - Install/link the generated SDK into the app
- Run
npm startin the React app - Note the compilation errors
Related issues/PRs
- Possibly #8017
- https://github.com/facebook/create-react-app/issues/3247
Suggest a fix/enhancement
For non-ES6 output: use the correct relative paths
For ES6 output: change static properties to static get myProp() { return value; } or MyClass.myProp (my personal preference, ref)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing both failures with the provided swagger-codegen commands and a fresh non-ejected create-react-app project. Inspect modules/swagger-codegen/src/main/resources/Javascript/es6/ApiClient.mustache and the generated ApiClient.js; done means generated SDKs compile in the stated React setup for the relevant usePromises and useES6 options.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, react
- Domain
- api, frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100