Fix super shitty bug caused by Apples implementation of Dictionary's conformance to Encodable sometimes encoding the dictionary as an array.

This commit is contained in:
Mathew Polzin
2019-01-26 19:50:20 -08:00
parent b2c81026f4
commit 3b73dc9989
2 changed files with 52 additions and 3 deletions
@@ -276,7 +276,13 @@ extension OpenAPIPathItem.PathProperties.Operation: Encodable {
try container.encode(parameters, forKey: .parameters)
try container.encode(responses, forKey: .responses)
// Hack to work around Dictionary encoding
// itself as an array in this case:
let stringKeyedDict = Dictionary(
responses.map { ($0.key.rawValue, $0.value) },
uniquingKeysWith: { $1 }
)
try container.encode(stringKeyedDict, forKey: .responses)
try container.encode(deprecated, forKey: .deprecated)
}
@@ -346,6 +352,29 @@ extension OpenAPIPathItem.PathProperties: Encodable {
}
}
extension OpenAPIResponse: Encodable {
private enum CodingKeys: String, CodingKey {
case description
case headers
case content
case links
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(description, forKey: .description)
// Hack to work around Dictionary encoding
// itself as an array in this case:
let stringKeyedDict = Dictionary(
content.map { ($0.key.rawValue, $0.value) },
uniquingKeysWith: { $1 }
)
try container.encode(stringKeyedDict, forKey: .content)
}
}
extension OpenAPIPathItem: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
@@ -787,7 +787,7 @@ public enum OpenAPIPathItem: Equatable {
}
}
public struct OpenAPIResponse: Encodable, Equatable {
public struct OpenAPIResponse: Equatable {
public let description: String
// public let headers:
public let content: ContentMap
@@ -801,9 +801,29 @@ public struct OpenAPIResponse: Encodable, Equatable {
public typealias ContentMap = [ContentType: Content]
public enum Code: Equatable, Hashable {
public enum Code: RawRepresentable, Equatable, Hashable {
public typealias RawValue = String
case `default`
case status(code: Int)
public var rawValue: String {
switch self {
case .default:
return "default"
case .status(code: let code):
return String(code)
}
}
public init?(rawValue: String) {
if let val = Int(rawValue) {
self = .status(code: val)
} else {
self = .default
}
}
}
public enum ContentType: String, Encodable, Equatable, Hashable {