Skip to content

Commit b5217e2

Browse files
authored
Merge pull request #76 from mattpolzin/get-all-routes
Get all routes
2 parents 0afa8f9 + 2128188 commit b5217e2

File tree

7 files changed

+137
-59
lines changed

7 files changed

+137
-59
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.

Sources/OpenAPIKit/DocumentInfo.swift

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ extension OpenAPI.Document {
2626
/// where the values are anything codable.
2727
public var vendorExtensions: [String: AnyCodable]
2828

29-
public init(title: String,
30-
description: String? = nil,
31-
termsOfService: URL? = nil,
32-
contact: Contact? = nil,
33-
license: License? = nil,
34-
version: String,
35-
vendorExtensions: [String: AnyCodable] = [:]) {
29+
public init(
30+
title: String,
31+
description: String? = nil,
32+
termsOfService: URL? = nil,
33+
contact: Contact? = nil,
34+
license: License? = nil,
35+
version: String,
36+
vendorExtensions: [String: AnyCodable] = [:]
37+
) {
3638
self.title = title
3739
self.description = description
3840
self.termsOfService = termsOfService
@@ -57,10 +59,12 @@ extension OpenAPI.Document {
5759
/// where the values are anything codable.
5860
public var vendorExtensions: [String: AnyCodable]
5961

60-
public init(name: String? = nil,
61-
url: URL? = nil,
62-
email: String? = nil,
63-
vendorExtensions: [String: AnyCodable] = [:]) {
62+
public init(
63+
name: String? = nil,
64+
url: URL? = nil,
65+
email: String? = nil,
66+
vendorExtensions: [String: AnyCodable] = [:]
67+
) {
6468
self.name = name
6569
self.url = url
6670
self.email = email
@@ -82,9 +86,11 @@ extension OpenAPI.Document {
8286
/// where the values are anything codable.
8387
public var vendorExtensions: [String: AnyCodable]
8488

85-
public init(name: String,
86-
url: URL? = nil,
87-
vendorExtensions: [String: AnyCodable] = [:]) {
89+
public init(
90+
name: String,
91+
url: URL? = nil,
92+
vendorExtensions: [String: AnyCodable] = [:]
93+
) {
8894
self.name = name
8995
self.url = url
9096
self.vendorExtensions = vendorExtensions

Sources/OpenAPIKit/Example.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ extension OpenAPI {
2525
/// where the values are anything codable.
2626
public let vendorExtensions: [String: AnyCodable]
2727

28-
public init(summary: String? = nil,
29-
description: String? = nil,
30-
value: Either<URL, AnyCodable>,
31-
vendorExtensions: [String: AnyCodable] = [:]) {
28+
public init(
29+
summary: String? = nil,
30+
description: String? = nil,
31+
value: Either<URL, AnyCodable>,
32+
vendorExtensions: [String: AnyCodable] = [:]
33+
) {
3234
self.summary = summary
3335
self.description = description
3436
self.value = value

Sources/OpenAPIKit/Operation.swift

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,20 @@ extension OpenAPI {
3232
/// where the values are anything codable.
3333
public var vendorExtensions: [String: AnyCodable]
3434

35-
public init(tags: [String]? = nil,
36-
summary: String? = nil,
37-
description: String? = nil,
38-
externalDocs: OpenAPI.ExternalDocumentation? = nil,
39-
operationId: String? = nil,
40-
parameters: Parameter.Array = [],
41-
requestBody: OpenAPI.Request? = nil,
42-
responses: OpenAPI.Response.Map,
43-
deprecated: Bool = false,
44-
security: [OpenAPI.SecurityRequirement]? = nil,
45-
servers: [OpenAPI.Server]? = nil,
46-
vendorExtensions: [String: AnyCodable] = [:]) {
35+
public init(
36+
tags: [String]? = nil,
37+
summary: String? = nil,
38+
description: String? = nil,
39+
externalDocs: OpenAPI.ExternalDocumentation? = nil,
40+
operationId: String? = nil,
41+
parameters: Parameter.Array = [],
42+
requestBody: OpenAPI.Request? = nil,
43+
responses: OpenAPI.Response.Map,
44+
deprecated: Bool = false,
45+
security: [OpenAPI.SecurityRequirement]? = nil,
46+
servers: [OpenAPI.Server]? = nil,
47+
vendorExtensions: [String: AnyCodable] = [:]
48+
) {
4749
self.tags = tags
4850
self.summary = summary
4951
self.description = description
@@ -59,18 +61,20 @@ extension OpenAPI {
5961
}
6062

6163
// variadic tags
62-
public init(tags: String...,
63-
summary: String? = nil,
64-
description: String? = nil,
65-
externalDocs: OpenAPI.ExternalDocumentation? = nil,
66-
operationId: String? = nil,
67-
parameters: Parameter.Array = [],
68-
requestBody: OpenAPI.Request? = nil,
69-
responses: OpenAPI.Response.Map,
70-
deprecated: Bool = false,
71-
security: [OpenAPI.SecurityRequirement]? = nil,
72-
servers: [OpenAPI.Server]? = nil,
73-
vendorExtensions: [String: AnyCodable] = [:]) {
64+
public init(
65+
tags: String...,
66+
summary: String? = nil,
67+
description: String? = nil,
68+
externalDocs: OpenAPI.ExternalDocumentation? = nil,
69+
operationId: String? = nil,
70+
parameters: Parameter.Array = [],
71+
requestBody: OpenAPI.Request? = nil,
72+
responses: OpenAPI.Response.Map,
73+
deprecated: Bool = false,
74+
security: [OpenAPI.SecurityRequirement]? = nil,
75+
servers: [OpenAPI.Server]? = nil,
76+
vendorExtensions: [String: AnyCodable] = [:]
77+
) {
7478
self.init(
7579
tags: tags,
7680
summary: summary,

Sources/OpenAPIKit/PathItem.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,21 @@ extension OpenAPI {
6464
/// where the values are anything codable.
6565
public var vendorExtensions: [String: AnyCodable]
6666

67-
public init(summary: String? = nil,
68-
description: String? = nil,
69-
servers: [OpenAPI.Server]? = nil,
70-
parameters: Parameter.Array = [],
71-
get: Operation? = nil,
72-
put: Operation? = nil,
73-
post: Operation? = nil,
74-
delete: Operation? = nil,
75-
options: Operation? = nil,
76-
head: Operation? = nil,
77-
patch: Operation? = nil,
78-
trace: Operation? = nil,
79-
vendorExtensions: [String: AnyCodable] = [:]) {
67+
public init(
68+
summary: String? = nil,
69+
description: String? = nil,
70+
servers: [OpenAPI.Server]? = nil,
71+
parameters: Parameter.Array = [],
72+
get: Operation? = nil,
73+
put: Operation? = nil,
74+
post: Operation? = nil,
75+
delete: Operation? = nil,
76+
options: Operation? = nil,
77+
head: Operation? = nil,
78+
patch: Operation? = nil,
79+
trace: Operation? = nil,
80+
vendorExtensions: [String: AnyCodable] = [:]
81+
) {
8082
self.summary = summary
8183
self.description = description
8284
self.servers = servers

Sources/OpenAPIKit/Server.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ extension OpenAPI {
2323
/// where the values are anything codable.
2424
public var vendorExtensions: [String: AnyCodable]
2525

26-
public init(url: URL,
27-
description: String? = nil,
28-
variables: OrderedDictionary<String, Variable> = [:],
29-
vendorExtensions: [String: AnyCodable] = [:]) {
26+
public init(
27+
url: URL,
28+
description: String? = nil,
29+
variables: OrderedDictionary<String, Variable> = [:],
30+
vendorExtensions: [String: AnyCodable] = [:]
31+
) {
3032
self.url = url
3133
self.description = description
3234
self.variables = variables

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)