Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Sources/OpenAPIKit/Schema Object/DereferencedJSONSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -729,12 +729,12 @@ extension JSONSchema: ExternallyDereferenceable {
newSchema = .init(
schema: .reference(newReference, core)
)
case .dynamicReference:
// TODO: external dereferencing of `$dynamicRef` is not implemented;
// deferred alongside local dynamic-scope resolution (see #359).
newComponents = .noComponents
newSchema = self
newMessages = []
case .dynamicReference(let dynamicRef, let core):
// Delegate to the wrapped JSONReference's external deref (same path as $ref).
let (newReference, components, messages) = try await dynamicRef.jsonReference.externallyDereferenced(with: loader)
newComponents = components
newMessages = messages
newSchema = .init(schema: .dynamicReference(JSONDynamicReference(newReference), core))
case .fragment(_):
newComponents = .noComponents
newSchema = self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,36 @@ final class JSONSchemaDynamicReferenceTests: XCTestCase {
}
}
}

#if ExternalLoading
extension JSONSchemaDynamicReferenceTests {
func test_externalDeref_dynamicReference_external() async throws {
// An external `$dynamicRef` is dereferenced through its underlying
// `JSONReference` -- same path as `$ref`: fetch + convert to an
// internal component reference.
let schema = JSONSchema.dynamicReference(
JSONDynamicReference(.external(.init(string: "./schema.json")!))
)

let (newSchema, components, messages) = try await schema.externallyDereferenced(with: JSONReferenceTests.SchemaLoader.self)

XCTAssertTrue(newSchema.isDynamicReference)
XCTAssertEqual(newSchema.dynamicReference?.name, "__schema_json")
XCTAssertEqual(components, .init(schemas: ["__schema_json": .string]))
XCTAssertEqual(messages, ["./schema.json"])
}

func test_externalDeref_dynamicReference_internal_noop() async throws {
// An internal `$dynamicRef` (anchor) is not external; external
// dereferencing leaves it unchanged.
let schema = JSONSchema.dynamicReference(.anchor("node"))

let (newSchema, components, messages) = try await schema.externallyDereferenced(with: JSONReferenceTests.SchemaLoader.self)

XCTAssertTrue(newSchema.isDynamicReference)
XCTAssertEqual(newSchema.dynamicReference?.absoluteString, "#node")
XCTAssertTrue(components.schemas.isEmpty)
XCTAssertEqual(messages, [])
}
}
#endif