From a752a43965d254d11fa314c613009d3d405837ec Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Wed, 24 Apr 2024 11:02:18 -0500 Subject: [PATCH] make result of Document external dereferencing discardable. fix json reference external dereferencing tests --- Sources/OpenAPIKit/Document/Document.swift | 1 + Tests/OpenAPIKitTests/JSONReferenceTests.swift | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Sources/OpenAPIKit/Document/Document.swift b/Sources/OpenAPIKit/Document/Document.swift index a2b13a36e..9c5c4519d 100644 --- a/Sources/OpenAPIKit/Document/Document.swift +++ b/Sources/OpenAPIKit/Document/Document.swift @@ -362,6 +362,7 @@ extension OpenAPI.Document { return try DereferencedDocument(self) } + @discardableResult public mutating func externallyDereference(with loader: Loader.Type, depth: ExternalDereferenceDepth = .iterations(1), context: [Loader.Message] = []) async throws -> [Loader.Message] { if case let .iterations(number) = depth, number <= 0 { diff --git a/Tests/OpenAPIKitTests/JSONReferenceTests.swift b/Tests/OpenAPIKitTests/JSONReferenceTests.swift index c5e74f18f..bb0d6d6d3 100644 --- a/Tests/OpenAPIKitTests/JSONReferenceTests.swift +++ b/Tests/OpenAPIKitTests/JSONReferenceTests.swift @@ -382,28 +382,31 @@ extension JSONReferenceTests { func test_externalDerefNoFragment() async throws { let reference: JSONReference = .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 = .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 = .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"]) } } @@ -414,8 +417,8 @@ extension JSONReferenceTests { } struct SchemaLoader: ExternalLoader { - static func load(_ url: URL) -> T where T: Decodable { - return JSONSchema.string as! T + static func load(_ url: URL) -> (T, [String]) { + return (JSONSchema.string as! T, [url.absoluteString]) } static func componentKey(type: T.Type, at url: URL) throws -> OpenAPI.ComponentKey {