[Swift] Safer handling of enums
Created by: joeboyscout04
Description
There are known issues around how Swagger handles enums. In Swift, if we attempt to parse an enum that doesn't exist, then it will fail. This is problematic when for example, there is an enum of states, and then more states get added in future versions of the API. The older Swagger clients will break instead of just ignoring the newer state.
A workaround is of course to use Strings or Ints instead of Enum, but this is Swift and we like our code strongly-typed...enums are much better for the developer experience.
Ideally, we could set a default value for the enum (typically "unknown" or similar) and then this value would be used as a fallback when the enum wasn't found in the list.
openapi-generator version
3.3.3
Suggest a fix/enhancement
A proposal for a change could be something like this: Where the "unknown" value could be annotated as the default for the enum in the swagger specification.
public enum PhoneType: String {
case iPhone7
case iPhone8
case iPhoneX
case iPhoneXS
case iPhoneXR
case unknown
}
extension PhoneType: Codable {
public init(from decoder: Decoder) throws {
self = try PhoneType(rawValue: decoder.singleValueContainer().decode(String.self)) ?? .unknown
}
}
I'm willing to send in a PR for this if anyone else thinks this might be a good idea.