-
Notifications
You must be signed in to change notification settings - Fork 149
Support integer-backed enums #242
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
Conversation
Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateStringEnum.swift
Show resolved
Hide resolved
|
Can those Int enum values have names instead of just values? |
|
@mapedd unfortunately no, OpenAPI dictates that for integer-backed enums, the items are the integer values themselves. It's generally recommended to put a mapping into the MyEnum:
description: |-
-1: negative one
0: zero
1: positive one
type: integer
enum:
- -1
- 0
- 1 |
|
Would description be mapped to swift enum cases ? |
|
Unfortunately, no. You'd just see the comment in the generated comment on top of the |
|
Are there any reasons why couldn't this mapped like: enum MyEnum: Int { ? |
|
Well the OpenAPI specification doesn't define any names for integer-backed enums. I agree it'd be useful, but we'd have to pick an arbitrary convention, which might differ to other conventions. So far we've avoided creating our own flavor of the OpenAPI specification on top of the official one. Are you aware of commonly used conventions for documenting integer-backed enums in OpenAPI? Maybe there is a widely adopted one that we could also follow. But I'm hesitant to just create one ourselves, as OpenAPI documents are language-agnostic and we don't want to diverge from the official specification unless necessary for generating valid Swift code. |
|
I see you point. But I'd counter that, the generated code for integer enums is not valid Swift then. |
|
I will check how other code generators deal with this case, seems kinda obvious to be able to encode some meaning into the values of an enum ... |
|
Yeah this project is meant to faithfully generate code from an arbitrary OpenAPI document, and since the OpenAPI specification doesn't provide a way to give integer enum cases a name, there isn't an obvious solution to this (there might be some highly opinionated ones that aren't part of the specification though). This project is not meant to fill in gaps in the OpenAPI specification itself, people can always wrap the generated code in a hand written layer and provide prettier APIs and still get the benefits of a typesafe API client/server. |
|
@czechboy0 isn't that what |
|
@mooshee Possibly, we don't support any vendor extensions in Swift OpenAPI Generator, the focus is on the official specification. |
Motivation
Fixes #241.
Modifications
Made the existing string-backed enum generation logic a tiny bit more generic, and integer support just fell out of it.
Result
Now integer-backed enums are also supported.
Test Plan
Updated tests.