|
| 1 | +// |
| 2 | +// Header.swift |
| 3 | +// OpenAPIKit |
| 4 | +// |
| 5 | +// Created by Mathew Polzin on 8/25/19. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import Poly |
| 10 | + |
| 11 | +extension OpenAPI { |
| 12 | + public struct Header: Equatable { |
| 13 | + public let description: String? |
| 14 | + public let required: Bool |
| 15 | + public let deprecated: Bool // default is false |
| 16 | + public let schemaOrContent: Either<SchemaProperty, OpenAPI.Content.Map> |
| 17 | + |
| 18 | + public typealias Map = [String: Either<Header, JSONReference<OpenAPI.Components, Header>>] |
| 19 | + |
| 20 | + public typealias SchemaProperty = Either<JSONReference<OpenAPI.Components, JSONSchema>, JSONSchema> |
| 21 | + |
| 22 | + public init(schemaOrContent: Either<SchemaProperty, OpenAPI.Content.Map>, |
| 23 | + description: String? = nil, |
| 24 | + required: Bool = false, |
| 25 | + deprecated: Bool = false) { |
| 26 | + self.schemaOrContent = schemaOrContent |
| 27 | + self.description = description |
| 28 | + self.required = required |
| 29 | + self.deprecated = deprecated |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// MARK: - Codable |
| 35 | + |
| 36 | +extension OpenAPI.Header { |
| 37 | + private enum CodingKeys: String, CodingKey { |
| 38 | + case description |
| 39 | + case required |
| 40 | + case deprecated |
| 41 | + |
| 42 | + // the following are alternatives |
| 43 | + case content |
| 44 | + case schema |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +extension OpenAPI.Header: Encodable { |
| 49 | + public func encode(to encoder: Encoder) throws { |
| 50 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 51 | + |
| 52 | + if required { |
| 53 | + try container.encode(required, forKey: .required) |
| 54 | + } |
| 55 | + |
| 56 | + switch schemaOrContent { |
| 57 | + case .a(let schema): |
| 58 | + try container.encode(schema, forKey: .schema) |
| 59 | + case .b(let contentMap): |
| 60 | + // Hack to work around Dictionary encoding |
| 61 | + // itself as an array in this case: |
| 62 | + let stringKeyedDict = Dictionary( |
| 63 | + contentMap.map { ($0.key.rawValue, $0.value) }, |
| 64 | + uniquingKeysWith: { $1 } |
| 65 | + ) |
| 66 | + try container.encode(stringKeyedDict, forKey: .content) |
| 67 | + } |
| 68 | + |
| 69 | + if description != nil { |
| 70 | + try container.encode(description, forKey: .description) |
| 71 | + } |
| 72 | + |
| 73 | + if deprecated { |
| 74 | + try container.encode(deprecated, forKey: .deprecated) |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +extension OpenAPI.Header: Decodable { |
| 80 | + public init(from decoder: Decoder) throws { |
| 81 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 82 | + |
| 83 | + required = try container.decodeIfPresent(Bool.self, forKey: .required) ?? false |
| 84 | + |
| 85 | + // hacky workaround for Dictionary decoding bug |
| 86 | + let maybeContentDict = try container.decodeIfPresent([String: OpenAPI.Content].self, forKey: .content) |
| 87 | + let maybeContent = maybeContentDict.map { contentDict in |
| 88 | + Dictionary(contentDict.compactMap { contentTypeString, content in |
| 89 | + OpenAPI.ContentType(rawValue: contentTypeString).map { ($0, content) } }, |
| 90 | + uniquingKeysWith: { $1 }) |
| 91 | + } |
| 92 | + |
| 93 | + let maybeSchema = try container.decodeIfPresent(SchemaProperty.self, forKey: .schema) |
| 94 | + |
| 95 | + switch (maybeContent, maybeSchema) { |
| 96 | + case (let content?, _): |
| 97 | + schemaOrContent = .init(content) |
| 98 | + case (_, let schema?): |
| 99 | + schemaOrContent = .init(schema) |
| 100 | + default: |
| 101 | + throw OpenAPI.DecodingError.unsatisfied(requirement: "A single path parameter must specify one but not both 'content' and 'schema'.", codingPath: decoder.codingPath) |
| 102 | + } |
| 103 | + |
| 104 | + description = try container.decodeIfPresent(String.self, forKey: .description) |
| 105 | + |
| 106 | + deprecated = try container.decodeIfPresent(Bool.self, forKey: .deprecated) ?? false |
| 107 | + } |
| 108 | +} |
0 commit comments