Skip to content

[all] add "x-enum-varnames" extension to control enum varname#917

Merged
jmini merged 2 commits into
OpenAPITools:masterfrom
osjupiter:feature/xenumname
Aug 30, 2018
Merged

[all] add "x-enum-varnames" extension to control enum varname#917
jmini merged 2 commits into
OpenAPITools:masterfrom
osjupiter:feature/xenumname

Conversation

@osjupiter
Copy link
Copy Markdown
Contributor

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\.
  • Filed the PR against the correct branch: master, 3.3.x, 4.0.x. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.

Description of the PR

(details of the change, additional tests that have been done, reference to the issue for tracking, etc)

For issue #893
Add "x-enum-varnames" vendor extension to control names of generated enum properties.

private void updateEnumVarsWithExtensions(List<Map<String, Object>> enumVars, Map<String, Object> vendorExtensions) {
if (vendorExtensions != null && vendorExtensions.containsKey("x-enum-varnames")) {
List<String> alias = (List<String>) vendorExtensions.get("x-enum-varnames");
if (alias.size() == enumVars.size()) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maye you could just log a warning if the sizes do not match.

In my opinion it is OK to still change the first two defined names in a case like this:

      EnumProp:
        type: string
        enum:
          - a
          - b
          - c
        x-enum-varnames:
          - FOO
          - BAR
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I see.
Then I'll make changes to allow to set different number of elements from target enum.

@jmini jmini mentioned this pull request Aug 29, 2018
@jmini jmini changed the title add "x-enum-varnames" extension to control enum varname [all] add "x-enum-varnames" extension to control enum varname Aug 30, 2018
@jmini jmini added this to the 3.2.3 milestone Aug 30, 2018
@jmini
Copy link
Copy Markdown
Member

jmini commented Aug 30, 2018

Tested with a java client generator with:

    EnumWithCustomName:
      type: integer
      format: int32
      enum:
        - 1
        - 2
      x-enum-varnames:
        - FOO
        - BAR

The produced code EnumWithCustomName.java looks good.

@jmini jmini merged commit 62abd51 into OpenAPITools:master Aug 30, 2018
@jmini
Copy link
Copy Markdown
Member

jmini commented Aug 30, 2018

Thank you for this great contribution!

@wing328
Copy link
Copy Markdown
Member

wing328 commented Aug 30, 2018

@osjupiter thanks for the PR, which has been included in the v3.2.3 release: https://twitter.com/oas_generator/status/1035200785066254336

jmini added a commit to jmini/openapi-experiments that referenced this pull request Sep 1, 2018
@phstudy
Copy link
Copy Markdown
Contributor

phstudy commented Sep 26, 2018

The codegen of integer type enum model works fine. However, If I reference the integer type enum in other model, it always will be treat as a outer class enum.

This issue was fixed in #1120.

A-Joshi pushed a commit to ihsmarkitoss/openapi-generator that referenced this pull request Feb 27, 2019
…ITools#917)

* add "x-enum-varnames" extension to control the naming of the enum parameter name
* allow to set different size from target enum
@akhileshnair08
Copy link
Copy Markdown

akhileshnair08 commented Aug 4, 2020

ABCEnum:
enum:
-{0,"aum"}
-{1,"gautham"}
-{2,"Buddha"}
x-enum-varnames:
- AUM
- GAUTHAM
- BUDDHA
type: object
properties:
num_value:
type: integer
format: int32
str_value:
type: string

Why is this not working???
getting below error:
unexpected error in Open-API generation (org.openapitools:openapi-generator-maven-plugin:4.1.3:generate:default:generate-sources)

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