mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-03-30 11:18:38 -07:00
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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user