OpenAPITools / OpenAPITools/openapi-generator

[BUG] [typescript] 'allOf' Inheritance causes enums to not be generated

Open
#8,618 4 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)?
  • 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

When using allOf, an enum defined in the child object is not generated.

openapi-generator version

Discovered with 5.0.0, validated against master

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  description: example
  version: 0.0.0
  title: example
servers:
  - url: /
paths:
  /foo:
    get:
      tags:
        - foo
      summary: Lists all foos
      operationId: listFoos
      responses:
        '200':
          $ref: '#/components/responses/FooArrayResponse'
components:
  schemas:
    Foo:
      type: object
      title: A metasyntactic object
      allOf:
        - $ref: '#/components/schemas/Base'
      required:
      - mode
      properties:
        mode:
          type: string
          enum:
            - cloud
            - native

    Base:
      type: object
      title: Base class
      required:
        - id
      properties:
        id:
          description: Unique identifier
          format: uuid
          type: string

  responses:
    FooArrayResponse:
      description: Response containing multiple foos
      content:
        '*/*':
          schema:
            type: array
            items:
              $ref: '#/components/schemas/Foo'

Resultant foo.ts:

import { RequestFile } from './models';
import { Base } from './base';

export class Foo {
    'mode': Foo.ModeEnum;
    /**
    * Unique identifier
    */
    'id': string;

    static discriminator: string | undefined = undefined;

    static attributeTypeMap: Array<{name: string, baseName: string, type: string}> = [
        {
            "name": "mode",
            "baseName": "mode",
            "type": "Foo.ModeEnum"
        },
        {
            "name": "id",
            "baseName": "id",
            "type": "string"
        }    ];

    static getAttributeTypeMap() {
        return Foo.attributeTypeMap;
    }
}

Expected foo.ts:

import { RequestFile } from './models';
import { Base } from './base';

export class Foo {
    'mode': Foo.ModeEnum;
    /**
    * Unique identifier
    */
    'id': string;

    static discriminator: string | undefined = undefined;

    static attributeTypeMap: Array<{name: string, baseName: string, type: string}> = [
        {
            "name": "mode",
            "baseName": "mode",
            "type": "Foo.ModeEnum"
        },
        {
            "name": "id",
            "baseName": "id",
            "type": "string"
        }    ];

    static getAttributeTypeMap() {
        return Foo.attributeTypeMap;
    }
}

export namespace Foo {
    export enum ModeEnum {
        Cloud = <any> 'cloud',
        Native = <any> 'native'
    }
}
Steps to reproduce

java -jar openapi-generator-cli.jar generate -g typescript-node -i ./example.yml -o tmp/

Workarounds

If you define the enum as its own model, you can then $ref it with no issue:

components:
  schemas:
    ModeEnum:
      type: string
      enum:
        - cloud
        - native

...

      properties:
        mode:
          $ref: '#/components/schemas/ModeEnum'

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 typescript-node generator and the example.yml reproduction, then run the documented java -jar openapi-generator-cli.jar command. Compare the generated foo.ts with the expected output; the work is done when the Foo.ModeEnum namespace is generated for the child enum while the inherited fields remain present.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.