-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathExternalDereferencingDocumentTests.swift
More file actions
293 lines (277 loc) · 11.2 KB
/
ExternalDereferencingDocumentTests.swift
File metadata and controls
293 lines (277 loc) · 11.2 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
//
// ExternalDereferencingDocumentTests.swift
//
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif
import Yams
import OpenAPIKit30
import XCTest
final class ExternalDereferencingDocumentTests: XCTestCase {
// temporarily test with an example of the new interface
func test_example() async throws {
/// An example of implementing a loader context for loading external references
/// into an OpenAPI document.
struct ExampleLoader: ExternalLoader {
typealias Message = String
static func load<T>(_ url: URL) async throws -> (T, [Message]) where T : Decodable {
// load data from file, perhaps. we will just mock that up for the test:
let data = try await mockData(componentKey(type: T.self, at: url))
// We use the YAML decoder purely for order-stability.
let decoded = try YAMLDecoder().decode(T.self, from: data)
let finished: T
// while unnecessary, a loader may likely want to attatch some extra info
// to keep track of where a reference was loaded from. This test makes sure
// the following strategy of using vendor extensions works.
if var extendable = decoded as? VendorExtendable {
extendable.vendorExtensions["x-source-url"] = AnyCodable(url)
finished = extendable as! T
} else {
finished = decoded
}
return (finished, [url.absoluteString])
}
static func componentKey<T>(type: T.Type, at url: URL) throws -> OpenAPIKit30.OpenAPI.ComponentKey {
// do anything you want here to determine what key the new component should be stored at.
// for the example, we will just transform the URL into a valid components key:
let urlString = url.pathComponents.dropFirst()
.joined(separator: "_")
.replacingOccurrences(of: ".", with: "_")
return try .forceInit(rawValue: urlString)
}
/// Mock up some data, just for the example.
static func mockData(_ key: OpenAPIKit30.OpenAPI.ComponentKey) async throws -> Data {
return try XCTUnwrap(files[key.rawValue])
}
static let files: [String: Data] = [
"params_name_json": """
{
"name": "name",
"description": "a lonely parameter",
"in": "path",
"required": true,
"schema": {
"$ref": "file://./schemas/string_param.json#"
}
}
""",
"schemas_string_param_json": """
{
"oneOf": [
{ "type": "string" },
{ "$ref": "file://./schemas/basic_object.json" }
]
}
""",
"schemas_basic_object_json": """
{
"type": "object"
}
""",
"paths_webhook_json": """
{
"summary": "just a webhook",
"get": {
"requestBody": {
"$ref": "file://./requests/webhook.json"
},
"responses": {
"200": {
"$ref": "file://./responses/webhook.json"
}
}
}
}
""",
"requests_webhook_json": """
{
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"body": {
"$ref": "file://./schemas/string_param.json"
}
}
},
"examples": {
"good": {
"$ref": "file://./examples/good.json"
}
},
"encoding": {
"enc1": {
"headers": {
"head1": {
"$ref": "file://./headers/webhook.json"
}
}
},
"enc2": {
"style": "form"
}
}
}
}
}
""",
"responses_webhook_json": """
{
"description": "webhook response",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"body": {
"type": "string"
},
"length": {
"type": "integer",
"minimum": 0
}
}
}
}
},
"headers": {
"X-Hello": {
"$ref": "file://./headers/webhook2.json"
}
}
}
""",
"headers_webhook_json": """
{
"schema": {
"$ref": "file://./schemas/string_param.json"
}
}
""",
"headers_webhook2_json": """
{
"content": {
"application/json": {
"schema": {
"$ref": "file://./schemas/string_param.json"
}
}
}
}
""",
"examples_good_json": """
{
"value": "{\\"body\\": \\"request me\\"}"
}
""",
"callbacks_one_json": """
{
"https://callback.site.com/callback": {
"summary": "just a callback"
}
}
""",
"paths_callback_json": """
{
"summary": "just a callback",
"get": {
"responses": {
"200": {
"description": "callback response",
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
},
"links": {
"link1": {
"$ref": "file://./links/first.json"
}
}
}
}
}
}
""",
"links_first_json": """
{
"operationId": "helloOp"
}
"""
].mapValues { $0.data(using: .utf8)! }
}
let document = OpenAPI.Document(
info: .init(title: "test document", version: "1.0.0"),
servers: [],
paths: [
"/hello/{name}": .init(
parameters: [
.reference(.external(URL(string: "file://./params/name.json")!))
],
get: .init(
operationId: "helloOp",
responses: [:],
callbacks: [
"callback1": .reference(.external(URL(string: "file://./callbacks/one.json")!))
]
)
),
"/goodbye/{name}": .init(
parameters: [
.reference(.external(URL(string: "file://./params/name.json")!))
]
),
"/webhook": .reference(.external(URL(string: "file://./paths/webhook.json")!)),
"/callback": .reference(.external(URL(string: "file://./paths/callback.json")!))
],
components: .init(
schemas: [
"name_param": .reference(.external(URL(string: "file://./schemas/string_param.json")!))
],
// just to show, no parameters defined within document components :
parameters: [:]
)
)
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
var docCopy1 = document
try await docCopy1.externallyDereference(with: ExampleLoader.self)
try await docCopy1.externallyDereference(with: ExampleLoader.self)
try await docCopy1.externallyDereference(with: ExampleLoader.self)
try await docCopy1.externallyDereference(with: ExampleLoader.self)
docCopy1.components.sort()
var docCopy2 = document
try await docCopy2.externallyDereference(with: ExampleLoader.self, depth: 4)
docCopy2.components.sort()
var docCopy3 = document
let messages = try await docCopy3.externallyDereference(with: ExampleLoader.self, depth: .full)
docCopy3.components.sort()
XCTAssertEqual(docCopy1, docCopy2)
XCTAssertEqual(docCopy2, docCopy3)
XCTAssertEqual(
messages.sorted(),
["file://./callbacks/one.json",
"file://./examples/good.json",
"file://./headers/webhook.json",
"file://./headers/webhook2.json",
"file://./links/first.json",
"file://./params/name.json",
"file://./params/name.json",
"file://./paths/callback.json",
"file://./paths/webhook.json",
"file://./requests/webhook.json",
"file://./responses/webhook.json",
"file://./schemas/basic_object.json",
"file://./schemas/string_param.json",
"file://./schemas/string_param.json",
"file://./schemas/string_param.json",
"file://./schemas/string_param.json",
"file://./schemas/string_param.json#"]
)
}
}