Discriminator Support#10
Conversation
| if let innerMapping = discriminator.mapping { | ||
|
|
||
| // TODO: Use type name machinery instead | ||
| mapping = innerMapping.mapValues { value in |
There was a problem hiding this comment.
I think this should work:
mapping = innerMapping.mapValues { value in
guard let key = OpenAPI.ComponentKey(rawValue: value) else {
throw GeneratorError("TBD") // This should never happen
}
// This takes care of options.rename.
// It _doesn't_ take options.entities.include / exclude into the account.
// Ideally CreateAPI should throw an error (or warning if --strict is disabled) if
// the type is exlucded (neat-picking).
guard let name = getTypeName(for: key) else {
// TODO:
}
return .userDefined(name: name)
}There was a problem hiding this comment.
guard let key = OpenAPI.ComponentKey(rawValue: value) else {
throw GeneratorError("TBD") // This should never happen
}The rawValue: constructor enforces that the provided value matches this regex: ^[a-zA-Z0-9\.\-_]+$.
At this stage the mapping values are still prefixed with "#/components/schemas/", which does not conform to said regex.
There was a problem hiding this comment.
You are right. I just tested it and yes, the string value should be the component name (without "#/components/schemas/").
String(value.dropFirst("#/components/schemas/".count))That's a nice approach for removing the prefix. I would've usually used replacingOccurrences, but this is much better.
|
I suggest to cleanup the generated code a bit. I think this should be functionally equivalent: switch try container.decode(Discriminator.self) {
case "a": self = .a(try container.decode(A.self))
// ...
} |
|
I fixed the test target in |
6c200ad to
4915b28
Compare
|
Thanks, @PhilipTrauner. Merging it. I'll address the following later today and add the expected test output to the repo: // TODO: Find it automatically
var projectPath = ("~/Developer/CreateAPI/" as NSString).expandingTildeInPathIt's needed for "snapshot" testing. The idea is that when you add a new test, and it can't find the expected files in the repo, it generates them for you to commit. |
Adds initial support for decoding
oneOfentities with a discriminator attribute.Using the existing property type name machinery when constructing the discriminator is still outstanding.
Would really appreciate some help with that, because I struggled to find a clean way to call into one of the name canonicalization functions.
Generated Code
Schema