-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[JavaScript] Usage with create-react-app + Promises + ES6 unsupported? #8024
Comments
A little more research revealed some additional information. The "Supported Language Features and Polyfills" section of the create-react-app README states that "Class Fields and Static Properties" are supported via the "Class Public Fields" TC39 proposal. That proposal, however, has been merged with the "Class Fields" TC39 proposal (currently Stage 2) which states:
So, again, it seems like the Swagger ES6 codegen is a little too bleeding edge. |
Just out of curiosity, does the generated SDK with |
@joyfulelement I haven't actually tested your scenario, but I imagine it would depend on how you have webpack/babel/browserify setup for your non-CRA project. The code generated when using both the |
@john-goldsmith have you found a solution for this? I'm having the same issue. I'm not really wanting to eject my CRA, but cannot seem to find a way to get our library to import into our react app. I have the same setup CRA + swagger-codegen JS client, and getting this error:
|
1. Don't use property initializers The ES6 Javascript generator used property initializers, as described [here](https://tc39.github.io/proposal-class-public-fields/). This is not standard ES6. In particular, create-react-app chokes on it (see [this issue](swagger-api/swagger-codegen#8024), which is fixed by this commit). This commit instead reverts back to using prototypes, as in the non-es6 JS generator. 2. Add missing pom.xml files This allows us to run `mvn integration-test` in these folders. 3. Add missing double-quotes around enum field names. This avoids errors due to fields that contain illegal characters. 4. Fix support for multiple inheritance The previous implementation was broken. I added static "initialize" methods, which supports multiple inheritance. 5. Comment out some broken tests. There were tests for OuterBoolean etc, which are not generated as part of index.js in the generated petstore clients. Hence, these tests threw an error. The actual tests were already commented out anyway. I just commented out one more line to avoid an error. 6. Remove some empty lines in generates JS files.
@jimklo Sorta -- I'm using the Pros:
Cons:
I created several directories in my SDK repo that mirror the directories in this repo for sanity's sake, but it's not required: my-sdk
└ modules
└ swagger-codegen
└ src
└ main
└ resources
└ Javascript
└ es6
├ api.mustache # https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/Javascript/es6/api.mustache
├ ApiClient.mustache # https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/Javascript/es6/ApiClient.mustache
├ partial_model_generic.mustache # https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/Javascript/es6/partial_model_generic.mustache
├ partial_model_inner_enum.mustache # https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/Javascript/es6/partial_model_inner_enum.mustache
└ README.md # Just for good measure And I create the SDK with following command: swagger-codegen generate \
--input-spec http://petstore.swagger.io/v2/swagger.json \
--lang javascript \
--output . \
--template-dir ./modules/swagger-codegen/src/main/resources/Javascript/es6 \
--additional-properties usePromises=true,useES6=true I haven't yet tested @delenius' changes noted above, but on the surface it sounds like it'll be the official/preferred fix. |
1. Don't use property initializers The ES6 Javascript generator used property initializers, as described [here](https://tc39.github.io/proposal-class-public-fields/). This is not standard ES6. In particular, create-react-app chokes on it (see [this issue](swagger-api/swagger-codegen#8024), which is fixed by this commit). This commit instead reverts back to using prototypes, as in the non-es6 JS generator. 2. Add missing pom.xml files This allows us to run `mvn integration-test` in these folders. 3. Add missing double-quotes around enum field names. This avoids errors due to fields that contain illegal characters. 4. Fix support for multiple inheritance The previous implementation was broken. I added static "initialize" methods, which supports multiple inheritance. 5. Comment out some broken tests. There were tests for OuterBoolean etc, which are not generated as part of index.js in the generated petstore clients. Hence, these tests threw an error. The actual tests were already commented out anyway. I just commented out one more line to avoid an error. 6. Remove some empty lines in generates JS files.
Hi, anyone to summarize the "state or art" on how to integrate a codegen swagger javascript or ES6 in a create-react-app without doing an eject? |
@mvamax , this works in the latest master of openapi-generator. It actually does not work with the es6 client, simply because CRA does not transpile the dependencies. You can run it in dev mode, but the build fails. However, it does work with the regular JS client (I only tested with Promises, but it probably works with or without). Try it out. |
Hi @john-goldsmith is it possible to generate reactjs component using swagger yaml? |
@john-goldsmith using the Thanks |
Description
The generated JavaScript output from passing the
usePromises
and/oruseES6
options don't seem to play nicely with a fresh non-ejected React app created usingcreate-react-app
. I realize this sounds more like acreate-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:
(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:
(ref)
I manually changed all instances of
static someProperty
in the generated SDK tostatic get someProperty()
and things seemed to work fine (but obviously that's not a real solution).Steps to reproduce
swagger-codegen
, generate JavaScript SDKs, passing theusePromises
and/oruseES6
optionscreate-react-app
to create a new appnpm start
in the React appRelated 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; }
orMyClass.myProp
(my personal preference, ref)The text was updated successfully, but these errors were encountered: