Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #1529
Closed
Open
Issue created Nov 22, 2018 by Administrator@rootContributor

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

Assignee
Assign to
Time tracking