OpenAPITools / OpenAPITools/openapi-generator
[BUG] [JavaScipt] Babel-Error while generating Integer Enums into JavaScript
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
[BUG] [JavaScipt] Issue with Integer Enums in JavaScript generation
openapi-generator version
6.2.1
Description
I also set up a repository to reproduce this bug easily
https://github.com/thismusicdude/openapi-enums
Case 1: Bug that occurred while developing
According to the OpenAPI specification, the enum keyword is used to specify a set of acceptable values for a parameter or property. Since the OpenAPI specification allows for the use of JSON objects to describe API operations and parameters which can include both string values and numerical values, my Team and I assumed that the enum keyword in the OpenAPI specification can be used also with numerical values. As written in the JSON specification .
After we compiled this following code with the openapi-generator via openapi-generator-cli to javascript, we ran into some issues.
# from File: openapi.yaml
firstCase:
type: integer
format: int32
enum:
- 0
- 25
- 50
- 75
- 100
default: 50
If we now take a look into the files of the generated module, we see this code-snippet
// from File: api/src/model/CoolParameters.js (generated)
/**
* @member {module:model/CoolParameters.FirstCaseEnum} firstCase
* @default FirstCaseEnum.50
*/
CoolParameters.prototype['firstCase'] = FirstCaseEnum.50;
/* . . . */
/**
* Allowed values for the <code>firstCase</code> property.
* @enum {Number}
* @readonly
*/
CoolParameters['FirstCaseEnum'] = {
/**
* value: 0
* @const
*/
"0": 0,
/**
* value: 50
* @const
*/
"50": 50,
/**
* value: 100
* @const
*/
"100": 100
};
While building the generated Code, Babel -which is already included in the generated module- throws a BABEL_PARSE_ERROR with the code MissingSemicolon. Which makes sense, since the generated Code in the first codeline is not ECMAScript compatible.
An easy way how this could be fixed, would be maybe to generate the code as following:
CoolParameters.prototype['firstCase'] = FirstCaseEnum['50'];
Other Test Cases we experimented with
After this incident we asked ourselves if there could be also other test-cases that could result in a similiar issue. So we defined in the included yaml file other parameters with other potential cases, that could result into issues while developing.
(To get to the parameters that throw an error through babel, just comment out the various previous parameters in the yaml file of the repo that throws an error.)
Case 2: secondCase
# from File openapi.yaml
secondCase:
enum:
- 5
- "5"
default: 5
Babel throws BABEL_PARSE_ERROR with the code MissingSemicolon.
Same error as described above. The generated code is the following:
// from File: api/src/model/CoolParameters.js (generated)
/**
* Allowed values for the <code>secondCase</code> property.
* @enum {Number}
* @readonly
*/
CoolParameters['SecondCaseEnum'] = {
/**
* value: 5
* @const
*/
"5": 5,
/**
* value: 5
* @const
*/
"5": 5
};
Case 3: thirdCase
# from File: openapi.yaml
thirdCase:
enum:
- 5
- "5"
default: "5"
compiles, but gives the following code as result, which could lead to some issues
// from File: api/src/model/CoolParameters.js (generated)
/**
* Allowed values for the <code>secondCase</code> property.
* @enum {Number}
* @readonly
*/
CoolParameters['thirdCaseEnum'] = {
/**
* value: 5
* @const
*/
"5": 5,
/**
* value: 5
* @const
*/
"5": 5
};
Case 4: fourthCase
So we tried the same with null and "null"
# from File: openapi.yaml
fourthCase:
enum:
- null
- "null"
default: null
Which results in
CoolParameters['FourthCaseEnum'] = {
// from File: api/src/model/CoolParameters.js (generated)
/**
* value: "null"
* @const
*/
"null": "null",
/**
* value: "null"
* @const
*/
"null": "null"
};
Since there is a null value this could also lead to some issues. If that is not that easy fixable: maybe the code generator could throw an exception which indicates that there are duplicate values.
OpenAPI declaration file content
openapi: 3.0.3
info:
version: 1.0.0
title: openapi-enums
description: >-
A minimal example that shows some issues with enums by generated code in via openapi-generator
servers:
# Added by API Auto Mocking Plugin
- description: SwaggerHub API Auto Mocking
url: https://virtserver.swaggerhub.com/MAX98TONTON/openapi-enums/1.0.0
components:
schemas:
coolParameters:
type: object
properties:
firstCase:
type: integer
format: int32
enum:
- 0
- 50
- 100
default: 50
secondCase:
enum:
- 5
- "5"
default: 5
thirdCase:
enum:
- 5
- "5"
default: "5"
fourthCase:
enum:
- null
- "null"
default: null
paths:
/enumBug:
post:
summary: Create a new planning session
operationId: createSession
tags:
- Session
description:
Creates a new planning session for the referenced patient and body site. Further stateful API calls will happen in the
context of that session. After the session is no longer required a client needs to cancel the session.
requestBody:
content:
application/json:
schema:
type: object
properties:
oneEnum:
$ref: "#/components/schemas/coolParameters"
required:
- oneEnum
responses:
"200":
description: A JSON object describing the session
"400":
description: Missing parameters
Generation Details
openapi-generator-cli generate -o ./api -i ./openapi.yaml -g javascript && cd api && npm install && npm audit fix --force
Steps to reproduce
-
clone this Repository
-
install dependencies with
npm install -
generate API Code with
npm run generate-api- or instead use the following command:
openapi-generator-cli generate -o ./api -i ./openapi.yaml -g javascript && cd api && npm install && npm audit fix --force
- or instead use the following command:
Potential fix for the first major bug that occured
Integer Enum Default Parameter
The major issue we had was the error thrown by babel. Instead of generating the code whle setting the default parameters of an enum like this
CoolParameters.prototype['firstCase'] = FirstCaseEnum.50;
An better way how this could be fixed, would be maybe to generate the code as following:
CoolParameters.prototype['firstCase'] = FirstCaseEnum['50'];
Fixes for other defined test-cases
Multiple Enum Valuess with same name
Maybe the code generator could also throw an exception which indicates that there are duplicate values in the generated enum dictionary, also when various types are used, like string and integer.
Typisation
If possible it would also help to generate into the right typisations. (Strings to Strings, Integers to Integers). Even though typisation plays a minor role in Javascript, it would still look nicer in the generated code.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue from openapi.yaml using the documented npm install and npm run generate-api commands, then inspect the generated api/src/model/CoolParameters.js. Compare the generated enum defaults and keys across the listed numeric, mixed-type, and null cases; done means the JavaScript output parses successfully and the reported enum cases are handled consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100