-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathExample.swift
More file actions
400 lines (353 loc) · 13.9 KB
/
Example.swift
File metadata and controls
400 lines (353 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//
// Example.swift
//
//
// Created by Mathew Polzin on 10/6/19.
//
import OpenAPIKitCore
import Foundation
extension OpenAPI {
/// OpenAPI Spec "Example Object"
///
/// See [OpenAPI Example Object](https://spec.openapis.org/oas/v3.2.0.html#example-object).
public struct Example: HasConditionalWarnings, CodableVendorExtendable, Sendable {
public let summary: String?
public let description: String?
/// Stores the OpenAPI `dataValue`, `serializedValue`, `externalValue`,
/// and `value` fields.
public let value: OpenAPI.Example.Value?
/// Dictionary of vendor extensions.
///
/// These should be of the form:
/// `[ "x-extensionKey": <anything>]`
/// where the values are anything codable.
public var vendorExtensions: [String: AnyCodable]
public let conditionalWarnings: [(any Condition, OpenAPI.Warning)]
public var dataValue: AnyCodable? { value?.dataValue }
public var serializedValue: String? { value?.serializedValue }
public var externalValue: URL? { value?.externalValue }
public var legacyValue: AnyCodable? { value?.legacyValue }
public var dataOrLegacyValue: AnyCodable? { value?.value }
@available(*, deprecated, message: "This initializer populates the deprecated 'value' field, use init(summary:description:dataValue:serializedValue:vendorExtensions:) or init(summary:description:dataValue:externalValue:vendorExtensions:) instead.")
public init(
summary: String? = nil,
description: String? = nil,
value: Either<URL, AnyCodable>?,
vendorExtensions: [String: AnyCodable] = [:]
) {
self.summary = summary
self.description = description
switch value {
case .a(let url): self.value = .value(data: nil, serialized: .b(url))
case .b(let value): self.value = .legacy(value)
case nil: self.value = nil
}
self.vendorExtensions = vendorExtensions
self.conditionalWarnings = self.value?.conditionalWarnings ?? []
}
public init(
summary: String? = nil,
description: String? = nil,
legacyValue: Either<URL, AnyCodable>?,
vendorExtensions: [String: AnyCodable] = [:]
) {
self.summary = summary
self.description = description
switch legacyValue {
case .a(let url): self.value = .value(data: nil, serialized: .b(url))
case .b(let value): self.value = .legacy(value)
case nil: self.value = nil
}
self.vendorExtensions = vendorExtensions
self.conditionalWarnings = self.value?.conditionalWarnings ?? []
}
public init(
summary: String? = nil,
description: String? = nil,
value: Value?,
vendorExtensions: [String: AnyCodable] = [:]
) {
self.summary = summary
self.description = description
self.value = value
self.vendorExtensions = vendorExtensions
self.conditionalWarnings = self.value?.conditionalWarnings ?? []
}
public init(
summary: String? = nil,
description: String? = nil,
dataValue: AnyCodable? = nil,
serializedValue: String? = nil,
vendorExtensions: [String: AnyCodable] = [:]
) {
self.summary = summary
self.description = description
if dataValue != nil || serializedValue != nil {
self.value = .value(data: dataValue, serialized: serializedValue.map(Either.a))
} else {
self.value = nil
}
self.vendorExtensions = vendorExtensions
self.conditionalWarnings = self.value?.conditionalWarnings ?? []
}
public init(
summary: String? = nil,
description: String? = nil,
dataValue: AnyCodable? = nil,
externalValue: URL?,
vendorExtensions: [String: AnyCodable] = [:]
) {
self.summary = summary
self.description = description
if dataValue != nil || externalValue != nil {
self.value = .value(data: dataValue, serialized: externalValue.map(Either.b))
} else {
self.value = nil
}
self.vendorExtensions = vendorExtensions
self.conditionalWarnings = self.value?.conditionalWarnings ?? []
}
}
}
extension OpenAPI.Example: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.summary == rhs.summary
&& lhs.description == rhs.description
&& lhs.value == rhs.value
&& lhs.vendorExtensions == rhs.vendorExtensions
}
}
extension OpenAPI.Example {
public typealias Map = OrderedDictionary<String, Either<OpenAPI.Reference<OpenAPI.Example>, OpenAPI.Example>>
}
extension OpenAPI.Example.Value {
fileprivate var conditionalWarnings: [(any Condition, OpenAPI.Warning)] {
[
OASWarnings.Doc.nonNilVersionWarning(objectName: "Example", fieldName: "dataValue", value: dataValue, minimumVersion: .v3_2_0),
OASWarnings.Doc.nonNilVersionWarning(objectName: "Example", fieldName: "serializedValue", value: serializedValue, minimumVersion: .v3_2_0)
].compactMap { $0 }
}
}
// MARK: - Either Convenience
extension Either where A == OpenAPI.Reference<OpenAPI.Example>, B == OpenAPI.Example {
@available(*, deprecated, message: "This function populates the deprecated 'value' field, use .value(summary:description:dataValue:serializedValue:vendorExtensions:) or .value(summary:description:dataValue:externalValue:vendorExtensions:) instead.")
public static func example(
summary: String? = nil,
description: String? = nil,
value: Either<URL, AnyCodable>?,
vendorExtensions: [String: AnyCodable] = [:]
) -> Self {
return .b(
.init(
summary: summary,
description: description,
legacyValue: value,
vendorExtensions: vendorExtensions
)
)
}
public static func example(
summary: String? = nil,
description: String? = nil,
dataValue: AnyCodable? = nil,
serializedValue: String? = nil,
vendorExtensions: [String: AnyCodable] = [:]
) -> Self {
return .b(
.init(
summary: summary,
description: description,
dataValue: dataValue,
serializedValue: serializedValue,
vendorExtensions: vendorExtensions
)
)
}
public static func example(
summary: String? = nil,
description: String? = nil,
dataValue: AnyCodable? = nil,
externalValue: URL?,
vendorExtensions: [String: AnyCodable] = [:]
) -> Self {
return .b(
.init(
summary: summary,
description: description,
dataValue: dataValue,
externalValue: externalValue,
vendorExtensions: vendorExtensions
)
)
}
}
// MARK: - Describable & Summarizable
extension OpenAPI.Example : OpenAPISummarizable {
public func overriddenNonNil(summary: String?) -> OpenAPI.Example {
guard let summary = summary else { return self }
return OpenAPI.Example(
summary: summary,
description: description,
value: value,
vendorExtensions: vendorExtensions
)
}
public func overriddenNonNil(description: String?) -> OpenAPI.Example {
guard let description = description else { return self }
return OpenAPI.Example(
summary: summary,
description: description,
value: value,
vendorExtensions: vendorExtensions
)
}
}
// MARK: - Codable
extension OpenAPI.Example: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(summary, forKey: .summary)
try container.encodeIfPresent(description, forKey: .description)
switch value {
case .legacy(let value):
try container.encode(value, forKey: .value)
case .value(data: let dataValue, serialized: let serialized):
try container.encodeIfPresent(dataValue, forKey: .dataValue)
switch serialized {
case .a(let serializedValue):
try container.encode(serializedValue, forKey: .serializedValue)
case .b(let externalValue):
try container.encode(externalValue.absoluteURL, forKey: .externalValue)
case nil:
break
}
case nil:
break;
}
if VendorExtensionsConfiguration.isEnabled(for: encoder) {
try encodeExtensions(to: &container)
}
}
}
extension OpenAPI.Example: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
if let value = try container.decodeIfPresent(AnyCodable.self, forKey: .value) {
guard !(container.contains(.externalValue) || container.contains(.dataValue) || container.contains(.serializedValue)) else {
throw GenericError(
subjectName: "example value",
details: "Found both `value` and one of `externalValue`, `dataValue`, or `serializedValue` keys in an Example. `value` cannot be used with these other keys.",
codingPath: container.codingPath
)
}
self.value = .legacy(value)
} else {
let dataValue = try container.decodeIfPresent(AnyCodable.self, forKey: .dataValue)
if let externalValue = try container.decodeURLAsStringIfPresent(forKey: .externalValue) {
guard !(container.contains(.serializedValue)) else {
throw GenericError(
subjectName: "example value",
details: "Found both `externalValue` and `serializedValue` keys in an Example. These fields are mutually exclusive.",
codingPath: container.codingPath
)
}
self.value = .value(data: dataValue, serialized: .b(externalValue))
} else if let serializedValue = try container.decodeIfPresent(String.self, forKey: .serializedValue) {
self.value = .value(data: dataValue, serialized: .a(serializedValue))
} else {
self.value = dataValue.map { .value(data: $0, serialized: nil) }
}
}
summary = try container.decodeIfPresent(String.self, forKey: .summary)
description = try container.decodeIfPresent(String.self, forKey: .description)
vendorExtensions = try Self.extensions(from: decoder)
conditionalWarnings = self.value?.conditionalWarnings ?? []
}
}
extension OpenAPI.Example {
internal enum CodingKeys: ExtendableCodingKey {
case summary
case description
case dataValue
case serializedValue
case value
case externalValue
case extended(String)
static var allBuiltinKeys: [CodingKeys] {
return [
.summary,
.description,
.dataValue,
.serializedValue,
.value,
.externalValue
]
}
static func extendedKey(for value: String) -> CodingKeys {
return .extended(value)
}
init?(stringValue: String) {
switch stringValue {
case "summary":
self = .summary
case "description":
self = .description
case "dataValue":
self = .dataValue
case "serializedValue":
self = .serializedValue
case "value":
self = .value
case "externalValue":
self = .externalValue
default:
self = .extendedKey(for: stringValue)
}
}
var stringValue: String {
switch self {
case .summary:
return "summary"
case .description:
return "description"
case .dataValue:
return "dataValue"
case .serializedValue:
return "serializedValue"
case .value:
return "value"
case .externalValue:
return "externalValue"
case .extended(let key):
return key
}
}
}
}
// MARK: - LocallyDereferenceable
extension OpenAPI.Example: LocallyDereferenceable {
/// Examples do not contain any references but for convenience
/// they can be "dereferenced" to themselves.
public func _dereferenced(
in components: OpenAPI.Components,
following references: Set<AnyHashable>,
dereferencedFromComponentNamed name: String?
) throws -> OpenAPI.Example{
var vendorExtensions = self.vendorExtensions
if let name {
vendorExtensions[OpenAPI.Components.componentNameExtension] = .init(name)
}
return .init(
summary: self.summary,
description: self.description,
value: self.value,
vendorExtensions: vendorExtensions
)
}
}
extension OpenAPI.Example: ExternallyDereferenceable {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components, [Loader.Message]) {
return (self, .init(), [])
}
}
extension OpenAPI.Example: Validatable {}