Skip to content

Commit

Permalink
test external dereferencing messages at the full document level.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpolzin committed Apr 25, 2024
1 parent c6d90f5 commit 1d8f556
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
8 changes: 5 additions & 3 deletions Sources/OpenAPIKit30/Operation/DereferencedOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,14 @@ extension OpenAPI.Operation: ExternallyDereferenceable {
let oldParameters = parameters
let oldRequestBody = requestBody
let oldResponses = responses
let oldCallbacks = callbacks
let oldServers = servers

async let (newParameters, c1, m1) = oldParameters.externallyDereferenced(with: loader)
async let (newRequestBody, c2, m2) = oldRequestBody.externallyDereferenced(with: loader)
async let (newResponses, c3, m3) = oldResponses.externallyDereferenced(with: loader)
async let (newCallbacks, c4, m4) = callbacks.externallyDereferenced(with: loader)
// let (newServers, c5, m5) = try await servers.externallyDereferenced(with: loader)
async let (newCallbacks, c4, m4) = oldCallbacks.externallyDereferenced(with: loader)
// let (newServers, c5, m5) = try await oldServers.externallyDereferenced(with: loader)

var newOperation = self
var newComponents = try await c1
Expand All @@ -153,7 +155,7 @@ extension OpenAPI.Operation: ExternallyDereferenceable {
try await newMessages += m4

// should not be necessary but current Swift compiler can't figure out conformance of ExternallyDereferenceable:
if let oldServers = servers {
if let oldServers {
let (newServers, c5, m5) = try await oldServers.externallyDereferenced(with: loader)
newOperation.servers = newServers
try newComponents.merge(c5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class ExternalDereferencingDocumentTests: XCTestCase {
/// An example of implementing a loader context for loading external references
/// into an OpenAPI document.
struct ExampleLoader: ExternalLoader {
typealias Message = Void
typealias Message = String

static func load<T>(_ url: URL) async throws -> (T, [Message]) where T : Decodable {
// load data from file, perhaps. we will just mock that up for the test:
Expand All @@ -32,7 +32,7 @@ final class ExternalDereferencingDocumentTests: XCTestCase {
} else {
finished = decoded
}
return (finished, [])
return (finished, [url.absoluteString])
}

static func componentKey<T>(type: T.Type, at url: URL) throws -> OpenAPIKit30.OpenAPI.ComponentKey {
Expand Down Expand Up @@ -217,7 +217,8 @@ final class ExternalDereferencingDocumentTests: XCTestCase {
.reference(.external(URL(string: "file://./params/name.json")!))
]
),
"/webhook": .reference(.external(URL(string: "file://./paths/webhook.json")!))
"/webhook": .reference(.external(URL(string: "file://./paths/webhook.json")!)),
"/callback": .reference(.external(URL(string: "file://./paths/callback.json")!))
],
components: .init(
schemas: [
Expand All @@ -242,10 +243,30 @@ final class ExternalDereferencingDocumentTests: XCTestCase {
docCopy2.components.sort()

var docCopy3 = document
try await docCopy3.externallyDereference(with: ExampleLoader.self, depth: .full)
let messages = try await docCopy3.externallyDereference(with: ExampleLoader.self, depth: .full)
docCopy3.components.sort()

XCTAssertEqual(docCopy1, docCopy2)
XCTAssertEqual(docCopy2, docCopy3)

XCTAssertEqual(
messages.sorted(),
["file://./callbacks/one.json",
"file://./examples/good.json",
"file://./headers/webhook.json",
"file://./headers/webhook.json",
"file://./links/first.json",
"file://./params/name.json",
"file://./params/name.json",
"file://./paths/callback.json",
"file://./paths/webhook.json",
"file://./requests/webhook.json",
"file://./responses/webhook.json",
"file://./schemas/basic_object.json",
"file://./schemas/string_param.json",
"file://./schemas/string_param.json",
"file://./schemas/string_param.json",
"file://./schemas/string_param.json#"]
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class ExternalDereferencingDocumentTests: XCTestCase {
/// An example of implementing a loader context for loading external references
/// into an OpenAPI document.
struct ExampleLoader: ExternalLoader {
typealias Message = Void
typealias Message = String

static func load<T>(_ url: URL) async throws -> (T, [Message]) where T : Decodable {
// load data from file, perhaps. we will just mock that up for the test:
Expand All @@ -32,7 +32,7 @@ final class ExternalDereferencingDocumentTests: XCTestCase {
} else {
finished = decoded
}
return (finished, [])
return (finished, [url.absoluteString])
}

static func componentKey<T>(type: T.Type, at url: URL) throws -> OpenAPIKit.OpenAPI.ComponentKey {
Expand Down Expand Up @@ -245,10 +245,31 @@ final class ExternalDereferencingDocumentTests: XCTestCase {
docCopy2.components.sort()

var docCopy3 = document
try await docCopy3.externallyDereference(with: ExampleLoader.self, depth: .full)
let messages = try await docCopy3.externallyDereference(with: ExampleLoader.self, depth: .full)
docCopy3.components.sort()

XCTAssertEqual(docCopy1, docCopy2)
XCTAssertEqual(docCopy2, docCopy3)

XCTAssertEqual(
messages.sorted(),
["file://./callbacks/one.json",
"file://./examples/good.json",
"file://./headers/webhook.json",
"file://./headers/webhook.json",
"file://./links/first.json",
"file://./params/name.json",
"file://./params/name.json",
"file://./paths/callback.json",
"file://./paths/webhook.json",
"file://./paths/webhook.json",
"file://./requests/webhook.json",
"file://./responses/webhook.json",
"file://./schemas/basic_object.json",
"file://./schemas/string_param.json",
"file://./schemas/string_param.json",
"file://./schemas/string_param.json",
"file://./schemas/string_param.json#"]
)
}
}

0 comments on commit 1d8f556

Please sign in to comment.