Skip to content

Commit

Permalink
make result of Document external dereferencing discardable. fix json …
Browse files Browse the repository at this point in the history
…reference external dereferencing tests
  • Loading branch information
mattpolzin committed Apr 24, 2024
1 parent e599b45 commit a752a43
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions Sources/OpenAPIKit/Document/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ extension OpenAPI.Document {
return try DereferencedDocument(self)
}

@discardableResult
public mutating func externallyDereference<Loader: ExternalLoader>(with loader: Loader.Type, depth: ExternalDereferenceDepth = .iterations(1), context: [Loader.Message] = []) async throws -> [Loader.Message] {
if case let .iterations(number) = depth,
number <= 0 {
Expand Down
13 changes: 8 additions & 5 deletions Tests/OpenAPIKitTests/JSONReferenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -382,28 +382,31 @@ extension JSONReferenceTests {
func test_externalDerefNoFragment() async throws {
let reference: JSONReference<JSONSchema> = .external(.init(string: "./schema.json")!)

let (newReference, components) = try await reference.externallyDereferenced(with: SchemaLoader.self)
let (newReference, components, messages) = try await reference.externallyDereferenced(with: SchemaLoader.self)

XCTAssertEqual(newReference, .component(named: "__schema_json"))
XCTAssertEqual(components, .init(schemas: ["__schema_json": .string]))
XCTAssertEqual(messages, ["./schema.json"])
}

func test_externalDerefFragment() async throws {
let reference: JSONReference<JSONSchema> = .external(.init(string: "./schema.json#/test")!)

let (newReference, components) = try await reference.externallyDereferenced(with: SchemaLoader.self)
let (newReference, components, messages) = try await reference.externallyDereferenced(with: SchemaLoader.self)

XCTAssertEqual(newReference, .component(named: "__schema_json__test"))
XCTAssertEqual(components, .init(schemas: ["__schema_json__test": .string]))
XCTAssertEqual(messages, ["./schema.json#/test"])
}

func test_externalDerefExternalComponents() async throws {
let reference: JSONReference<JSONSchema> = .external(.init(string: "./schema.json#/components/schemas/test")!)

let (newReference, components) = try await reference.externallyDereferenced(with: SchemaLoader.self)
let (newReference, components, messages) = try await reference.externallyDereferenced(with: SchemaLoader.self)

XCTAssertEqual(newReference, .component(named: "__schema_json__components_schemas_test"))
XCTAssertEqual(components, .init(schemas: ["__schema_json__components_schemas_test": .string]))
XCTAssertEqual(messages, ["./schema.json#/components/schemas/test"])
}
}

Expand All @@ -414,8 +417,8 @@ extension JSONReferenceTests {
}

struct SchemaLoader: ExternalLoader {
static func load<T>(_ url: URL) -> T where T: Decodable {
return JSONSchema.string as! T
static func load<T: Decodable>(_ url: URL) -> (T, [String]) {
return (JSONSchema.string as! T, [url.absoluteString])
}

static func componentKey<T>(type: T.Type, at url: URL) throws -> OpenAPI.ComponentKey {
Expand Down

0 comments on commit a752a43

Please sign in to comment.