|
| 1 | +// |
| 2 | +// DereferencedDocument.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Mathew Polzin on 6/19/20. |
| 6 | +// |
| 7 | + |
| 8 | +/// An `OpenAPI.Document` type that guarantees |
| 9 | +/// its `paths` and `security` are inlined instead of |
| 10 | +/// referenced. You create a `DereferencedDocument` |
| 11 | +/// by calling the `locallyDereferenced()` method |
| 12 | +/// on an `OpenAPI.Document`. |
| 13 | +@dynamicMemberLookup |
| 14 | +public struct DereferencedDocument: Equatable { |
| 15 | + /// The original OpenAPI document prior to being |
| 16 | + /// dereferenced. |
| 17 | + public let underlyingDocument: OpenAPI.Document |
| 18 | + |
| 19 | + /// This property maps the path of each route (`OpenAPI.Path`) to the |
| 20 | + /// documentation for that route (`DereferencedPathItem`). |
| 21 | + public let paths: DereferencedPathItem.Map |
| 22 | + |
| 23 | + /// A declaration of which security mechanisms can be used across the API. |
| 24 | + /// |
| 25 | + /// The list of values includes alternative security requirement objects that can |
| 26 | + /// be used. Only one of the security requirement objects need to be satisfied |
| 27 | + /// to authorize a request. Individual operations can override this definition. |
| 28 | + /// |
| 29 | + /// To make security optional, an empty security requirement can be included |
| 30 | + /// in the array. |
| 31 | + public let security: [DereferencedSecurityRequirement] |
| 32 | + |
| 33 | + public subscript<T>(dynamicMember path: KeyPath<OpenAPI.Document, T>) -> T { |
| 34 | + return underlyingDocument[keyPath: path] |
| 35 | + } |
| 36 | + |
| 37 | + /// Create a `DereferencedDocument` if all references in the |
| 38 | + /// document can be found in its Components Object. |
| 39 | + /// |
| 40 | + /// - Important: This only attempts to dereference components in the |
| 41 | + /// Components Object. Any references pointing to other files or other |
| 42 | + /// locations in the same file will `throw`. |
| 43 | + /// |
| 44 | + /// - Throws: `ReferenceError.cannotLookupRemoteReference` or |
| 45 | + /// `MissingReferenceError.referenceMissingOnLookup(name:)` depending |
| 46 | + /// on whether an unresolvable reference points to another file or just points to a |
| 47 | + /// component in the same file that cannot be found in the Components Object. |
| 48 | + internal init(_ document: OpenAPI.Document) throws { |
| 49 | + self.paths = try document.paths.mapValues { |
| 50 | + try DereferencedPathItem( |
| 51 | + $0, |
| 52 | + resolvingIn: document.components |
| 53 | + ) |
| 54 | + } |
| 55 | + self.security = try document.security.map { |
| 56 | + try DereferencedSecurityRequirement( |
| 57 | + $0, |
| 58 | + resolvingIn: document.components |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + self.underlyingDocument = document |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +extension DereferencedDocument { |
| 67 | + /// The pairing of a path and the path item that describes the |
| 68 | + /// route at that path. |
| 69 | + public struct Route: Equatable { |
| 70 | + public let path: OpenAPI.Path |
| 71 | + public let pathItem: DereferencedPathItem |
| 72 | + |
| 73 | + public init( |
| 74 | + path: OpenAPI.Path, |
| 75 | + pathItem: DereferencedPathItem |
| 76 | + ) { |
| 77 | + self.path = path |
| 78 | + self.pathItem = pathItem |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /// Get an array of all routes in the document. A route is |
| 83 | + /// the pairing of a path and the path item that describes the |
| 84 | + /// route at that path. |
| 85 | + public var routes: [Route] { |
| 86 | + return paths.map { (path, pathItem) in .init(path: path, pathItem: pathItem) } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +extension DereferencedDocument { |
| 91 | + /// Resolve the document's routes and endpoints. |
| 92 | + /// |
| 93 | + /// OpenAPI allows routes and endpoints to take on things like |
| 94 | + /// servers, parameters, and security requirements from |
| 95 | + /// various different locations in the `OpenAPI.Document`. A |
| 96 | + /// `ResolvedDocument` offers access to canonical routes |
| 97 | + /// and endpoints that collect and self-contain all necessary |
| 98 | + /// information about the given component. |
| 99 | + /// |
| 100 | + /// **Example** |
| 101 | + /// |
| 102 | + /// A particular `GET` endpoint takes its security |
| 103 | + /// requirements from the root OpenAPI `security` |
| 104 | + /// array, it takes a path item parameter from the `PathItem` it |
| 105 | + /// resides within, and it defines an additional query parameter. |
| 106 | + /// |
| 107 | + /// The `ResolvedEndpoint` exposed by the `ResolvedDocument` |
| 108 | + /// will have the inherited security in its `security` array and it will have |
| 109 | + /// both the path and query parameters in its `parameters` array. |
| 110 | + public func resolved() -> ResolvedDocument { |
| 111 | + return ResolvedDocument(dereferencedDocument: self) |
| 112 | + } |
| 113 | +} |
0 commit comments