OpenAPITools / OpenAPITools/openapi-generator
[BUG][TYPESCRIPT] Property names should not be sanitized, but quoted instead
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- What's the version of OpenAPI Generator used?
- Have you search for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Bounty to sponsor the fix (example)
Description
As a developer I expect the modelPropertyNaming='original' to keep property names equal to the name provided in the openapi spec file. Not all variable names are accepted in TypeScript (and JavaScript) so for variable names it is expected to sanitize the names to generate valid code. For property names this is not the case, but the property names are still sanitized and altered. This should only happen when not doing this would produce invalid code. This is not the case, property names can be quoted to allow all possible names.
At the moment this generates clients that are not interoperable with the server
Actual:
/**
*
* @export
* @interface Example
*/
export interface Example {
/**
*
* @type {string}
* @memberof Example
*/
type?: string;
/**
*
* @type {string}
* @memberof Example
*/
mime_type?: string;
}
Expected:
/**
*
* @export
* @interface Example
*/
export interface Example {
/**
*
* @type {string}
* @memberof Example
*/
"@type"?: string;
/**
*
* @type {string}
* @memberof Example
*/
"mime-type"?: string;
}
openapi-generator version
4.1.2
OpenAPI declaration file content or url
openapi: 3.0.0
info:
title: modelNamingExample
version: v1
paths:
/example:
post:
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/Example"
responses:
"200":
description: ""
content:
application/json:
schema:
$ref: "#/components/schemas/Example"
components:
schemas:
Example:
description: ""
type: object
properties:
"@type":
type: string
"mime-type":
type: string
Command line used for generation
Note: This is not only limited to the typescript-axios generator but all typescript-* generators. The AbstractTypeScriptClientCodegen.java file, which is used in all / most TypeScript generators, causes the problem.
openapi-generator generate \
-i example.yaml \
-g typescript-axios \
-o generated-sources/openapi \
--additional-properties=supportsES6=true,modelPropertyNaming='original'
Steps to reproduce
- Generate API with above command and yaml file.
- Check
Exampleinterface in generatedapi.tsfile.
Related issues/PRs
I know there are some similar issues, but those are more on a specific case, like an @ being removed or a - being replaced by an _. This issue is more focussed on the sanitizing of property names in general which shouldn't happen unless needed.
https://github.com/OpenAPITools/openapi-generator/issues/4065
Suggest a fix
I would suggest wrapping all property names (so not variable names) in quotes. You could argue that to keep it clean you should only wrap property names that require quotes to be valid should have quotes, but it would add complexity that is not really needed.
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
Start by reading AbstractTypeScriptClientCodegen.java at the property-name sanitization logic, then reproduce the issue with the provided example.yaml and typescript-axios command using modelPropertyNaming='original'. Done means generated TypeScript interfaces preserve names such as "@type" and "mime-type" by quoting them rather than sanitizing them.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100