OpenAPITools / OpenAPITools/openapi-generator

[BUG][angular-typescript] Generated model property names not compatbile with api spec in case of reserved keywords

Open
#6,282 7 comments 2 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

Typescript model classes have wrong property names in case they are reserved keywords. I guess the same would happen if they are not
valid typescript identifiers for other reasons.

openapi-generator version

all versions I know. Tested on 4.3.1 and master.

OpenAPI declaration file content or url

api.yaml:

openapi: '3.0.0'
info:
  description: Reproducer for openapi issue
  version: 1.0.0
  title: Reproducer for openapi issue
servers:
  - url: https://example.com
paths:
  /inputOptions:
    get:
      responses:
        200:
          description: An api call returning the ExampleObject with broken model
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ExampleObject'
components:
  schemas:
    ExampleObject:
      type: object
      required: ['function']
      properties:
        function:
          type: string
Command line used for generation
java -jar ~/git/openapi-generator/modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i api.yaml -o generated/ -g typescript-angular
Steps to reproduce

Using the generated client with an backend using the original property name - in my case a spring-java backend will cause the _function property to be undefined while the function property is set (but not declared in the generated model). This is not very surprising, so looking at the generated file generated/model/exampleObject.ts should be enough:

export interface ExampleObject { 
    _function: string;
}
Related issues/PRs
Suggest a fix

The generated code should use the original property names to be compatible with the api spec. If names cannot be used safely as identifiers, StringLiterals could be used (see https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#22-names):

export interface ExampleObject { 
    "function": string;
}

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 with the api.yaml reproducer and the typescript-angular generator command, then inspect generated/model/exampleObject.ts. Compare the generated _function property with the original function name and the suggested TypeScript string-literal form. Done means regenerating the model preserves the API property name safely.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.