As you can see, the switch on the discriminator value contains both the explicit mappings catdog, woof values but also what seems to be the implicit mappings by the name of the reference Cat and Dog.
Since I supply explicit mappings, I would expect only the explicit ones to be present:
let discriminator: String = try container.decode("type")
switch discriminator {
case "cat":
self = .cat(try Cat(from: decoder))
case "dog":
self = .dog(try Dog(from: decoder))
case "woof":
self = .dog(try Dog(from: decoder))
default:
throw DecodingError.dataCorrupted(DecodingError.Context.init(codingPath: decoder.codingPath, debugDescription: "Couldn't find type to decode with discriminator \(discriminator)"))
}
I think that if we detect an explicit mapping for a type, we should remove the implicit one in CodeFormatter.swift#L216
So just adding mapping.removeValue(forKey: reference.name) here would work:
if let discriminatorMapping = groupSchema.discriminator?.mapping {
for (key, value) in discriminatorMapping {
let reference = Reference<Schema>(value)
// remove the implicit mapping for that reference
mapping.removeValue(forKey: reference.name)
// add the explicit one
mapping[key] = getReferenceContext(reference)
}
}
@yonaskolb any thoughts? If you agree with the approach, I can open a PR.
I used the specs.yaml from the repo but for reference I put the relevant part here:
Hi,
I noticed that when I set an explicit mappings to a
discriminator
, the default mapping remain in the generated code when decoding.Given this spec
The generated
SingeAnimal.swift
containshttps://github.com/yonaskolb/SwagGen/blob/2b3140b821129190aab28a5cf9b1e5e2151dbc9b/Specs/TestSpec/generated/Swift/Sources/Models/SingleAnimal.swift#L16-L25
As you can see, the switch on the
discriminator
value contains both the explicit mappingscat
dog
,woof
values but also what seems to be the implicit mappings by the name of the referenceCat
andDog
.Since I supply explicit mappings, I would expect only the explicit ones to be present:
I think that if we detect an explicit mapping for a type, we should remove the implicit one in CodeFormatter.swift#L216
So just adding
mapping.removeValue(forKey: reference.name)
here would work:@yonaskolb any thoughts? If you agree with the approach, I can open a PR.
I used the specs.yaml from the repo but for reference I put the relevant part here: