[JavaScript] Client produces a wrong object for a string enum type that is used with $ref
Created by: advance512
Description
Quoting the initial post in https://github.com/swagger-api/swagger-codegen/issues/4819.
I got something like { '0': 'f', '1': 'i', '2': 's', '3': 'h' } for an enum type. It should be "fish".
openapi-generator version
openapi-generator-cli-3.3.3-20181113.090410-41.jar
OpenAPI declaration file content or url
swagger: "2.0"
info:
version: 1.0.0
schemes:
- http
paths:
/pet:
post:
parameters:
responses:
'405':
description: Invalid input
definitions:
EnumArrays:
type: object
properties:
array_enum:
type: array
items:
type: string
enum:
- fish
- crab
array_enum_ref:
type: array
items:
$ref: '#/definitions/StringEnumObject'
StringEnumObject:
type: string
enum:
- fish
- crab
Command line used for generation
$ java -jar modules/swagger-codegen-cli/target/openapi-generator-cli-3.3.3-20181113.090410-41.jar generate -i test.yaml -l javascript -o output
Steps to reproduce
A reproduction is available at: https://github.com/advance512/swagger-codegen-issue-4819/tree/openapi-generator-cli-3.3.3-20181113.090410-41.jar
Related issues/PRs
A lot more information available here: https://github.com/swagger-api/swagger-codegen/issues/4819
Suggest a fix/enhancement
I can fix the issue by modifying convertToType
method defined in ApiClient.mustache
like this:
--- a/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache
+++ b/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache
@@ -470,7 +470,7 @@
if (type === Object) {
// generic object, return directly
return data;
- } else if (typeof type === 'function') {
+ } else if (typeof type.constructFromObject === 'function') {
// for model type like: User
return type.constructFromObject(data);
} else if (Array.isArray(type)) {
I think ApiClient.convertToType()
can always call type.constructFromObject()
for types that have constructFromObject
method. But I'm not sure about its side effects.