OpenAPITools / OpenAPITools/openapi-generator

[BUG][typescript-fetch] Any schema overlaps with the type of another property of the same name.

Open
#8,187 1 comment 1 reaction 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)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

If the schemas have Hoge and Fuga and the property in Fuga has hoge, then this Fuga > hoge is characteristic only if the title name is the same as Hoge and lowercase hoge (case sensitive). Hoge properties overlap with Fuga > hoge properties for generated Models.

openapi-generator version

3.0.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Sample API
  version: 1.0.0
components:
  schemas:
    Hoge:
      type: object
      properties:
        hogeid:
          type: integer
          format: int64
          title: id
        hogename:
          type: string
          title: name
    Fuga:
      type: object
      properties:
        fugaid:
          type: integer
          format: int64
          example: 1
          title: id
        fuganame:
          type: string
          example: Laptop
          title: name
        hoge:
          title: hoge
          type: object
          properties:
            id:
              type: integer
              format: int64
              example: 1
              title: id
            name:
              type: string
              example: Laptop
              title: name
          required:
          - id
          - name
      required:
      - hoge
paths:
  /hoge:
    get:
      description: hoge
      responses:
        '200':
          description: hoge
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Hoge'
  /fuga:
    get:
      description: fuga
      responses:
        '200':
          description: fuga
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Fuga'
Generation Details

This report is node.js execution environment.

node.js: v14.15.0
@openapitools/openapi-generator-cli: ^2.1.5

The symptom is that Hoge's properties should be hogeid and hogename, but they are replaced by Fuga's hoge property type (id, name).

// Hoge.ts
/**
 * 
 * @export
 * @interface Hoge
 */
export interface Hoge {
    /**
     * 
     * @type {number}
     * @memberof Hoge
     */
    id: number; // Oops... Fuga's hoge property overlap it...
    /**
     * 
     * @type {string}
     * @memberof Hoge
     */
    name: string;
}
// Fuga.ts
import {
    Hoge,
    HogeFromJSON,
    HogeFromJSONTyped,
    HogeToJSON,
} from './';

/**
 * 
 * @export
 * @interface Fuga
 */
export interface Fuga {
    /**
     * 
     * @type {number}
     * @memberof Fuga
     */
    fugaid?: number;
    /**
     * 
     * @type {string}
     * @memberof Fuga
     */
    fuganame?: string;
    /**
     * 
     * @type {Hoge}
     * @memberof Fuga
     */
    hoge: Hoge; // The reference is Hoge, but the contents match with the properties of Fuga> hoge.
}

This can be fixed by removing the title or renaming the title to something other than hoge.
ex) If you try to change the title of Fuga> hoge to upper camel case.

Fuga:
    #...
    hoge:
        title: Hoge # <= I made it into an upper camel case
        type: object
        properties:
        id:
            type: integer
            format: int64
            example: 1
            title: id
        name:
            type: string
            example: Laptop
            title: name
        required:
        - id
        - name

Result is ...

// Hoge.ts
/**
 * 
 * @export
 * @interface Hoge
 */
export interface Hoge {
    /**
     * 
     * @type {number}
     * @memberof Hoge
     */
    hogeid?: number;
    /**
     * 
     * @type {string}
     * @memberof Hoge
     */
    hogename?: string;
}
// Fuga.ts
import {
    Hoge1,
    Hoge1FromJSON,
    Hoge1FromJSONTyped,
    Hoge1ToJSON,
} from './';

/**
 * 
 * @export
 * @interface Fuga
 */
export interface Fuga {
    /**
     * 
     * @type {number}
     * @memberof Fuga
     */
    fugaid?: number;
    /**
     * 
     * @type {string}
     * @memberof Fuga
     */
    fuganame?: string;
    /**
     * 
     * @type {Hoge1}
     * @memberof Fuga
     */
    hoge: Hoge1;
}
// Fuga's hoge model type file.
/**
 * 
 * @export
 * @interface Hoge1
 */
export interface Hoge1 {
    /**
     * 
     * @type {number}
     * @memberof Hoge1
     */
    id: number;
    /**
     * 
     * @type {string}
     * @memberof Hoge1
     */
    name: string;
}
Steps to reproduce

Use CLI for

$ openapi-generator-cli generate -g typescript-fetch -i sample.yaml -o ./sample/yaml --additional-properties=typescriptThreePlus=true,modelPropertyNaming=original
Related issues/PRs

I looked for it but couldn't find it 🥺.

Suggest a fix

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

Run the provided openapi-generator-cli command with sample.yaml and inspect the generated Hoge.ts, Fuga.ts, and nested model files. Trace the typescript-fetch model generation path to see why Fuga.hoge overwrites Hoge's properties; done means Hoge retains hogeid and hogename while the nested model has distinct id and name properties.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.