-
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 client produces a wrong object for a string enum type that is used with $ref #4819
Comments
I can confirm this. It is a major bug that basically prevents using arrays of enums.
Instead of returning an array of strings, I return an array of characters - e.g. This is because in the generated ApiClient.js, in convertToType() you see:
Now, the $ref should return a type of string, but instead it returns an object.. and then in This looks similar to another bug I found that was with refs to primitive types: |
I managed to go around this issue by explicitly including the type:
Very sub-optimal, but it works for now. |
@knzm can you please submit a PR so that we can review the fix? |
@advance512 can you try the fix suggested by @knzm to see if that works for you? |
Unfortunately, his fix doesn't help me since I am dependent on auto-generated code and don't want to add any manual steps in. I can try his fix one time, just to see if it helps. Will try to do it this week and update |
Please try to test it locally. If it works, we'll incorpoate the fix into
the mustache template.
…On Thu, Mar 30, 2017 at 12:11 PM, Alon Diamant ***@***.***> wrote:
Unfortunately, his fix doesn't help me since I am dependent on
auto-generated code and don't want to add any manual steps in. I can try
his fix one time, just to see if it helps. Will try to do it this week and
update
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#4819 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AA5BdBpaAjc-Ez2DAbEoB25J_fkZjXdFks5rqyuBgaJpZM4MElIA>
.
|
I have tried to reproduce this bug with the exact given specs (current latest master) but looks like problem is already gone, despite to the suggested change (for Here are the pieces of correctly generated model definitions: EnumArrays:
StringEnumObject:
@knzm, any chance to confirm and close the issue? |
I still see this issue. Using swagger-codegen-cli-3.0.0-20170727.135949-1.jar with the command line:
I see that arrays of strings get converted into arrays of objects with indexes as keys and each char as the value. e.g.
This should be, of course, an simple array of 4 strings.
Once again, looking at the generated ApiClient.js:518, you see that:
Now, wth the context being:
the type is an Array of enum strings, so itemType becomes the Object shown above, and then you recursively enter
Now, when you get to the line: for..in used on a string has the following behavior:
These are actually "0", "1", "2". Not even numeric values. Now, remember, data is a string. So
The issue here is that we get to a point where the type is an object with possible string Enum values, the data is a string, but instead of looking at the entire string as the string value to check against the Enum we get a index -> character Object. |
@kirpit - is the issue only fixed with ES6 usage, or did you also check useES6=false output? |
@wing328 - please reopen this. The issue persists even with the latest trunk as shown above. :) |
I don't understand, I again tried against the current master 4dafa5b and results for both fish / crab and languages were generated correctly. There has been also kind of trimming for the attribute names in
And yes also checked the
|
Are you using $ref? |
Any update on this, @kirpit ? I am still seeing this issue, even now.. |
Can you please paste the specs and write the commands that you are able to reproduce the error on current master? |
@kirpit - I am offering the best reproduction instructions that I can think of. Please do the following:
As usual,
The type represents an Array of enum strings. In
Now, as you run
Now, when you get to the line:
These are actually "0", "1", "2". Not even numeric values. Again, remember,
So
So finally, when the initial call to
The issue here is that we get to a point where the type is an object with possible string Enum values, the data is a string, but instead of looking at the entire string as the string value to check against the Enum we get a index -> character Object. @kirpit, is there is anything more I can offer, let me know. |
I'll add that I am still getting the same issue with v3.0.0, e.g. using |
@adamholdenyall aval lo asinu migratzya shel javascript le-3.0.0. ze lo ya'avod. |
er, @advance512, sorry. |
@webron huh? :) I am using the latest master, but the same issue happens with 3.0.0 too. |
@advance512 ma kore? Version 3.0.0 of the codegen requires a migration of the generators/templates before they can be used with it. At the moment we have about 5-6 migrated generators, JavaScript is not one of them. More information in the release notes - https://github.com/swagger-api/swagger-codegen/releases/tag/v3.0.0-rc0. |
@webron Hakol tov. :) @kirpit You can follow the instructions and see the issue occurring for yourself. |
@advance512 - okay, you were the one who mentioned |
@webron can you explain what you mean? |
I'm confused. One time you're talking about swagger-codegen-cli-3.0.0-20170727.135949-1.jar another time on swagger-codegen-cli-2.4.0-20180412.075009-225.jar. 3.0.0 officially supports a very limited set of languages right now. 2.4.0 supports the entire set. |
@webron Okay, let me make it clear then:
I hope all of this makes sense. |
BTW, @kirpit, @wing328, @webron , I think I found the root of the issue. If you look at the generated
Note the difference way of treating enums. So, a single enum gets created using
which does not even contain a call to a This seems to originate from here: |
On second thought, I think @knzm 's fix is worth a shot:
|
I also encountered @knzm problem with this. |
Hey guys, I am dealing with this bug for some time now and it's really time wasting (especially today and I really hate when I finally realize it was this issue). Thanks. |
Hi guys! When this issue will be fixed? |
Hi All any plans on fixing the issue? |
I can confirm this still happens in: swagger-codegen-cli-2.4.0-20181113.142213-347.jar |
@CodeNinjai |
I just posted a $50 bounty on this at: |
FYI, this was fixed in OpenAPITools/openapi-generator#1429 |
Description
I got something like
{ '0': 'f', '1': 'i', '2': 's', '3': 'h' }
for an enum type. It should be"fish"
.Swagger-codegen version
2.2.2-SNAPSHOT (revision: bb81fc1)
Swagger declaration file content or url
Command line used for generation
Steps to reproduce
npm install
Related issues
I did not find any known issues about it. However, there were a number of bugs related to enum types with Javascript client in the past.
#3639 [javascript client] constructFromObject calls undefined function constructFromObject on enums
#3416 [JavaScript] String Enum types produce invalid code
#2102 [JavaScript] Enums are not exported correctly
Suggest a Fix
I can fix the issue by modifying
convertToType
method defined inApiClient.mustache
like this:I think
ApiClient.convertToType()
can always calltype.constructFromObject()
for types that haveconstructFromObject
method. But I'm not sure about its side effects.The text was updated successfully, but these errors were encountered: