{"action":"created","issue":{"url":"https://api.github.com/repos/swagger-api/swagger-codegen/issues/4819","repository_url":"https://api.github.com/repos/swagger-api/swagger-codegen","labels_url":"https://api.github.com/repos/swagger-api/swagger-codegen/issues/4819/labels{/name}","comments_url":"https://api.github.com/repos/swagger-api/swagger-codegen/issues/4819/comments","events_url":"https://api.github.com/repos/swagger-api/swagger-codegen/issues/4819/events","html_url":"https://github.com/swagger-api/swagger-codegen/issues/4819","id":208516277,"number":4819,"title":"Javascript client produces a wrong object for a string enum type that is used with $ref","user":{"login":"knzm","id":143724,"avatar_url":"https://avatars3.githubusercontent.com/u/143724?v=4","gravatar_id":"","url":"https://api.github.com/users/knzm","html_url":"https://github.com/knzm","followers_url":"https://api.github.com/users/knzm/followers","following_url":"https://api.github.com/users/knzm/following{/other_user}","gists_url":"https://api.github.com/users/knzm/gists{/gist_id}","starred_url":"https://api.github.com/users/knzm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/knzm/subscriptions","organizations_url":"https://api.github.com/users/knzm/orgs","repos_url":"https://api.github.com/users/knzm/repos","events_url":"https://api.github.com/users/knzm/events{/privacy}","received_events_url":"https://api.github.com/users/knzm/received_events","type":"User","site_admin":false},"labels":[{"id":160493340,"url":"https://api.github.com/repos/swagger-api/swagger-codegen/labels/Client:%20JavaScript/Node.js","name":"Client: JavaScript/Node.js","color":"fad8c7","default":false},{"id":417609785,"url":"https://api.github.com/repos/swagger-api/swagger-codegen/labels/Feature:%20Enum","name":"Feature: Enum","color":"eeccff","default":false},{"id":64180581,"url":"https://api.github.com/repos/swagger-api/swagger-codegen/labels/Issue:%20Bug","name":"Issue: Bug","color":"e11d21","default":false},{"id":160482740,"url":"https://api.github.com/repos/swagger-api/swagger-codegen/labels/Need%20community%20contribution","name":"Need community contribution","color":"e3ff00","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/swagger-api/swagger-codegen/milestones/14","html_url":"https://github.com/swagger-api/swagger-codegen/milestone/14","labels_url":"https://api.github.com/repos/swagger-api/swagger-codegen/milestones/14/labels","id":2318070,"number":14,"title":"v2.2.3","description":"","creator":{"login":"wing328","id":934260,"avatar_url":"https://avatars2.githubusercontent.com/u/934260?v=4","gravatar_id":"","url":"https://api.github.com/users/wing328","html_url":"https://github.com/wing328","followers_url":"https://api.github.com/users/wing328/followers","following_url":"https://api.github.com/users/wing328/following{/other_user}","gists_url":"https://api.github.com/users/wing328/gists{/gist_id}","starred_url":"https://api.github.com/users/wing328/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wing328/subscriptions","organizations_url":"https://api.github.com/users/wing328/orgs","repos_url":"https://api.github.com/users/wing328/repos","events_url":"https://api.github.com/users/wing328/events{/privacy}","received_events_url":"https://api.github.com/users/wing328/received_events","type":"User","site_admin":false},"open_issues":3,"closed_issues":756,"state":"closed","created_at":"2017-02-13T09:01:07Z","updated_at":"2017-07-31T20:11:04Z","due_on":"2017-07-16T07:00:00Z","closed_at":"2017-07-16T17:55:47Z"},"comments":8,"created_at":"2017-02-17T18:11:47Z","updated_at":"2017-08-02T16:42:04Z","closed_at":"2017-05-25T03:06:19Z","body":"<!--\r\nPlease follow the issue template below for bug reports and feature requests.\r\nAlso please indicate in the issue title which language/library is concerned. Eg:  [JAVA] Bug generating foo with bar \r\n-->\r\n\r\n##### Description\r\n\r\n<!-- describe what is the question, suggestion or issue and why this is a problem for you. -->\r\n\r\nI got something like `{ '0': 'f', '1': 'i', '2': 's', '3': 'h' }` for an enum type. It should be `\"fish\"`.\r\n\r\n##### Swagger-codegen version\r\n\r\n2.2.2-SNAPSHOT (revision: bb81fc130aedd64a4d35232d5173b12ce758e967)\r\n\r\n##### Swagger declaration file content or url\r\n\r\n```yaml\r\nswagger: \"2.0\"\r\ninfo:\r\n  version: 1.0.0\r\nschemes:\r\n  - http\r\npaths:\r\n  /pet:\r\n    post:\r\n      parameters:\r\n      responses:\r\n        '405':\r\n          description: Invalid input\r\n\r\ndefinitions:\r\n  EnumArrays:\r\n    type: object\r\n    properties:\r\n      array_enum:\r\n        type: array\r\n        items:\r\n          type: string\r\n          enum:\r\n            - fish\r\n            - crab\r\n      array_enum_ref:\r\n        type: array\r\n        items:\r\n          $ref: '#/definitions/StringEnumObject'\r\n  StringEnumObject:\r\n    type: string\r\n    enum:\r\n      - fish\r\n      - crab\r\n```\r\n\r\n##### Command line used for generation\r\n\r\n<!-- including the language, libraries and various options -->\r\n\r\n```\r\n$ java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i test.yaml -l javascript -o output\r\n```\r\n\r\n##### Steps to reproduce\r\n\r\n<!-- unambiguous set of steps to reproduce the bug.-->\r\n\r\n1. Run codegen by the above command\r\n2. Cd into output directory, then run `npm install`\r\n3. Start a node interactive shell and type them:\r\n\r\n```\r\n$ node\r\n> var SwaggerClient = require('./src')\r\n> SwaggerClient.EnumArrays.constructFromObject({\"array_enum\": [\"fish\", \"crab\"]})\r\nexports { array_enum: [ 'fish', 'crab' ] }\r\n> SwaggerClient.EnumArrays.constructFromObject({\"array_enum_ref\": [\"fish\", \"crab\"]})\r\nexports {\r\n  array_enum_ref: \r\n   [ { '0': 'f', '1': 'i', '2': 's', '3': 'h' },\r\n     { '0': 'c', '1': 'r', '2': 'a', '3': 'b' } ] }\r\n```\r\n\r\n##### Related issues\r\n\r\n<!-- has a similar issue been reported before? Please do a search in https://github.com/swagger-api/swagger-codegen/issues?utf8=%E2%9C%93&q=is%3Aissue%20 -->\r\n\r\nI 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.\r\n\r\n#3639 [javascript client] constructFromObject calls undefined function constructFromObject on enums\r\n#3416 [JavaScript] String Enum types produce invalid code\r\n#2102 [JavaScript] Enums are not exported correctly\r\n\r\n##### Suggest a Fix\r\n\r\n<!-- if you can't fix the bug yourself, perhaps you can point to what might be\r\n  causing the problem (line of code or commit) -->\r\n\r\nI can fix the issue by modifying `convertToType` method defined in `ApiClient.mustache` like this:\r\n\r\n```diff\r\n--- a/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache\r\n+++ b/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache\r\n@@ -470,7 +470,7 @@\r\n         if (type === Object) {\r\n           // generic object, return directly\r\n           return data;\r\n-        } else if (typeof type === 'function') {\r\n+        } else if (typeof type.constructFromObject === 'function') {\r\n           // for model type like: User\r\n           return type.constructFromObject(data);\r\n         } else if (Array.isArray(type)) {\r\n```\r\n\r\nI think `ApiClient.convertToType()` can always call `type.constructFromObject()` for types that have `constructFromObject` method. But I'm not sure about its side effects.\r\n"},"comment":{"url":"https://api.github.com/repos/swagger-api/swagger-codegen/issues/comments/319729360","html_url":"https://github.com/swagger-api/swagger-codegen/issues/4819#issuecomment-319729360","issue_url":"https://api.github.com/repos/swagger-api/swagger-codegen/issues/4819","id":319729360,"user":{"login":"advance512","id":3903412,"avatar_url":"https://avatars1.githubusercontent.com/u/3903412?v=4","gravatar_id":"","url":"https://api.github.com/users/advance512","html_url":"https://github.com/advance512","followers_url":"https://api.github.com/users/advance512/followers","following_url":"https://api.github.com/users/advance512/following{/other_user}","gists_url":"https://api.github.com/users/advance512/gists{/gist_id}","starred_url":"https://api.github.com/users/advance512/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/advance512/subscriptions","organizations_url":"https://api.github.com/users/advance512/orgs","repos_url":"https://api.github.com/users/advance512/repos","events_url":"https://api.github.com/users/advance512/events{/privacy}","received_events_url":"https://api.github.com/users/advance512/received_events","type":"User","site_admin":false},"created_at":"2017-08-02T16:42:04Z","updated_at":"2017-08-02T16:42:04Z","body":"I can confirm that this issue has been solved when using v3.0.0."}}
{"actor":{"display_login":"advance512"}}