Skip to content

Commit 0932f91

Browse files
committed
Add getter that retrieves an array of routes for a Document.
1 parent 0afa8f9 commit 0932f91

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

Sources/OpenAPIKit/Document.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,31 @@ extension OpenAPI {
5252
}
5353
}
5454

55+
extension OpenAPI.Document {
56+
/// A `Route` is the combination of a path (where the route lives)
57+
/// and a path item (the definition of the route).
58+
public struct Route: Equatable {
59+
public let path: OpenAPI.Path
60+
public let pathItem: OpenAPI.PathItem
61+
62+
public init(
63+
path: OpenAPI.Path,
64+
pathItem: OpenAPI.PathItem
65+
) {
66+
self.path = path
67+
self.pathItem = pathItem
68+
}
69+
}
70+
71+
/// Get all routes for this document.
72+
///
73+
/// - Returns: An Array of `Routes` with the path
74+
/// and the definition of the route.
75+
public var routes: [Route] {
76+
return paths.map { (path, pathItem) in .init(path: path, pathItem: pathItem) }
77+
}
78+
}
79+
5580
extension OpenAPI {
5681
/// If the security scheme is of type "oauth2" or "openIdConnect",
5782
/// then the value is a list of scope names required for the execution.

Tests/OpenAPIKitTests/DocumentTests.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,43 @@ final class DocumentTests: XCTestCase {
4141
)
4242
}
4343

44+
func test_getRoutes() {
45+
let pi1 = OpenAPI.PathItem(
46+
parameters: [],
47+
get: .init(
48+
tags: "hi",
49+
parameters: [],
50+
responses: [:]
51+
)
52+
)
53+
54+
let pi2 = OpenAPI.PathItem(
55+
get: .init(
56+
responses: [:]
57+
)
58+
)
59+
60+
let test = OpenAPI.Document(
61+
info: .init(title: "hi", version: "1.0"),
62+
servers: [
63+
.init(url: URL(string: "https://google.com")!)
64+
],
65+
paths: [
66+
"/hi/there": pi1,
67+
"/hi": pi2
68+
],
69+
components: .init(schemas: ["hello": .string])
70+
)
71+
72+
XCTAssertEqual(
73+
test.routes,
74+
[
75+
.init(path: "/hi/there", pathItem: pi1),
76+
.init(path: "/hi", pathItem: pi2)
77+
]
78+
)
79+
}
80+
4481
func test_existingSecuritySchemeSuccess() {
4582
let docData =
4683
"""

0 commit comments

Comments
 (0)