Fix a third dictionary-as-array bug

This commit is contained in:
Mathew Polzin
2019-01-26 20:04:11 -08:00
parent b45fc73188
commit 75ec4f156e
2 changed files with 42 additions and 10 deletions
@@ -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)
}
}
@@ -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)
}
}
}