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
21 changes: 11 additions & 10 deletions Sources/OpenAPIKit/Document/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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)

Expand All @@ -317,7 +318,7 @@ extension OpenAPI.Document {
public var allTags: Set<String> {
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 ?? [] }
)
Expand Down
34 changes: 34 additions & 0 deletions Tests/OpenAPIKitTests/Document/DocumentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand All @@ -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: [],
Expand All @@ -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: [],
Expand All @@ -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: [],
Expand All @@ -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() {
Expand Down