Skip to content

Commit e599b45

Browse files
committed
pipe messages through all external dereferencing
1 parent a49d111 commit e599b45

24 files changed

+190
-129
lines changed

Sources/OpenAPIKit/Components Object/Components.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,10 @@ extension OpenAPI.Components {
320320
}
321321

322322
extension OpenAPI.Components {
323-
internal mutating func externallyDereference<Loader: ExternalLoader>(with loader: Loader.Type, depth: ExternalDereferenceDepth = .iterations(1)) async throws {
323+
internal mutating func externallyDereference<Loader: ExternalLoader>(with loader: Loader.Type, depth: ExternalDereferenceDepth = .iterations(1), context: [Loader.Message] = []) async throws -> [Loader.Message] {
324324
if case let .iterations(number) = depth,
325325
number <= 0 {
326-
return
326+
return context
327327
}
328328

329329
let oldSchemas = schemas
@@ -336,15 +336,15 @@ extension OpenAPI.Components {
336336
let oldCallbacks = callbacks
337337
let oldPathItems = pathItems
338338

339-
async let (newSchemas, c1) = oldSchemas.externallyDereferenced(with: loader)
340-
async let (newResponses, c2) = oldResponses.externallyDereferenced(with: loader)
341-
async let (newParameters, c3) = oldParameters.externallyDereferenced(with: loader)
342-
async let (newExamples, c4) = oldExamples.externallyDereferenced(with: loader)
343-
async let (newRequestBodies, c5) = oldRequestBodies.externallyDereferenced(with: loader)
344-
async let (newHeaders, c6) = oldHeaders.externallyDereferenced(with: loader)
345-
async let (newSecuritySchemes, c7) = oldSecuritySchemes.externallyDereferenced(with: loader)
346-
async let (newCallbacks, c8) = oldCallbacks.externallyDereferenced(with: loader)
347-
async let (newPathItems, c9) = oldPathItems.externallyDereferenced(with: loader)
339+
async let (newSchemas, c1, m1) = oldSchemas.externallyDereferenced(with: loader)
340+
async let (newResponses, c2, m2) = oldResponses.externallyDereferenced(with: loader)
341+
async let (newParameters, c3, m3) = oldParameters.externallyDereferenced(with: loader)
342+
async let (newExamples, c4, m4) = oldExamples.externallyDereferenced(with: loader)
343+
async let (newRequestBodies, c5, m5) = oldRequestBodies.externallyDereferenced(with: loader)
344+
async let (newHeaders, c6, m6) = oldHeaders.externallyDereferenced(with: loader)
345+
async let (newSecuritySchemes, c7, m7) = oldSecuritySchemes.externallyDereferenced(with: loader)
346+
async let (newCallbacks, c8, m8) = oldCallbacks.externallyDereferenced(with: loader)
347+
async let (newPathItems, c9, m9) = oldPathItems.externallyDereferenced(with: loader)
348348

349349
schemas = try await newSchemas
350350
responses = try await newResponses
@@ -377,7 +377,9 @@ extension OpenAPI.Components {
377377
&& c8Resolved.isEmpty
378378
&& c9Resolved.isEmpty
379379

380-
if noNewComponents { return }
380+
let newMessages = try await context + m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9
381+
382+
if noNewComponents { return newMessages }
381383

382384
try merge(c1Resolved)
383385
try merge(c2Resolved)
@@ -391,9 +393,9 @@ extension OpenAPI.Components {
391393

392394
switch depth {
393395
case .iterations(let number):
394-
try await externallyDereference(with: loader, depth: .iterations(number - 1))
396+
return try await externallyDereference(with: loader, depth: .iterations(number - 1), context: newMessages)
395397
case .full:
396-
try await externallyDereference(with: loader, depth: .full)
398+
return try await externallyDereference(with: loader, depth: .full, context: newMessages)
397399
}
398400
}
399401
}

Sources/OpenAPIKit/Content/DereferencedContent.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,28 +77,31 @@ extension OpenAPI.Content: LocallyDereferenceable {
7777
}
7878

7979
extension OpenAPI.Content: ExternallyDereferenceable {
80-
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
80+
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components, [Loader.Message]) {
8181
let oldSchema = schema
8282

83-
async let (newSchema, c1) = oldSchema.externallyDereferenced(with: loader)
83+
async let (newSchema, c1, m1) = oldSchema.externallyDereferenced(with: loader)
8484

8585
var newContent = self
8686
var newComponents = try await c1
87+
var newMessages = try await m1
8788

8889
newContent.schema = try await newSchema
8990

9091
if let oldExamples = examples {
91-
let (newExamples, c2) = try await oldExamples.externallyDereferenced(with: loader)
92+
let (newExamples, c2, m2) = try await oldExamples.externallyDereferenced(with: loader)
9293
newContent.examples = newExamples
9394
try newComponents.merge(c2)
95+
newMessages += m2
9496
}
9597

9698
if let oldEncoding = encoding {
97-
let (newEncoding, c3) = try await oldEncoding.externallyDereferenced(with: loader)
99+
let (newEncoding, c3, m3) = try await oldEncoding.externallyDereferenced(with: loader)
98100
newContent.encoding = newEncoding
99101
try newComponents.merge(c3)
102+
newMessages += m3
100103
}
101104

102-
return (newContent, newComponents)
105+
return (newContent, newComponents, newMessages)
103106
}
104107
}

Sources/OpenAPIKit/Content/DereferencedContentEncoding.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,17 @@ extension OpenAPI.Content.Encoding: LocallyDereferenceable {
5858
}
5959

6060
extension OpenAPI.Content.Encoding: ExternallyDereferenceable {
61-
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
61+
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components, [Loader.Message]) {
6262
let newHeaders: OpenAPI.Header.Map?
6363
let newComponents: OpenAPI.Components
64+
let newMessages: [Loader.Message]
6465

6566
if let oldHeaders = headers {
66-
(newHeaders, newComponents) = try await oldHeaders.externallyDereferenced(with: loader)
67+
(newHeaders, newComponents, newMessages) = try await oldHeaders.externallyDereferenced(with: loader)
6768
} else {
6869
newHeaders = nil
6970
newComponents = .init()
71+
newMessages = []
7072
}
7173

7274
let newEncoding = OpenAPI.Content.Encoding(
@@ -77,6 +79,6 @@ extension OpenAPI.Content.Encoding: ExternallyDereferenceable {
7779
allowReserved: allowReserved
7880
)
7981

80-
return (newEncoding, newComponents)
82+
return (newEncoding, newComponents, newMessages)
8183
}
8284
}

Sources/OpenAPIKit/Document/Document.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,24 +362,26 @@ extension OpenAPI.Document {
362362
return try DereferencedDocument(self)
363363
}
364364

365-
public mutating func externallyDereference<Loader: ExternalLoader>(with loader: Loader.Type, depth: ExternalDereferenceDepth = .iterations(1)) async throws {
365+
public mutating func externallyDereference<Loader: ExternalLoader>(with loader: Loader.Type, depth: ExternalDereferenceDepth = .iterations(1), context: [Loader.Message] = []) async throws -> [Loader.Message] {
366366
if case let .iterations(number) = depth,
367367
number <= 0 {
368-
return
368+
return context
369369
}
370370

371371
let oldPaths = paths
372372
let oldWebhooks = webhooks
373373

374-
async let (newPaths, c1) = oldPaths.externallyDereferenced(with: loader)
375-
async let (newWebhooks, c2) = oldWebhooks.externallyDereferenced(with: loader)
374+
async let (newPaths, c1, m1) = oldPaths.externallyDereferenced(with: loader)
375+
async let (newWebhooks, c2, m2) = oldWebhooks.externallyDereferenced(with: loader)
376376

377377
paths = try await newPaths
378378
webhooks = try await newWebhooks
379379
try await components.merge(c1)
380380
try await components.merge(c2)
381381

382-
try await components.externallyDereference(with: loader, depth: depth)
382+
let m3 = try await components.externallyDereference(with: loader, depth: depth, context: context)
383+
384+
return try await context + m1 + m2 + m3
383385
}
384386
}
385387

Sources/OpenAPIKit/Either/Either+ExternallyDereferenceable.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import OpenAPIKitCore
1010
// MARK: - ExternallyDereferenceable
1111
extension Either: ExternallyDereferenceable where A: ExternallyDereferenceable, B: ExternallyDereferenceable {
1212

13-
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
13+
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components, [Loader.Message]) {
1414
switch self {
1515
case .a(let a):
16-
let (newA, components) = try await a.externallyDereferenced(with: loader)
17-
return (.a(newA), components)
16+
let (newA, components, messages) = try await a.externallyDereferenced(with: loader)
17+
return (.a(newA), components, messages)
1818
case .b(let b):
19-
let (newB, components) = try await b.externallyDereferenced(with: loader)
20-
return (.b(newB), components)
19+
let (newB, components, messages) = try await b.externallyDereferenced(with: loader)
20+
return (.b(newB), components, messages)
2121
}
2222
}
2323
}

Sources/OpenAPIKit/Example.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ extension OpenAPI.Example: LocallyDereferenceable {
209209
}
210210

211211
extension OpenAPI.Example: ExternallyDereferenceable {
212-
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
213-
return (self, .init())
212+
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components, [Loader.Message]) {
213+
return (self, .init(), [])
214214
}
215215
}
216216

Sources/OpenAPIKit/ExternalLoader.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@ import Foundation
1212
/// without knowing the details of what decoder is being used or how new internal
1313
/// references should be named.
1414
public protocol ExternalLoader {
15+
/// This can be anything that an implementor of this protocol wants to pass back from
16+
/// the `load()` function and have available after all external loading has been done.
17+
///
18+
/// A trivial type if no Messages are needed would be Void.
19+
associatedtype Message
20+
1521
/// Load the given URL and decode it as Type `T`. All Types `T` are `Decodable`, so
1622
/// the only real responsibility of a `load` function is to locate and load the given
1723
/// `URL` and pass its `Data` or `String` (depending on the decoder) to an appropriate
1824
/// `Decoder` for the given file type.
19-
static func load<T>(_: URL) async throws -> T where T: Decodable
25+
static func load<T>(_: URL) async throws -> (T, [Message]) where T: Decodable
2026

2127
/// Determine the next Component Key (where to store something in the
2228
/// Components Object) for a new object of the given type that was loaded
@@ -30,5 +36,5 @@ public protocol ExternalLoader {
3036
}
3137

3238
public protocol ExternallyDereferenceable {
33-
func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components)
39+
func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components, [Loader.Message])
3440
}

Sources/OpenAPIKit/Header/DereferencedHeader.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,27 @@ extension OpenAPI.Header: LocallyDereferenceable {
8484
}
8585

8686
extension OpenAPI.Header: ExternallyDereferenceable {
87-
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
87+
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components, [Loader.Message]) {
8888

8989
// if not for a Swift bug, this whole next bit would just be the
9090
// next line:
9191
// let (newSchemaOrContent, components) = try await schemaOrContent.externallyDereferenced(with: loader)
9292

9393
let newSchemaOrContent: Either<SchemaContext, OpenAPI.Content.Map>
9494
let newComponents: OpenAPI.Components
95+
let newMessages: [Loader.Message]
9596

9697
switch schemaOrContent {
9798
case .a(let schemaContext):
98-
let (context, components) = try await schemaContext.externallyDereferenced(with: loader)
99+
let (context, components, messages) = try await schemaContext.externallyDereferenced(with: loader)
99100
newSchemaOrContent = .a(context)
100101
newComponents = components
102+
newMessages = messages
101103
case .b(let contentMap):
102-
let (map, components) = try await contentMap.externallyDereferenced(with: loader)
104+
let (map, components, messages) = try await contentMap.externallyDereferenced(with: loader)
103105
newSchemaOrContent = .b(map)
104106
newComponents = components
107+
newMessages = messages
105108
}
106109

107110
let newHeader = OpenAPI.Header(
@@ -112,6 +115,6 @@ extension OpenAPI.Header: ExternallyDereferenceable {
112115
vendorExtensions: vendorExtensions
113116
)
114117

115-
return (newHeader, newComponents)
118+
return (newHeader, newComponents, newMessages)
116119
}
117120
}

Sources/OpenAPIKit/JSONReference.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -538,16 +538,16 @@ extension JSONReference: LocallyDereferenceable where ReferenceType: LocallyDere
538538
}
539539

540540
extension JSONReference: ExternallyDereferenceable where ReferenceType: ExternallyDereferenceable & Decodable & Equatable {
541-
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
541+
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components, [Loader.Message]) {
542542
switch self {
543543
case .internal(let ref):
544-
return (.internal(ref), .init())
544+
return (.internal(ref), .init(), [])
545545
case .external(let url):
546546
let componentKey = try loader.componentKey(type: ReferenceType.self, at: url)
547-
let component: ReferenceType = try await loader.load(url)
547+
let (component, messages): (ReferenceType, [Loader.Message]) = try await loader.load(url)
548548
var components = OpenAPI.Components()
549549
components[keyPath: ReferenceType.openAPIComponentsKeyPath][componentKey] = component
550-
return (try components.reference(named: componentKey.rawValue, ofType: ReferenceType.self).jsonReference, components)
550+
return (try components.reference(named: componentKey.rawValue, ofType: ReferenceType.self).jsonReference, components, messages)
551551
}
552552
}
553553
}
@@ -580,9 +580,9 @@ extension OpenAPI.Reference: LocallyDereferenceable where ReferenceType: Locally
580580
}
581581

582582
extension OpenAPI.Reference: ExternallyDereferenceable where ReferenceType: ExternallyDereferenceable & Decodable & Equatable {
583-
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
584-
let (newRef, components) = try await jsonReference.externallyDereferenced(with: loader)
585-
return (.init(newRef), components)
583+
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components, [Loader.Message]) {
584+
let (newRef, components, messages) = try await jsonReference.externallyDereferenced(with: loader)
585+
return (.init(newRef), components, messages)
586586
}
587587
}
588588

Sources/OpenAPIKit/Link.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,13 @@ extension OpenAPI.Link: LocallyDereferenceable {
290290
}
291291

292292
extension OpenAPI.Link: ExternallyDereferenceable {
293-
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
294-
let (newServer, newComponents) = try await server.externallyDereferenced(with: loader)
293+
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components, [Loader.Message]) {
294+
let (newServer, newComponents, newMessages) = try await server.externallyDereferenced(with: loader)
295295

296296
var newLink = self
297297
newLink.server = newServer
298298

299-
return (newLink, newComponents)
299+
return (newLink, newComponents, newMessages)
300300
}
301301
}
302302

0 commit comments

Comments
 (0)