diff --git a/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes+Codable.swift b/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes+Codable.swift index 91a71cd..3299047 100644 --- a/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes+Codable.swift +++ b/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes+Codable.swift @@ -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() diff --git a/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift b/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift index faad090..83266bd 100644 --- a/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift +++ b/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift @@ -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 {