-
Notifications
You must be signed in to change notification settings - Fork 52
Open enum support #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintain 8000 ers 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
Comments
While it is an interesting pattern I struggle to imagine how this would look in Kotlin's type system. In TypeScript it would be a union type ( Designing how this should look in Kotlin would be the first step to make this work. |
In Zalando we make use of What has been a proposal before, is generating fallbacks for enums, which I think would be a better addition to fabrikt. See proposed options: #256 (comment) |
In Swift it generates a public struct OpenEnum: Codable, Hashable, Sendable {
/// - Remark: Generated from `#/components/schemas/OpenEnum/value1`.
@frozen public enum Value1Payload: String, Codable, Hashable, Sendable, CaseIterable {
case foo = "foo"
case bar = "bar"
case baz = "baz"
}
/// - Remark: Generated from `#/components/schemas/OpenEnum/value1`.
public var value1: Components.Schemas.OpenEnum.Value1Payload?
/// - Remark: Generated from `#/components/schemas/OpenEnum/value2`.
public var value2: Swift.String?
} If They also add a check when deserializing the data to make sure one of the two properties is present public struct OpenEnum: Codable, Hashable, Sendable {
// ....
public init(from decoder: any Decoder) throws {
var errors: [any Error] = []
do {
self.value1 = try decoder.decodeFromSingleValueContainer()
} catch {
errors.append(error)
}
do {
self.value2 = try decoder.decodeFromSingleValueContainer()
} catch {
errors.append(error)
}
try Swift.DecodingError.verifyAtLeastOneSchemaIsNotNil(
[
self.value1,
self.value2
],
type: Self.self,
codingPath: decoder.codingPath,
errors: errors
)
}
} |
Agreed, it would be very useful to have this The trick with Do you think it would be complicated to add support for open enums using |
I don't think it will ever get added to the spec. But fabrikt could generate a fallback based on a global CLI parameter. |
Yes we could make it work for fabrikt. But for eg, in our case we are trying to use the same OpenAPI spec to generate:
So our OpenAPI spec needs to use keywords and patterns that are supported by the 3 generators. If fabrikt supports a This is why I feel using |
Hey 👋
I'm using an OpenAPI specification that makes use of
anyOf
to implement the "open enum pattern".This is what I'm doing:
Generated model:
When using kotlinx as serialization library the
OpenEnum
model is not generated and the property using this schema is of typeString
. Does fabrikt (+ kotlinx) have support foranyOf
?This pattern is very interesting to force the client (mobile app for eg) to handle unknown cases. It allows more flexibility for the server to add new cases without breaking the API contract or having to version each new addition to the enum
Example taken from the Swift OpenAPI generator documentation: https://swiftpackageindex.com/apple/swift-openapi-generator/1.7.0/documentation/swift-openapi-generator/useful-openapi-patterns#Open-enums-and-oneOfs
The text was updated successfully, but these errors were encountered: