Skip to content

Adding support for named integer enums #1214

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

Merged
merged 9 commits into from
Mar 27, 2025

Conversation

barrybarrette
Copy link
Contributor

Adding support for named enums via an optional extension, x-enum-varnames.

This extension is added to the schema inline with the enum definition:

 "Siva.e_Install_State": {
        "enum": [
          0,
          1,
          2,
          3,
          4,
          5,
          6,
          99
        ],
        "type": "integer",
        "format": "int32",
        "x-enum-varnames": [
          "Deinstalled",
          "Installed",
          "Upcoming_Site",
          "Lab_Site",
          "Pending_Deinstall",
          "Suspended",
          "Install_In_Progress",
          "Unknown"
        ]
      },

The result:
image

@barrybarrette barrybarrette changed the title Adding support for named enums Adding support for named integer enums Mar 6, 2025
@dbanty
Copy link
Collaborator

dbanty commented Mar 15, 2025

I've resisted adding custom OpenAPI attributes just for this generator... ideally, we could adopt conventions used by more popular projects, like OpenAPITools. Is x-enum-varnames used elsewhere already?

@barrybarrette
Copy link
Contributor Author

ideally, we could adopt conventions used by more popular projects, like OpenAPITools. Is x-enum-varnames used elsewhere already?

Yes, OpenAPITools adopted it in this PR -> OpenAPITools/openapi-generator#917

I did also search around for any other official or even adopted standard for this but came up empty.

@barrybarrette
Copy link
Contributor Author

Just fixed the possible null reference that failed tests. Also added a sanity check to ensure the lengths of the var_names matches the length of the actual values, inspired by the OpenAPITools implementation.

Copy link
Collaborator

@dbanty dbanty left a comment

Choose a reason for hiding this comment

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

Looking good! In addition to the one thought below, could you add something to the README about this feature? It's kinda the only docs we have right now 😓

Copy link
Collaborator

@dbanty dbanty left a comment

Choose a reason for hiding this comment

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

I think this is good to go. We should come up with a way to emit warnings effectively that don't stop code from being generated, but that needs some broader thinking outside this change.

@dbanty dbanty added this pull request to the merge queue Mar 27, 2025
Merged via the queue into openapi-generators:main with commit dd9ad5a Mar 27, 2025
22 checks passed
@knope-bot knope-bot bot mentioned this pull request Mar 27, 2025
github-merge-queue bot pushed a commit that referenced this pull request Mar 31, 2025
> [!IMPORTANT]
> Merging this pull request will create this release

## Features

### Adding support for named integer enums

#1214 by @barrybarrette

Adding support for named integer enums via an optional extension,
`x-enum-varnames`.

This extension is added to the schema inline with the `enum` definition:
```
"MyEnum": {
    "enum": [
        0,
        1,
        2,
        3,
        4,
        5,
        6,
        99
    ],
    "type": "integer",
    "format": "int32",
    "x-enum-varnames": [
        "Deinstalled",
        "Installed",
        "Upcoming_Site",
        "Lab_Site",
        "Pending_Deinstall",
        "Suspended",
        "Install_In_Progress",
        "Unknown"
    ]
}
```

The result:

![image](https://github.com/user-attachments/assets/780880b3-2f1f-49be-823b-f9abb713a3e1)

Co-authored-by: knope-bot[bot] <152252888+knope-bot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants