-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for filtering OpenAPI document (#319)
### Motivation When generating client code, Swift OpenAPI Generator generates code for the entire OpenAPI document, even if the user only makes use of a subset of its types and operations. Generating code that is unused constitutes overhead for the adopter: - The overhead of generating code for unused types and operations - The overhead of compiling the generated code - The overhead of unused code in the users codebase (AOT generation) This is particularly noticeable when working with a small subset of a large API, which can result in O(100k) lines of unused code and long generation and compile times. For a more detailed motivation and design, see the proposal in #303. ### Modifications - Add document filter to the generator config. - Run filter as a post-transition hook in the generator pipeline after parsing the document. - Provide a CLI command that outputs the filtered document to stdout. ### Result Users can filter a document before code-generation. For large APIs, this can result in >90% speedup (see proposal). ### Test Plan - Unit tests. --------- Signed-off-by: Si Beaumont <beaumont@apple.com> Co-authored-by: Honza Dvorsky <honza@apple.com>
- Loading branch information
1 parent
3f8542b
commit 4c8ed5c
Showing
12 changed files
with
935 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftOpenAPIGenerator open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@preconcurrency import OpenAPIKit | ||
|
||
/// Rules used to filter an OpenAPI document. | ||
public struct DocumentFilter: Codable, Sendable { | ||
|
||
/// Operations with these operation IDs will be included in the filter. | ||
public var operations: [String]? | ||
|
||
/// Operations tagged with these tags will be included in the filter. | ||
public var tags: [String]? | ||
|
||
/// These paths will be included in the filter. | ||
public var paths: [OpenAPI.Path]? | ||
|
||
/// These (additional) schemas will be included in the filter. | ||
/// | ||
/// These schemas are included in addition to the transitive closure of schema dependencies of | ||
/// the paths included in the filter. | ||
public var schemas: [String]? | ||
|
||
/// Create a new DocumentFilter. | ||
/// | ||
/// - Parameters: | ||
/// - operations: Operations with these IDs will be included in the filter. | ||
/// - tags: Operations tagged with these tags will be included in the filter. | ||
/// - paths: These paths will be included in the filter. | ||
/// - schemas: These (additional) schemas will be included in the filter. | ||
public init( | ||
operations: [String] = [], | ||
tags: [String] = [], | ||
paths: [OpenAPI.Path] = [], | ||
schemas: [String] = [] | ||
) { | ||
self.operations = operations | ||
self.tags = tags | ||
self.paths = paths | ||
self.schemas = schemas | ||
} | ||
|
||
/// Filter an OpenAPI document. | ||
/// | ||
/// - Parameter document: The OpenAPI document to filter. | ||
/// - Returns: The filtered document. | ||
/// - Throws: If any requested document components do not exist in the original document. | ||
/// - Throws: If any dependencies of the requested document components cannot be resolved. | ||
public func filter(_ document: OpenAPI.Document) throws -> OpenAPI.Document { | ||
var builder = FilteredDocumentBuilder(document: document) | ||
for tag in tags ?? [] { | ||
try builder.includeOperations(tagged: tag) | ||
} | ||
for operationID in operations ?? [] { | ||
try builder.includeOperation(operationID: operationID) | ||
} | ||
for path in paths ?? [] { | ||
try builder.includePath(path) | ||
} | ||
for schema in schemas ?? [] { | ||
try builder.includeSchema(schema) | ||
} | ||
return try builder.filter() | ||
} | ||
} |
Oops, something went wrong.