Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[typescript] Fix generation of enum models #7529

Merged
merged 1 commit into from
Sep 28, 2020
Merged

[typescript] Fix generation of enum models #7529

merged 1 commit into from
Sep 28, 2020

Conversation

bodograumann
Copy link
Contributor

Due to the fact that the consolidated typescript template was forked from master so long ago, there are still some bugs left that where fixed for other typescript generators in the meantime. This is one of them.

This fixes #665 for the consolidated typescript generator. (With sample definition)
The issue there is, that top-level enum schemas, which are not nested as attributes of other schemas are just generated as normal object schemas, which is not suited to represent the different allowed values.

Original fix for typescript-node was in PR #2266, merged as 8417c5b in version 4.1.0.

CC: @TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02)

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • If contributing template-only or documentation-only changes which will change sample output, build the project beforehand.
  • Run the shell script ./bin/generate-samples.shto update all Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*. For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

This fixes #665 for the consolidated typescript generator.
Original fix for typescript-node was in PR #2266, merged as
8417c5b in version 4.1.0.
@auto-labeler
Copy link

auto-labeler bot commented Sep 28, 2020

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

Copy link
Member

@macjohnny macjohnny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@macjohnny macjohnny merged commit 54dd185 into OpenAPITools:master Sep 28, 2020
@macjohnny macjohnny added this to the 5.0.0 milestone Sep 28, 2020
@duhseekoh
Copy link

Is there anyway I can pick this change up prior to a proper 5.0 release? im using the docker cli approach with typescript-axios. I tried pulling in the openapi-generator-cli:latest instead of v4.3.1 and it still doesn't generate types for top level enums. Any ideas?

@macjohnny
Copy link
Member

@duhseekoh the openapi-generator-cli:latest should contain the changes. please check the output of the version information to make sure you are using the right version.

@duhseekoh
Copy link

duhseekoh commented Oct 21, 2020

I confirmed in the output i'm using 5.0.0-SNAPSHOT.

An enum outlined in the swagger.json like this, isn't having a type generated for it when using the 5.0.0-SNAPSHOT + typescript-axios.

"components": {
  "schemas": {
    "Color": {
        "enum": [
          "BLUE",
          "YELLOW",
          "GREEN"
        ],
        "type": "string",
        "additionalProperties": false
    }
  }
}

@macjohnny
Copy link
Member

The fix in this PR is for the new typescript generator.
Would you like to fix this for the (old) typescript-axios generator? Please open an issue with a minimal reproduction example as posted above

@duhseekoh
Copy link

Oh, is the direction to use the new typescript generator? Is typescript-axios deprecated?

@macjohnny
Copy link
Member

@duhseekoh the typescript-axios generator is not deprecated, but the long-term goal would be to have all flavors of typescript generators united in the currently experimental typescript generator. This can happen if the community helps to port the current typescript generators.

@duhseekoh
Copy link

It turns out typescript-axios does in fact generate TS enums for top level spec enums.

What I discovered, after pulling the source for openapi-generator down and through some trial and error, is that the generator fails when it hits an enum with additionalProperties attribute on it. In open api generator local dev mode, it exits the process when it encounters this. When it's ran via the packaged cli, it silently ignores those enums, and thus they are never included in the script. This is on 4.3.1 and the latest 5.x snapshot.

I can create a bug if needed. But first I was curious if it was expected behavior that it should fail when including that extra attribute?

Fails

"components": {
  "schemas": {
    "Color": {
        "enum": [
          "BLUE",
          "YELLOW",
          "GREEN"
        ],
        "type": "string",
        "additionalProperties": false
    }
  }
}

Works (generates Color enum)

"components": {
  "schemas": {
    "Color": {
        "enum": [
          "BLUE",
          "YELLOW",
          "GREEN"
        ],
        "type": "string"
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[typescript] support for enum
3 participants