OpenAPITools / OpenAPITools/openapi-generator

[BUG][typescript-angular & maybeothers] hasEnums returns true for reference enums leading in empty namespace generation

Open
#15,334 0 comments 4 reactions 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

Description

When a schema definition has a reference to and another enum schema, the mustache template value hasEnums is set to true, despite the schema itself not having an enum.
This causes typescript-angular/modelEnum.mustache, to render an empty namespace field.

openapi-generator version

6.5.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: hasEnums Demo
  version: 1.0.0
paths: {}
components:
  schemas:
    SchemaWithRefToEnum:
      type: object
      properties:
        foo:
          type: string
        bar:
          $ref: '#/components/schemas/Baz'
    Baz:
      enum:
        - Foo
        - Bar
      type: string
Steps to reproduce

generate the angular client with openapi-generator-cli generate -i [file.yaml] -g typescript-angular -o output-directory.

Then look into "output-directory/model/schemaWithRefToEnum.ts" and you will find the following result:

///...
import { EnumSchema } from './enumSchema';


export interface SchemaWithRefToEnum { 
    foo?: string;
    refToEnum?: EnumSchema;
}
export namespace SchemaWithRefToEnum {
}

The export namespace SchemaWithRefToEnum { section is empty an not needed, since it would only be present, if there was an enum directly referenced. Since it is not, this sections is empty.

Adding a bit of debug information shows the following

// modelGenericEnums.mustache
// hasEnums: true
export namespace SchemaWithRefToEnum {
// foo isEnum false
// bar isEnum false
}
Related issues/PRs

It is worth mention, that this issue is not present on https://editor.swagger.io/. Generating the typescript-angular client from there, results in the expected result --> no empty namespace is included.

Suggest a fix

If an enum is referenced via $ref, the hasEnums should not be true.

Possible places for the error:

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

Reproduce the issue with the YAML declaration and the typescript-angular generation command, then inspect model/schemaWithRefToEnum.ts and typescript-angular/modelEnum.mustache. Trace how hasEnums is populated, including the suggested TypeScript codegen methods. Done means a referenced enum no longer causes an empty namespace to be generated.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, java, typescript
Domain
devtools, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.