Skip to content

Commit

Permalink
Merge branch 'main' into release/4_0
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpolzin authored Feb 12, 2025
2 parents 8353ec0 + 90c137c commit c5aa61b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
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://spec.openapis.org/oas/v3.1.1.html#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

0 comments on commit c5aa61b

Please sign in to comment.