Open
Description
Question
Hello,
I'm writing a client with an openapi.yml that frequently uses nullable ref properties, as below:
// Technique 1
my_field:
nullable: true
allof:
- $ref: '#/components/schemas/MyField'
An alternative technique, AFAIK, is:
// Technique 2
my_field:
oneOf:
- $ref: '#/components/schemas/MyField'
- type: 'null'
With both techniques, the generated code is not quite easy to deal with, both when decoding responses or encoding requests. The generated payload is a struct that has a non-semantic value1
property, or an enum that does not expose any accessor for extracting its non-null value.
// Technique 1
public struct my_fieldPayload: Codable, Hashable, Sendable {
public var value1: Components.Schemas.MyField
public init(value1: Components.Schemas.MyField) {
self.value1 = value1
}
}
// Technique 2
@frozen public enum my_fieldPayload: Codable, Hashable, Sendable {
case MyField(Components.Schemas.MyField)
case case2(OpenAPIRuntime.OpenAPIValueContainer)
}
We're far from the expected Swift optionals. The struct generated by the technique 1 is surprisingly shallow.
Did any member in the community meet the same problem? Is there any workaround? Or a better YAML technique for specifying a nullable ref, while preserving good Swift ergonomics? Maybe I'm just holding it wrong?