diff --git a/Sources/OpenAPIKit/Document/Document.swift b/Sources/OpenAPIKit/Document/Document.swift index 0e302d4c9..8264509c0 100644 --- a/Sources/OpenAPIKit/Document/Document.swift +++ b/Sources/OpenAPIKit/Document/Document.swift @@ -217,12 +217,13 @@ extension OpenAPI.Document { } } - /// Retrieve an array of all locally defined Operation Ids defined by - /// this API. These Ids are guaranteed to be unique by + /// Retrieve an array of all Operation Ids defined by locally + /// by this API. These Ids are guaranteed to be unique by /// the OpenAPI Specification. /// - /// PathItems will be looked up in the components, but any remote references - /// or path items missing from the components will be ignored. + /// `PathItems` from `paths` and `webhooks` will be looked + /// up in the components, but any remote references or path items + /// missing from the components will be ignored. /// /// The ordering is not necessarily significant, but it will /// be the order in which each operation is occurred within @@ -232,10 +233,10 @@ extension OpenAPI.Document { /// See [Operation Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#operation-object) in the specifcation. /// public var allOperationIds: [String] { - return paths.values - .compactMap { components[$0] } - .flatMap { $0.endpoints } - .compactMap { $0.operation.operationId } + return (paths.values + webhooks.values) + .compactMap { components[$0] } + .flatMap { $0.endpoints } + .compactMap { $0.operation.operationId } } /// All servers referenced anywhere in the whole document. @@ -296,7 +297,7 @@ extension OpenAPI.Document { } } - for pathItem in paths.values { + for pathItem in (paths.values + webhooks.values) { let pathItemServers = components[pathItem]?.servers ?? [] pathItemServers.forEach(insertUniquely) @@ -317,7 +318,7 @@ extension OpenAPI.Document { public var allTags: Set { return Set( (tags ?? []).map { $0.name } - + paths.values.compactMap { components[$0] } + + (paths.values + webhooks.values).compactMap { components[$0] } .flatMap { $0.endpoints } .flatMap { $0.operation.tags ?? [] } ) diff --git a/Tests/OpenAPIKitTests/Document/DocumentTests.swift b/Tests/OpenAPIKitTests/Document/DocumentTests.swift index c68703e8d..4ae5f9070 100644 --- a/Tests/OpenAPIKitTests/Document/DocumentTests.swift +++ b/Tests/OpenAPIKitTests/Document/DocumentTests.swift @@ -79,6 +79,7 @@ final class DocumentTests: XCTestCase { } func test_getAllOperationIds() { + // paths, no operation ids, no components, no webhooks let t1 = OpenAPI.Document( info: .init(title: "test", version: "1.0"), servers: [], @@ -93,6 +94,7 @@ final class DocumentTests: XCTestCase { XCTAssertEqual(t1.allOperationIds, []) + // paths, one operation id (second one nil), no components, no webhooks let t2 = OpenAPI.Document( info: .init(title: "test", version: "1.0"), servers: [], @@ -107,6 +109,7 @@ final class DocumentTests: XCTestCase { XCTAssertEqual(t2.allOperationIds, ["test"]) + // paths, multiple operation ids, no components, no webhooks let t3 = OpenAPI.Document( info: .init(title: "test", version: "1.0"), servers: [], @@ -121,6 +124,7 @@ final class DocumentTests: XCTestCase { XCTAssertEqual(t3.allOperationIds, ["test", "two"]) + // paths, one operation id (first one nil), no components, no webhooks let t4 = OpenAPI.Document( info: .init(title: "test", version: "1.0"), servers: [], @@ -134,6 +138,36 @@ final class DocumentTests: XCTestCase { ) XCTAssertEqual(t4.allOperationIds, ["two"]) + + // paths, one operation id, one component reference, no webhooks + let t5 = OpenAPI.Document( + info: .init(title: "test", version: "1.0"), + servers: [], + paths: [ + "/hello": .init( + get: .init(operationId: "test", responses: [:])), + "/hello/world": .reference(.component(named: "hello-world")) + ], + components: .init( + pathItems: ["hello-world": .init(put: .init(operationId: "two", responses: [:]))] + ) + ) + + XCTAssertEqual(t5.allOperationIds, ["test", "two"]) + + // no paths, one webhook with an operation id + let t6 = OpenAPI.Document( + info: .init(title: "test", version: "1.0"), + servers: [], + paths: [:], + webhooks: [ + "/hello": .init( + get: .init(operationId: "test", responses: [:])) + ], + components: .noComponents + ) + + XCTAssertEqual(t6.allOperationIds, ["test"]) } func test_allServersEmpty() {