Skip to content

Conversation

@czechboy0
Copy link
Contributor

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.

@czechboy0 czechboy0 merged commit e7cac50 into apple:main Sep 1, 2023
@czechboy0 czechboy0 deleted the hd-support-int-backed-enums branch September 1, 2023 17:38
@czechboy0 czechboy0 added the 🔨 semver/patch No public API change. label Sep 4, 2023
@mapedd
Copy link

mapedd commented Oct 26, 2023

Can those Int enum values have names instead of just values?

@czechboy0
Copy link
Contributor Author

@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 description field of the schema itself, such as:

MyEnum:
  description: |-
    -1: negative one
    0: zero
    1: positive one  
  type: integer
  enum:
    - -1
    - 0
    - 1

@mapedd
Copy link

mapedd commented Oct 26, 2023

Would description be mapped to swift enum cases ?

@czechboy0
Copy link
Contributor Author

Unfortunately, no. You'd just see the comment in the generated comment on top of the MyEnum type.

@mapedd
Copy link

mapedd commented Oct 26, 2023

Are there any reasons why couldn't this mapped like:

enum MyEnum: Int {
case myCase = 1
...
}

?

@czechboy0
Copy link
Contributor Author

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.

@mapedd
Copy link

mapedd commented Oct 26, 2023

I see you point. But I'd counter that, the generated code for integer enums is not valid Swift then.
It compiles , sure, but 99.99% of people would write integer backed enum like this:

enum MyEnum: Int {
   case myCaseA = 1
   case myCaseB = 2
   ...
}

@mapedd
Copy link

mapedd commented Oct 26, 2023

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 ...

@czechboy0
Copy link
Contributor Author

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.

@mooshee
Copy link

mooshee commented Feb 10, 2024

@czechboy0 isn't that what x-enum-varnames is for?

@czechboy0
Copy link
Contributor Author

@mooshee Possibly, we don't support any vendor extensions in Swift OpenAPI Generator, the focus is on the official specification.

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

Labels

🔨 semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support integer-based enums

4 participants