Closed brysontyrrell closed 2 months ago
🤦♂️ And I did figure it out after writing everything out for this issue.
for device in results{
switch device {
case .iOS(let device):
print(device.value1. deviceType!)
case .tvOS(let device):
print(device.value1. deviceType!)
case .watchOS(let device):
print(device.value1. deviceType!)
default:
print("UNKNOWN")
}
}
Now that I understand I can work with the types, but I do wish there was a way for that outermost type to reflect the underlying type.
Right, as you discovered, this results in a Swift enum that you'll need to switch over.
We offer some syntactic sugar on the output type enums to get-an-expected-case-or-throw, but we don't do that for all enums.
Question
I'm posting this as a question as I'm not sure if it is a bug.
In a third-party OpenAPI document I am using there's a fairly complex response schema that matches on a discriminator mapped to others schemas, and those schemas use
allOf
with a ref to another shared schema and their own.These are truncated for brevity, but below are how they've generated their OpenAPI doc and the resulting Swift code.
The base schema:
One of the mapped schemas:
The
MobileDeviceInventory
schema that all three mapped schemas share and is the one that contains the discriminator:The generated Swift code in the same order:
MobileDeviceInventory
is the schema that contains thedeviceType
property used by the outer discriminator.In Xcode when I try to interact with the response from a request the Swift compiler throws errors that the members I'm trying to access don't exist. I can verify that the request response has decoded to the generated struct correctly. If I print out the object I can inspect the structure:
I would expect to be able to reach that property (and others) at
response.value.deviceType
but I instead get a compiler error:The operation returns a
MobileDeviceResponse
, but that response willinit
into another type based on the discriminator, but Swift can't figure out what the possible properties are.How do I work with this response so I can test which device type came back to access the appropriate properties and satisfy the compiler checks?