swagger-api / swagger-api/swagger-codegen

[JavaScript] Usage with create-react-app + Promises + ES6 unsupported?

Open
#8,024 9 comments 1 reaction 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 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
  1. Using swagger-codegen, generate JavaScript SDKs, passing the usePromises and/or useES6 options
  2. Use create-react-app to create a new app
  3. Install/link the generated SDK into the app
  4. Run npm start in the React app
  5. Note the compilation errors
Related issues/PRs
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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.