OpenAPITools / OpenAPITools/openapi-generator

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

Open
#4,748 10 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: TypeScript Issue: Bug
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
  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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.