yonaskolb / SwagGen

OpenAPI/Swagger 3.0 Parser and Swift code generator
MIT License
625 stars 147 forks source link

`Unknown` case for enums in models #135

Closed Kastet closed 5 years ago

Kastet commented 5 years ago

Context

As API payloads evolve they need to be backwards compatible with the older versions of mobile clients. Generally, adding new fields and removing optional fields is safe. Changes in field names and types aren't safe but not often required. What does happen often is new cases get added to enum and oneOf types. For example:

struct PaymentMethod {
    enum Type {
        case creditCard
    }

    let type: Type
}

If a new type payPal needs to be added without braking the existing clients I'd have to create a new version of the endpoint.

Suggestion

Add an option to SwagGen to generate an additional unknown case to enum type properties in models.

struct PaymentMethod {
    enum Type {
        case creditCard
        case unknown
    }

    let type: Type
}
yonaskolb commented 5 years ago

Yeah that’s an interesting addition. Another way would be to have an option that makes all enums like this optional

Kastet commented 5 years ago

True. Though an unknown case seems to be more explicit and probably harder to misinterpret.

Kastet commented 5 years ago

I can add this and send a PR if you are happy with the suggestion

yonaskolb commented 5 years ago

Yeah, go for it!