Skip to content

Commit

Permalink
Merge pull request #76 from mattpolzin/get-all-routes
Browse files Browse the repository at this point in the history
Get all routes
  • Loading branch information
mattpolzin authored May 23, 2020
2 parents 0afa8f9 + 2128188 commit b5217e2
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 59 deletions.
25 changes: 25 additions & 0 deletions Sources/OpenAPIKit/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,31 @@ extension OpenAPI {
}
}

extension OpenAPI.Document {
/// A `Route` is the combination of a path (where the route lives)
/// and a path item (the definition of the route).
public struct Route: Equatable {
public let path: OpenAPI.Path
public let pathItem: OpenAPI.PathItem

public init(
path: OpenAPI.Path,
pathItem: OpenAPI.PathItem
) {
self.path = path
self.pathItem = pathItem
}
}

/// Get all routes for this document.
///
/// - Returns: An Array of `Routes` with the path
/// and the definition of the route.
public var routes: [Route] {
return paths.map { (path, pathItem) in .init(path: path, pathItem: pathItem) }
}
}

extension OpenAPI {
/// If the security scheme is of type "oauth2" or "openIdConnect",
/// then the value is a list of scope names required for the execution.
Expand Down
34 changes: 20 additions & 14 deletions Sources/OpenAPIKit/DocumentInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ extension OpenAPI.Document {
/// where the values are anything codable.
public var vendorExtensions: [String: AnyCodable]

public init(title: String,
description: String? = nil,
termsOfService: URL? = nil,
contact: Contact? = nil,
license: License? = nil,
version: String,
vendorExtensions: [String: AnyCodable] = [:]) {
public init(
title: String,
description: String? = nil,
termsOfService: URL? = nil,
contact: Contact? = nil,
license: License? = nil,
version: String,
vendorExtensions: [String: AnyCodable] = [:]
) {
self.title = title
self.description = description
self.termsOfService = termsOfService
Expand All @@ -57,10 +59,12 @@ extension OpenAPI.Document {
/// where the values are anything codable.
public var vendorExtensions: [String: AnyCodable]

public init(name: String? = nil,
url: URL? = nil,
email: String? = nil,
vendorExtensions: [String: AnyCodable] = [:]) {
public init(
name: String? = nil,
url: URL? = nil,
email: String? = nil,
vendorExtensions: [String: AnyCodable] = [:]
) {
self.name = name
self.url = url
self.email = email
Expand All @@ -82,9 +86,11 @@ extension OpenAPI.Document {
/// where the values are anything codable.
public var vendorExtensions: [String: AnyCodable]

public init(name: String,
url: URL? = nil,
vendorExtensions: [String: AnyCodable] = [:]) {
public init(
name: String,
url: URL? = nil,
vendorExtensions: [String: AnyCodable] = [:]
) {
self.name = name
self.url = url
self.vendorExtensions = vendorExtensions
Expand Down
10 changes: 6 additions & 4 deletions Sources/OpenAPIKit/Example.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ extension OpenAPI {
/// where the values are anything codable.
public let vendorExtensions: [String: AnyCodable]

public init(summary: String? = nil,
description: String? = nil,
value: Either<URL, AnyCodable>,
vendorExtensions: [String: AnyCodable] = [:]) {
public init(
summary: String? = nil,
description: String? = nil,
value: Either<URL, AnyCodable>,
vendorExtensions: [String: AnyCodable] = [:]
) {
self.summary = summary
self.description = description
self.value = value
Expand Down
52 changes: 28 additions & 24 deletions Sources/OpenAPIKit/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@ extension OpenAPI {
/// where the values are anything codable.
public var vendorExtensions: [String: AnyCodable]

public init(tags: [String]? = nil,
summary: String? = nil,
description: String? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
operationId: String? = nil,
parameters: Parameter.Array = [],
requestBody: OpenAPI.Request? = nil,
responses: OpenAPI.Response.Map,
deprecated: Bool = false,
security: [OpenAPI.SecurityRequirement]? = nil,
servers: [OpenAPI.Server]? = nil,
vendorExtensions: [String: AnyCodable] = [:]) {
public init(
tags: [String]? = nil,
summary: String? = nil,
description: String? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
operationId: String? = nil,
parameters: Parameter.Array = [],
requestBody: OpenAPI.Request? = nil,
responses: OpenAPI.Response.Map,
deprecated: Bool = false,
security: [OpenAPI.SecurityRequirement]? = nil,
servers: [OpenAPI.Server]? = nil,
vendorExtensions: [String: AnyCodable] = [:]
) {
self.tags = tags
self.summary = summary
self.description = description
Expand All @@ -59,18 +61,20 @@ extension OpenAPI {
}

// variadic tags
public init(tags: String...,
summary: String? = nil,
description: String? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
operationId: String? = nil,
parameters: Parameter.Array = [],
requestBody: OpenAPI.Request? = nil,
responses: OpenAPI.Response.Map,
deprecated: Bool = false,
security: [OpenAPI.SecurityRequirement]? = nil,
servers: [OpenAPI.Server]? = nil,
vendorExtensions: [String: AnyCodable] = [:]) {
public init(
tags: String...,
summary: String? = nil,
description: String? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
operationId: String? = nil,
parameters: Parameter.Array = [],
requestBody: OpenAPI.Request? = nil,
responses: OpenAPI.Response.Map,
deprecated: Bool = false,
security: [OpenAPI.SecurityRequirement]? = nil,
servers: [OpenAPI.Server]? = nil,
vendorExtensions: [String: AnyCodable] = [:]
) {
self.init(
tags: tags,
summary: summary,
Expand Down
28 changes: 15 additions & 13 deletions Sources/OpenAPIKit/PathItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,21 @@ extension OpenAPI {
/// where the values are anything codable.
public var vendorExtensions: [String: AnyCodable]

public init(summary: String? = nil,
description: String? = nil,
servers: [OpenAPI.Server]? = nil,
parameters: Parameter.Array = [],
get: Operation? = nil,
put: Operation? = nil,
post: Operation? = nil,
delete: Operation? = nil,
options: Operation? = nil,
head: Operation? = nil,
patch: Operation? = nil,
trace: Operation? = nil,
vendorExtensions: [String: AnyCodable] = [:]) {
public init(
summary: String? = nil,
description: String? = nil,
servers: [OpenAPI.Server]? = nil,
parameters: Parameter.Array = [],
get: Operation? = nil,
put: Operation? = nil,
post: Operation? = nil,
delete: Operation? = nil,
options: Operation? = nil,
head: Operation? = nil,
patch: Operation? = nil,
trace: Operation? = nil,
vendorExtensions: [String: AnyCodable] = [:]
) {
self.summary = summary
self.description = description
self.servers = servers
Expand Down
10 changes: 6 additions & 4 deletions Sources/OpenAPIKit/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ extension OpenAPI {
/// where the values are anything codable.
public var vendorExtensions: [String: AnyCodable]

public init(url: URL,
description: String? = nil,
variables: OrderedDictionary<String, Variable> = [:],
vendorExtensions: [String: AnyCodable] = [:]) {
public init(
url: URL,
description: String? = nil,
variables: OrderedDictionary<String, Variable> = [:],
vendorExtensions: [String: AnyCodable] = [:]
) {
self.url = url
self.description = description
self.variables = variables
Expand Down
37 changes: 37 additions & 0 deletions Tests/OpenAPIKitTests/DocumentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,43 @@ final class DocumentTests: XCTestCase {
)
}

func test_getRoutes() {
let pi1 = OpenAPI.PathItem(
parameters: [],
get: .init(
tags: "hi",
parameters: [],
responses: [:]
)
)

let pi2 = OpenAPI.PathItem(
get: .init(
responses: [:]
)
)

let test = OpenAPI.Document(
info: .init(title: "hi", version: "1.0"),
servers: [
.init(url: URL(string: "https://google.com")!)
],
paths: [
"/hi/there": pi1,
"/hi": pi2
],
components: .init(schemas: ["hello": .string])
)

XCTAssertEqual(
test.routes,
[
.init(path: "/hi/there", pathItem: pi1),
.init(path: "/hi", pathItem: pi2)
]
)
}

func test_existingSecuritySchemeSuccess() {
let docData =
"""
Expand Down

0 comments on commit b5217e2

Please sign in to comment.