OpenAPITools / OpenAPITools/openapi-generator

[BUG] Incorrect (de)serialization of reserved words (typescript-angular)

Open
#9,521 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
Description

The typescript-angular code generator does escape reserved words, but doesn't annotate these fields so the (de)serializer doesn't properly (de)serializes these fields. Assume that an API defines a Counting object that only holds an in field. This specified an object-type called Counting that has only an in field. This is emitted like this:

export interface Counting { 
    _in?: number;
}

Clients will write the _in field (with an underscore) and it will be serialized as { "_in": 123 } which won't be understood by the server. The same problem occurs when the server sends { "in": 123 } and it will be stored in a Javascript object that contains the in field. But clients will look for the _in field that is undefined.

openapi-generator version

This was checked with the latest openapi generator (v5.2.0).

OpenAPI declaration file content or url
{
  "openapi": "3.0.1",
  "info": {
    "title": "TEST",
    "version": "v1"
  },
  "paths": {},
  "components": {
    "schemas": {
      "Counting": {
        "type": "object",
        "properties": {
          "in": {
            "type": "integer",
            "format": "int32"
          }
        }
      }
    }
  }
}
Possible fix

The generated code will use the standard Angular HttpClient. Unfortunately, this class uses the standard json functions json.stringify and json.parse, so there is no standard way to fix the JSON serialization.

An option could be to annotate the fields (just like the standard typescript generator does) and to add an Angular HttpInterceptor that performs the actual JSON serialization and takes this mapping into account. It would also require the registration of the HTTP interceptor in the pipeline.

Another option would be to abort code-generation when reserved words are encountered in the model. Now, it silently fails to work and may go unnoticed. Aborting code-generation indicates there is a bug in the code.

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 running the supplied OpenAPI declaration through the typescript-angular generator and inspect the generated Counting model and its serialization behavior for the reserved in field. Review the two proposed directions—field annotations with an Angular HttpInterceptor, or rejecting reserved words—and define which behavior should be implemented before changing the generator.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.