From 75ec4f156ea664f5d5eed6625683e82b7cb10dbe Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Sat, 26 Jan 2019 20:04:11 -0800 Subject: [PATCH] Fix a third dictionary-as-array bug --- .../OpenAPI/OpenAPITypes+Codable.swift | 31 +++++++++++++++++++ .../JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift | 21 +++++++------ 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes+Codable.swift b/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes+Codable.swift index 3299047..bc561a2 100644 --- a/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes+Codable.swift +++ b/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes+Codable.swift @@ -388,3 +388,34 @@ extension OpenAPIPathItem: Encodable { } } } + +extension OpenAPISchema: Encodable { + private enum CodingKeys: String, CodingKey { + case openAPIVersion = "openapi" + case info + case servers + case paths + case components + case security + case tags + case externalDocs + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + + try container.encode(openAPIVersion, forKey: .openAPIVersion) + + try container.encode(info, forKey: .info) + + // Hack to work around Dictionary encoding + // itself as an array in this case: + let stringKeyedDict = Dictionary( + paths.map { ($0.key.rawValue, $0.value) }, + uniquingKeysWith: { $1 } + ) + try container.encode(stringKeyedDict, forKey: .paths) + + try container.encode(components, forKey: .components) + } +} diff --git a/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift b/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift index 83266bd..b17dea5 100644 --- a/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift +++ b/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift @@ -878,14 +878,7 @@ public struct OpenAPIComponents: Equatable, Encodable, ReferenceRoot { } /// The root of an OpenAPI 3.0 document. -public struct OpenAPISchema: Encodable { - private enum CodingKeys: String, CodingKey { - case openAPIVersion = "openapi" - case info - case paths - case components - } - +public struct OpenAPISchema { public let openAPIVersion: Version public let info: Info // public let servers: @@ -928,17 +921,25 @@ public struct OpenAPISchema: Encodable { } } - public struct PathComponents: Encodable, Equatable, Hashable { + public struct PathComponents: RawRepresentable, Encodable, Equatable, Hashable { public let components: [String] public init(_ components: [String]) { self.components = components } + public init?(rawValue: String) { + components = rawValue.split(separator: "/").map(String.init) + } + + public var rawValue: String { + return components.joined(separator: "/") + } + public func encode(to encoder: Encoder) throws { var container = encoder.singleValueContainer() - try container.encode(components.joined(separator: "/")) + try container.encode(rawValue) } } }