question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG][TYPESCRIPT] Property names should not be sanitized, but quoted instead

See original GitHub issue

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
  1. Generate API with above command and yaml file.
  2. Check Example interface in generated api.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:open
  • Created 4 years ago
  • Reactions:4
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
ffMathycommented, Dec 19, 2019

I agree with this.

1reaction
smorandicommented, Jun 24, 2020

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…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cleaning, Disinfecting, and Sanitizing - MedlinePlus
Some of them are helpful, but others are harmful and cause ... Disinfecting does not necessarily clean dirty surfaces or remove germs.
Read more >
Don't try to sanitize input. Escape output. - Ben Hoyt
Why you should escape output correctly, but generally not sanitize user input. ... sets their name to include double quotes, like "; badFunc();...
Read more >
How To Clean and Disinfect Schools To Help Slow the Spread ...
Cleaning works by using soap (or detergent) and water to physically remove germs from surfaces. This process does not necessarily kill germs, but...
Read more >
Cleaning and Sanitizing
When considering a good cleaner the following properties should be considered: ... Clean-in-place systems (C.I.P.) - is an automated cleaning system.
Read more >
9 best EPA-registered disinfectant sprays, wipes and more
With that being said, disinfectants are helpful to have at home, but they're not everyday, all-purpose cleaners and should not be used as ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found