Skip to content

Commit 3781886

Browse files
authored
Merge pull request #483 from mattpolzin/add-missing-lookup-helper
Add missing lookupOnce variants
2 parents cfc01be + d2f3eff commit 3781886

1 file changed

Lines changed: 87 additions & 18 deletions

File tree

Sources/OpenAPIKit/Components Object/Components+JSONReference.swift

Lines changed: 87 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,10 @@ extension OpenAPI.Components {
167167
/// object will have its `summary` and/or `description` overridden by that of the reference.
168168
/// This only applies if the referenced object would normally have a summary/description.
169169
///
170-
/// - Important: Looking up an external reference (i.e. one that points to another file)
171-
/// is not currently supported by OpenAPIKit and will therefore always throw an error.
170+
/// - Important: Loading an external reference (i.e. one that points to
171+
/// another file) is done via `externallyDereferenced(with:)` functions
172+
/// available on most types; attempting to lookup an external reference
173+
/// will `throw`.
172174
///
173175
/// - Throws: `ReferenceError.cannotLookupRemoteReference` or
174176
/// `ReferenceError.missingOnLookup(name:,key:)`
@@ -207,8 +209,10 @@ extension OpenAPI.Components {
207209
/// object will have its `summary` and/or `description` overridden by that of the reference.
208210
/// This only applies if the referenced object would normally have a summary/description.
209211
///
210-
/// - Important: Looking up an external reference (i.e. one that points to another file)
211-
/// is not currently supported by OpenAPIKit and will therefore always throw an error.
212+
/// - Important: Loading an external reference (i.e. one that points to
213+
/// another file) is done via `externallyDereferenced(with:)` functions
214+
/// available on most types; attempting to lookup an external reference
215+
/// will `throw`.
212216
///
213217
/// - Throws: `ReferenceError.cannotLookupRemoteReference` or
214218
/// `ReferenceError.missingOnLookup(name:,key:)` or
@@ -239,8 +243,10 @@ extension OpenAPI.Components {
239243
/// of just looking it up see the various `dereference` functions
240244
/// on this type for more information.
241245
///
242-
/// - Important: Looking up an external reference (i.e. one that points to another file)
243-
/// is not currently supported by OpenAPIKit and will therefore always throw an error.
246+
/// - Important: Loading an external reference (i.e. one that points to
247+
/// another file) is done via `externallyDereferenced(with:)` functions
248+
/// available on most types; attempting to lookup an external reference
249+
/// will `throw`.
244250
///
245251
/// - Throws: `ReferenceError.cannotLookupRemoteReference` or
246252
/// `ReferenceError.missingOnLookup(name:,key:)`
@@ -262,8 +268,10 @@ extension OpenAPI.Components {
262268
/// of just looking it up see the various `dereference` functions
263269
/// on this type for more information.
264270
///
265-
/// - Important: Looking up an external reference (i.e. one that points to another file)
266-
/// is not currently supported by OpenAPIKit and will therefore always throw an error.
271+
/// - Important: Loading an external reference (i.e. one that points to
272+
/// another file) is done via `externallyDereferenced(with:)` functions
273+
/// available on most types; attempting to lookup an external reference
274+
/// will `throw`.
267275
///
268276
/// - Throws: `ReferenceError.cannotLookupRemoteReference` or
269277
/// `ReferenceError.missingOnLookup(name:,key:)` or
@@ -301,8 +309,10 @@ extension OpenAPI.Components {
301309
/// of just looking it up see the various `dereference` functions
302310
/// on this type for more information.
303311
///
304-
/// - Important: Looking up an external reference (i.e. one that points to another file)
305-
/// is not currently supported by OpenAPIKit and will therefore always throw an error.
312+
/// - Important: Loading an external reference (i.e. one that points to
313+
/// another file) is done via `externallyDereferenced(with:)` functions
314+
/// available on most types; attempting to lookup an external reference
315+
/// will `throw`.
306316
///
307317
/// - Throws: `ReferenceError.cannotLookupRemoteReference` or
308318
/// `ReferenceError.missingOnLookup(name:,key:)`
@@ -337,8 +347,10 @@ extension OpenAPI.Components {
337347
/// of just looking it up see the various `dereference` functions
338348
/// on this type for more information.
339349
///
340-
/// - Important: Looking up an external reference (i.e. one that points to another file)
341-
/// is not currently supported by OpenAPIKit and will therefore always throw an error.
350+
/// - Important: Loading an external reference (i.e. one that points to
351+
/// another file) is done via `externallyDereferenced(with:)` functions
352+
/// available on most types; attempting to lookup an external reference
353+
/// will `throw`.
342354
///
343355
/// - Throws: `ReferenceError.cannotLookupRemoteReference` or
344356
/// `ReferenceError.missingOnLookup(name:,key:)` or
@@ -399,8 +411,10 @@ extension OpenAPI.Components {
399411
/// of just looking it up see the various `dereference` functions
400412
/// on this type for more information.
401413
///
402-
/// - Important: Looking up an external reference (i.e. one that points to another file)
403-
/// is not currently supported by OpenAPIKit and will therefore always throw an error.
414+
/// - Important: Loading an external reference (i.e. one that points to
415+
/// another file) is done via `externallyDereferenced(with:)` functions
416+
/// available on most types; attempting to lookup an external reference
417+
/// will `throw`.
404418
///
405419
/// - Throws: `ReferenceError.cannotLookupRemoteReference` or
406420
/// `ReferenceError.missingOnLookup(name:,key:)` or
@@ -414,22 +428,52 @@ extension OpenAPI.Components {
414428
}
415429
}
416430

431+
/// Pass an `Either` with a reference or a component. `lookupOnce()` will
432+
/// return the component value if it is found in the Components Object.
433+
/// That value may itself be a reference. If you want to look things up in
434+
/// the components object recursively use `lookup()`.
435+
///
436+
/// If you want to look something up without throwing, you might want to use the subscript
437+
/// operator on the `Components`.
438+
///
439+
/// If you also want to fully dereference the value in question instead of
440+
/// just looking it up see the various `dereference` functions on this type
441+
/// for more information.
442+
///
443+
/// - Important: Loading an external reference (i.e. one that points to
444+
/// another file) is done via `externallyDereferenced(with:)` functions
445+
/// available on most types; attempting to lookup an external reference
446+
/// will `throw`.
447+
///
448+
/// - Throws: `ReferenceError.cannotLookupRemoteReference` or
449+
/// `ReferenceError.missingOnLookup(name:,key:)`
450+
public func lookupOnce<ReferenceType: ComponentDictionaryLocatable>(_ maybeReference: Either<OpenAPI.Reference<ReferenceType>, ReferenceType>) throws -> Either<OpenAPI.Reference<ReferenceType>, ReferenceType> {
451+
switch maybeReference {
452+
case .a(let reference):
453+
return try lookupOnce(reference.jsonReference)
454+
case .b(let value):
455+
return .b(value)
456+
}
457+
}
458+
417459
/// Lookup schema in the `Components`. If the JSONSchema is not a
418460
/// reference, it will be returned as-is. If it is a reference, it will be
419461
/// looked up in the components if it refers to a components entry.
420462
///
421463
/// This function will follow subsequent refernences found within the
422464
/// `Components` as long as no cycles are encountered.
423465
///
424-
/// If you want to look something up without throwing, you might want to use the subscript
425-
/// operator on the `Components`.
466+
/// If you want to look something up without throwing, you might want to
467+
/// use the subscript operator on the `Components`.
426468
///
427469
/// If you also want to fully dereference the value in question instead
428470
/// of just looking it up see the various `dereference` functions
429471
/// on this type for more information.
430472
///
431-
/// - Important: Looking up an external reference (i.e. one that points to another file)
432-
/// is not currently supported by OpenAPIKit and will therefore always throw an error.
473+
/// - Important: Loading an external reference (i.e. one that points to
474+
/// another file) is done via `externallyDereferenced(with:)` functions
475+
/// available on most types; attempting to lookup an external reference
476+
/// will `throw`.
433477
///
434478
/// - Throws: `ReferenceError.cannotLookupRemoteReference` or
435479
/// `ReferenceError.missingOnLookup(name:,key:)` or
@@ -441,6 +485,31 @@ extension OpenAPI.Components {
441485
return try lookup(reference)
442486
}
443487

488+
/// Lookup schema in the `Components`. If the JSONSchema is not a
489+
/// reference, it will be returned as-is. If it is a reference, it will be
490+
/// looked up in the components if it refers to a components entry.
491+
///
492+
/// If you want to look something up without throwing, you might want to
493+
/// use the subscript operator on the `Components`.
494+
///
495+
/// If you also want to fully dereference the value in question instead
496+
/// of just looking it up see the various `dereference` functions
497+
/// on this type for more information.
498+
///
499+
/// - Important: Loading an external reference (i.e. one that points to
500+
/// another file) is done via `externallyDereferenced(with:)` functions
501+
/// available on most types; attempting to lookup an external reference
502+
/// will `throw`.
503+
///
504+
/// - Throws: `ReferenceError.cannotLookupRemoteReference` or
505+
/// `ReferenceError.missingOnLookup(name:,key:)`
506+
public func lookupOnce(_ schema: JSONSchema) throws -> JSONSchema {
507+
guard case .reference(let reference, _) = schema.value else {
508+
return schema
509+
}
510+
return try lookupOnce(reference).flattenToJsonSchema()
511+
}
512+
444513
/// Create an `OpenAPI.Reference`.
445514
///
446515
/// - Throws: If the given name does not refer to an existing component of the given type.

0 commit comments

Comments
 (0)