Skip to content

Commit 66b6bf2

Browse files
committed
support refernces in Content.Map values
1 parent 1b1a40a commit 66b6bf2

22 files changed

+254
-191
lines changed

Sources/OpenAPIKit/Content/Content.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,15 @@ extension OpenAPI.Content {
319319
}
320320

321321
extension OpenAPI.Content {
322-
public typealias Map = OrderedDictionary<OpenAPI.ContentType, OpenAPI.Content>
322+
public typealias Map = OrderedDictionary<OpenAPI.ContentType, Either<OpenAPI.Reference<OpenAPI.Content>, OpenAPI.Content>>
323+
}
324+
325+
extension OpenAPI.Content.Map {
326+
/// Construct an OpenAPI.Content.Map for which none of the values are
327+
/// references (all values are OpenAPI.Content).
328+
public static func direct(_ map: OrderedDictionary<OpenAPI.ContentType, OpenAPI.Content>) -> OpenAPI.Content.Map {
329+
map.mapValues { .b($0) }
330+
}
323331
}
324332

325333
extension OpenAPI.Content {

Sources/OpenAPIKit/Content/DereferencedContent.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public struct DereferencedContent: Equatable {
3737
internal init(
3838
_ content: OpenAPI.Content,
3939
resolvingIn components: OpenAPI.Components,
40-
following references: Set<AnyHashable>
40+
following references: Set<AnyHashable>,
41+
dereferencedFromComponentNamed name: String?
4142
) throws {
4243
self.schema = try content.schema?._dereferenced(in: components, following: references, dereferencedFromComponentNamed: nil)
4344
self.itemSchema = try content.itemSchema?._dereferenced(in: components, following: references, dereferencedFromComponentNamed: nil)
@@ -67,6 +68,11 @@ public struct DereferencedContent: Equatable {
6768
self.encoding = nil
6869
}
6970

71+
var content = content
72+
if let name {
73+
content.vendorExtensions[OpenAPI.Components.componentNameExtension] = .init(name)
74+
}
75+
7076
self.underlyingContent = content
7177
}
7278

@@ -85,7 +91,7 @@ extension OpenAPI.Content: LocallyDereferenceable {
8591
following references: Set<AnyHashable>,
8692
dereferencedFromComponentNamed name: String?
8793
) throws -> DereferencedContent {
88-
return try DereferencedContent(self, resolvingIn: components, following: references)
94+
return try DereferencedContent(self, resolvingIn: components, following: references, dereferencedFromComponentNamed: name)
8995
}
9096
}
9197

Sources/OpenAPIKit/Either/Either+Convenience.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ extension Either where B == OpenAPI.Link {
103103
public var linkValue: B? { b }
104104
}
105105

106+
extension Either where B == OpenAPI.Content {
107+
/// Retrieve the content if that is what this property contains.
108+
public var contentValue: B? { b }
109+
}
110+
106111
extension Either where B == OpenAPI.Content.Map {
107112
/// Retrieve the content map if that is what this property contains.
108113
public var contentValue: B? { b }

Sources/OpenAPIKit/Header/DereferencedHeader.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ public struct DereferencedHeader: Equatable {
4747
case .b(let contentMap):
4848
self.schemaOrContent = .b(
4949
try contentMap.mapValues {
50-
try DereferencedContent(
51-
$0,
52-
resolvingIn: components,
53-
following: references
50+
try $0._dereferenced(
51+
in: components,
52+
following: references,
53+
dereferencedFromComponentNamed: nil
5454
)
5555
}
5656
)

Sources/OpenAPIKit/Parameter/DereferencedParameter.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public struct DereferencedParameter: Equatable {
4949
case .b(let contentMap):
5050
self.schemaOrContent = .b(
5151
try contentMap.mapValues {
52-
try DereferencedContent(
53-
$0,
54-
resolvingIn: components,
55-
following: references
52+
try $0._dereferenced(
53+
in: components,
54+
following: references,
55+
dereferencedFromComponentNamed: nil
5656
)
5757
}
5858
)

Sources/OpenAPIKitCompat/Compat30To31.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ extension Either where A == OpenAPIKit30.OpenAPI.Parameter.SchemaContext, B == O
116116
case .a(let context):
117117
.a(context.to31())
118118
case .b(let contentMap):
119-
.b(contentMap.mapValues { $0.to31() })
119+
.b(contentMap.mapValues { .b($0.to31()) })
120120
}
121121
}
122122
}
@@ -328,7 +328,7 @@ extension OpenAPIKit30.OpenAPI.Response: To31 {
328328
OpenAPIKit.OpenAPI.Response(
329329
description: description,
330330
headers: headers?.mapValues(eitherRefTo31),
331-
content: content.mapValues { $0.to31() },
331+
content: content.mapValues { .b($0.to31()) },
332332
links: links.mapValues(eitherRefTo31),
333333
vendorExtensions: vendorExtensions
334334
)
@@ -339,7 +339,7 @@ extension OpenAPIKit30.OpenAPI.Request: To31 {
339339
fileprivate func to31() -> OpenAPIKit.OpenAPI.Request {
340340
OpenAPIKit.OpenAPI.Request(
341341
description: description,
342-
content: content.mapValues { $0.to31() },
342+
content: content.mapValues { .b($0.to31()) },
343343
required: `required`,
344344
vendorExtensions: vendorExtensions
345345
)

Tests/OpenAPIKitCompatTests/DocumentConversionTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,10 @@ fileprivate func assertEqualNewToOld(_ newRequest: OpenAPIKit.OpenAPI.Request, _
10861086
fileprivate func assertEqualNewToOld(_ newContentMap: OpenAPIKit.OpenAPI.Content.Map, _ oldContentMap: OpenAPIKit30.OpenAPI.Content.Map) throws {
10871087
for ((newCt, newContent), (oldCt, oldContent)) in zip(newContentMap, oldContentMap) {
10881088
XCTAssertEqual(newCt, oldCt)
1089+
guard case let .b(newContent) = newContent else {
1090+
XCTFail("Expected new content not to be a reference but found a reference: \(newContent)")
1091+
return
1092+
}
10891093
switch (newContent.schema?.value, oldContent.schema) {
10901094
case (.reference(let ref1, _), .a(let ref2)):
10911095
XCTAssertEqual(ref1.absoluteString, ref2.absoluteString)

Tests/OpenAPIKitTests/Content/ContentTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ final class ContentTests: XCTestCase {
163163
}
164164

165165
func test_contentMap() {
166-
let _: OpenAPI.Content.Map = [
166+
let _: OpenAPI.Content.Map = .direct([
167167
.bmp: .init(schema: .init(.string(contentEncoding: .binary))),
168168
.css: .init(schema: .init(.string)),
169169
.csv: .init(schema: .init(.string)),
@@ -197,7 +197,7 @@ final class ContentTests: XCTestCase {
197197
.anyVideo: .init(schema: .string(contentEncoding: .binary)),
198198

199199
.any: .init(schema: .string(contentEncoding: .binary))
200-
]
200+
])
201201
}
202202
}
203203

Tests/OpenAPIKitTests/Document/DocumentTests.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ final class DocumentTests: XCTestCase {
258258
"/hello/world": .init(
259259
servers: [],
260260
get: .init(
261-
responses: [.default: .response(description: "test", content: [.json: .init(schema: .string)])],
261+
responses: [.default: .response(description: "test", content: [.json: .content(.init(schema: .string))])],
262262
servers: []
263263
)
264264
)
@@ -280,7 +280,7 @@ final class DocumentTests: XCTestCase {
280280
"/hello/world": .init(
281281
servers: [],
282282
get: .init(
283-
responses: [.default: .response(description: "test", content: [.json: .init(schema: .string)])],
283+
responses: [.default: .response(description: "test", content: [.json: .content(.init(schema: .string))])],
284284
servers: []
285285
)
286286
)
@@ -302,7 +302,7 @@ final class DocumentTests: XCTestCase {
302302
"/hello/world": .init(
303303
servers: [s1, s2],
304304
get: .init(
305-
responses: [.default: .response(description: "test", content: [.json: .init(schema: .string)])],
305+
responses: [.default: .response(description: "test", content: [.json: .content(.init(schema: .string))])],
306306
servers: []
307307
)
308308
)
@@ -324,7 +324,7 @@ final class DocumentTests: XCTestCase {
324324
"/hello/world": .init(
325325
servers: [],
326326
get: .init(
327-
responses: [.default: .response(description: "test", content: [.json: .init(schema: .string)])],
327+
responses: [.default: .response(description: "test", content: [.json: .content(.init(schema: .string))])],
328328
servers: [s1, s2]
329329
)
330330
)
@@ -346,7 +346,7 @@ final class DocumentTests: XCTestCase {
346346
"/hello/world": .init(
347347
servers: [s1, s2],
348348
get: .init(
349-
responses: [.default: .response(description: "test", content: [.json: .init(schema: .string)])],
349+
responses: [.default: .response(description: "test", content: [.json: .content(.init(schema: .string))])],
350350
servers: [s1, s2]
351351
)
352352
)
@@ -369,7 +369,7 @@ final class DocumentTests: XCTestCase {
369369
"/hello/world": .init(
370370
servers: [s2],
371371
get: .init(
372-
responses: [.default: .response(description: "test", content: [.json: .init(schema: .string)])],
372+
responses: [.default: .response(description: "test", content: [.json: .content(.init(schema: .string))])],
373373
servers: [s3]
374374
)
375375
)
@@ -393,7 +393,7 @@ final class DocumentTests: XCTestCase {
393393
"/hello/world": .init(
394394
servers: [s2, s4],
395395
get: .init(
396-
responses: [.default: .response(description: "test", content: [.json: .init(schema: .string)])],
396+
responses: [.default: .response(description: "test", content: [.json: .content(.init(schema: .string))])],
397397
servers: [s3]
398398
)
399399
)
@@ -417,7 +417,7 @@ final class DocumentTests: XCTestCase {
417417
"/hello/world": .init(
418418
servers: [s2, s4],
419419
get: .init(
420-
responses: [.default: .response(description: "test", content: [.json: .init(schema: .string)])],
420+
responses: [.default: .response(description: "test", content: [.json: .content(.init(schema: .string))])],
421421
servers: [s3]
422422
)
423423
)
@@ -438,7 +438,7 @@ final class DocumentTests: XCTestCase {
438438
paths: [
439439
"/hello/world": .init(
440440
get: .init(
441-
responses: [.default: .response(description: "test", content: [.json: .init(schema: .string)])]
441+
responses: [.default: .response(description: "test", content: [.json: .content(.init(schema: .string))])]
442442
)
443443
)
444444
],

0 commit comments

Comments
 (0)