Skip to content

Support multiple discriminator mappings to share one type #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers 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

Merged
merged 5 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/CreateAPI/Generator/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ struct TypealiasDeclaration: Declaration {
struct Discriminator {
let propertyName: String
let mapping: [String: TypeIdentifier]

func correspondingMappings(for property: Property) -> Array<(key: String, value: TypeIdentifier)> {
mapping.filter { $1 == property.type }.sorted { $0.key < $1.key }
}
}

struct DeclarationMetadata {
Expand Down
2 changes: 1 addition & 1 deletion Sources/CreateAPI/Generator/Generator+Render.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ extension Generator {
}
}
}

// TODO: Refactor
var protocols = decl.protocols
if decl.isForm {
Expand Down
10 changes: 5 additions & 5 deletions Sources/CreateAPI/Generator/Templates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,12 @@ final class Templates {
func initFromDecoderOneOfWithDiscriminator(properties: [Property], discriminator: Discriminator) -> String {
var statements = ""
for property in properties {
if let correspondingMapping = discriminator.mapping.first(where: { $1 == property.type}) {
let correspondingMappings = discriminator.correspondingMappings(for: property)
for mapping in correspondingMappings {
statements += """
case \"\(correspondingMapping.key)\": self = .\(property.name)(try container.decode(\(property.type).self))
case \"\(mapping.key)\": self = .\(property.name)(try container.decode(\(property.type).self))

"""
} else {
continue
}
}

Expand All @@ -344,9 +343,10 @@ final class Templates {
}

func encodeOneOf(properties: [Property]) -> String {
let statements = properties.map {
let statements: [String] = properties.map {
"case .\($0.name)(let value): try container.encode(value)"
}

return """
\(access)func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public struct Container: Codable {

switch (try container.decode(Discriminator.self)).kind {
case "a": self = .a(try container.decode(A.self))
case "d": self = .a(try container.decode(A.self))
case "b": self = .b(try container.decode(B.self))
case "c": self = .c(try container.decode(C.self))

Expand Down
1 change: 1 addition & 0 deletions Tests/CreateAPITests/Specs/discriminator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ components:
a: "#/components/schemas/A"
b: "#/components/schemas/B"
c: "#/components/schemas/C"
d: "#/components/schemas/A"