From 85d5fef3c856d15c37a069f24d15ded906aed7e3 Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Sun, 27 Jan 2019 13:22:18 -0800 Subject: [PATCH] Fix bug where some references were correctly encoded as objects and others were just encoded as strings --- .../OpenAPI/OpenAPITypes+Codable.swift | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes+Codable.swift b/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes+Codable.swift index bc561a2..f1511b9 100644 --- a/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes+Codable.swift +++ b/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes+Codable.swift @@ -155,7 +155,6 @@ extension JSONNode: Encodable { case oneOf case anyOf case not - case reference = "$ref" } public func encode(to encoder: Encoder) throws { @@ -192,16 +191,20 @@ extension JSONNode: Encodable { try container.encode(node, forKey: .not) case .reference(let reference): - var container = encoder.container(keyedBy: SubschemaCodingKeys.self) + var container = encoder.singleValueContainer() - try container.encode(reference, forKey: .reference) + try container.encode(reference) } } } extension JSONReference: Encodable { + private enum CodingKeys: String, CodingKey { + case ref = "$ref" + } + public func encode(to encoder: Encoder) throws { - var container = encoder.singleValueContainer() + var container = encoder.container(keyedBy: CodingKeys.self) let referenceString: String = { switch self { @@ -212,7 +215,7 @@ extension JSONReference: Encodable { } }() - try container.encode(referenceString) + try container.encode(referenceString, forKey: .ref) } }