[BUG][TYPESCRIPT] Property names should not be sanitized, but quoted instead
See original GitHub issueBug 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
Example
interface in generatedapi.ts
file.
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.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:10 (2 by maintainers)
I agree with this.
yes please, this would really be helpful. or maybe, if you don’t want to change existing behaviour make it configurable by a new value for the modelNamingProperty, like “originalNoSanitation” or something along this line…