Skip to content

Commit d0e9fb5

Browse files
committed
Add some conveniences for working with references to JSON Schemas
1 parent a137e14 commit d0e9fb5

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ extension OpenAPI.Components {
230230
/// If you want to look something up without throwing, you might want to use the subscript
231231
/// operator on the `Components`.
232232
///
233+
/// If you are recursing through JSONSchema references, you may want to
234+
/// call `flattenToJsonSchema()` on the result of `lookupOnce()` because
235+
/// `lookupOnce()` will wrap any resulting `JSONSchema` reference in an
236+
/// `OpenAPI.Reference` which adds work if all you want to do is recurse.
237+
///
233238
/// If you also want to fully dereference the value in question instead
234239
/// of just looking it up see the various `dereference` functions
235240
/// on this type for more information.
@@ -287,6 +292,11 @@ extension OpenAPI.Components {
287292
/// If you want to look something up without throwing, you might want to use the subscript
288293
/// operator on the `Components`.
289294
///
295+
/// If you are recursing through JSONSchema references, you may want to
296+
/// call `flattenToJsonSchema()` on the result of `lookupOnce()` because
297+
/// `lookupOnce()` will wrap any resulting `JSONSchema` reference in an
298+
/// `OpenAPI.Reference` which adds work if all you want to do is recurse.
299+
///
290300
/// If you also want to fully dereference the value in question instead
291301
/// of just looking it up see the various `dereference` functions
292302
/// on this type for more information.

Sources/OpenAPIKit/JSONReference.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,42 @@ extension OpenAPI {
387387
}
388388
}
389389

390+
public extension OpenAPI.Reference where ReferenceType == JSONSchema {
391+
/// When an OpenAPI.Reference points at a JSONSchema, it can be represented
392+
/// as a JSONSchema directly because JSONSchema has an innate ability to
393+
/// represent references itself.
394+
///
395+
/// Example:
396+
///
397+
/// OpenAPI.Reference<JSONSchema>.component(named: "hello").asJsonSchema()
398+
/// // results in JSONSchema.reference(.component(named: "hello"))
399+
func asJsonSchema() -> JSONSchema {
400+
.reference(jsonReference)
401+
.overriddenNonNil(summary: summary)
402+
.overriddenNonNil(description: description)
403+
}
404+
}
405+
406+
public extension Either where A == OpenAPI.Reference<JSONSchema>, B == JSONSchema {
407+
/// When an Either represents a reference or a schema, it can be
408+
/// "flattened" to a JSONSchema because JSONSchema has an innate ability to
409+
/// represent references itself.
410+
///
411+
/// Examples:
412+
///
413+
/// Either<OpenAPI.Reference<JSONSchema>, JSONSchema>.a(.component(named: "hello")).flattenToJsonSchema()
414+
/// // results in JSONSchema.reference(.component(named: "hello"))
415+
///
416+
/// Either<OpenAPI.Reference<JSONSchema>, JSONSchema>.b(.string).flattenToJsonSchema()
417+
/// // results in JSONSchema.string
418+
func flattenToJsonSchema() -> JSONSchema {
419+
switch self {
420+
case .a(let reference): reference.asJsonSchema()
421+
case .b(let schema): schema
422+
}
423+
}
424+
}
425+
390426
public extension JSONReference {
391427
/// Create an OpenAPI.Reference from the given JSONReference.
392428
func openAPIReference(withDescription description: String? = nil) -> OpenAPI.Reference<ReferenceType> {

Tests/OpenAPIKitTests/JSONReferenceTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,21 @@ final class JSONReferenceTests: XCTestCase {
180180
XCTAssertEqual(t1.openAPIReference().internalValue, .component(name: "hello"))
181181

182182
}
183+
184+
func test_asJsonSchema() {
185+
XCTAssertEqual(
186+
OpenAPI.Reference<JSONSchema>.component(named: "hello").asJsonSchema(),
187+
JSONSchema.reference(.component(named: "hello"))
188+
)
189+
XCTAssertEqual(
190+
Either<OpenAPI.Reference<JSONSchema>, JSONSchema>.a(.component(named: "hello")).flattenToJsonSchema(),
191+
JSONSchema.reference(.component(named: "hello"))
192+
)
193+
XCTAssertEqual(
194+
Either<OpenAPI.Reference<JSONSchema>, JSONSchema>.b(.string).flattenToJsonSchema(),
195+
.string
196+
)
197+
}
183198
}
184199

185200
// MARK: Codable

0 commit comments

Comments
 (0)