|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftOpenAPIGenerator open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +@preconcurrency import OpenAPIKit |
| 16 | + |
| 17 | +/// Rules used to filter an OpenAPI document. |
| 18 | +public struct DocumentFilter: Codable, Sendable { |
| 19 | + |
| 20 | + /// Operations with these operation IDs will be included in the filter. |
| 21 | + public var operations: [String]? |
| 22 | + |
| 23 | + /// Operations tagged with these tags will be included in the filter. |
| 24 | + public var tags: [String]? |
| 25 | + |
| 26 | + /// These paths will be included in the filter. |
| 27 | + public var paths: [OpenAPI.Path]? |
| 28 | + |
| 29 | + /// These (additional) schemas will be included in the filter. |
| 30 | + /// |
| 31 | + /// These schemas are included in addition to the transitive closure of schema dependencies of |
| 32 | + /// the paths included in the filter. |
| 33 | + public var schemas: [String]? |
| 34 | + |
| 35 | + /// Create a new DocumentFilter. |
| 36 | + /// |
| 37 | + /// - Parameters: |
| 38 | + /// - operations: Operations with these IDs will be included in the filter. |
| 39 | + /// - tags: Operations tagged with these tags will be included in the filter. |
| 40 | + /// - paths: These paths will be included in the filter. |
| 41 | + /// - schemas: These (additional) schemas will be included in the filter. |
| 42 | + public init( |
| 43 | + operations: [String] = [], |
| 44 | + tags: [String] = [], |
| 45 | + paths: [OpenAPI.Path] = [], |
| 46 | + schemas: [String] = [] |
| 47 | + ) { |
| 48 | + self.operations = operations |
| 49 | + self.tags = tags |
| 50 | + self.paths = paths |
| 51 | + self.schemas = schemas |
| 52 | + } |
| 53 | + |
| 54 | + /// Filter an OpenAPI document. |
| 55 | + /// |
| 56 | + /// - Parameter document: The OpenAPI document to filter. |
| 57 | + /// - Returns: The filtered document. |
| 58 | + /// - Throws: If any requested document components do not exist in the original document. |
| 59 | + /// - Throws: If any dependencies of the requested document components cannot be resolved. |
| 60 | + public func filter(_ document: OpenAPI.Document) throws -> OpenAPI.Document { |
| 61 | + var builder = FilteredDocumentBuilder(document: document) |
| 62 | + for tag in tags ?? [] { |
| 63 | + try builder.includeOperations(tagged: tag) |
| 64 | + } |
| 65 | + for operationID in operations ?? [] { |
| 66 | + try builder.includeOperation(operationID: operationID) |
| 67 | + } |
| 68 | + for path in paths ?? [] { |
| 69 | + try builder.includePath(path) |
| 70 | + } |
| 71 | + for schema in schemas ?? [] { |
| 72 | + try builder.includeSchema(schema) |
| 73 | + } |
| 74 | + return try builder.filter() |
| 75 | + } |
| 76 | +} |
0 commit comments