Skip to content

Commit

Permalink
Include path item parameters in filter (apple#658)
Browse files Browse the repository at this point in the history
### Motivation

When filtering by a specific operationId, any path item-level parameters
were not included (only operation-level parameters).

### Modifications

Include path item level parameters.

### Result

No errors for missing references when filtering for operations that have
a path item-level parameter with a reference.

### Test Plan

Adapted unit tests to cover this.
  • Loading branch information
czechboy0 authored Oct 25, 2024
1 parent 924cf2f commit 7b4dd6b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
1 change: 1 addition & 0 deletions Sources/_OpenAPIGeneratorCore/Hooks/FilteredDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ private extension FilteredDocumentBuilder {
guard predicate(endpoint) else { continue }
if requiredEndpoints[path] == nil { requiredEndpoints[path] = Set() }
if requiredEndpoints[path]!.insert(endpoint.method).inserted {
for parameter in originalPathItem.parameters { try includeParameter(parameter) }
try includeComponentsReferencedBy(endpoint.operation)
}
}
Expand Down
51 changes: 43 additions & 8 deletions Tests/OpenAPIGeneratorCoreTests/Hooks/Test_FilteredDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ final class Test_FilteredDocument: XCTestCase {
- name: t
paths:
/things/a:
parameters:
- $ref: '#/components/parameters/A'
get:
operationId: getA
tags:
Expand All @@ -52,6 +54,12 @@ final class Test_FilteredDocument: XCTestCase {
type: string
B:
$ref: '#/components/schemas/A'
parameters:
A:
in: query
schema:
type: string
name: A
responses:
A:
description: success
Expand All @@ -75,14 +83,16 @@ final class Test_FilteredDocument: XCTestCase {
filter: DocumentFilter(tags: ["t"]),
hasPaths: ["/things/a"],
hasOperations: ["getA"],
hasSchemas: ["A"]
hasSchemas: ["A"],
hasParameters: ["A"]
)
assert(
filtering: document,
filter: DocumentFilter(paths: ["/things/a"]),
hasPaths: ["/things/a"],
hasOperations: ["getA", "deleteA"],
hasSchemas: ["A"]
hasSchemas: ["A"],
hasParameters: ["A"]
)
assert(
filtering: document,
Expand All @@ -96,7 +106,8 @@ final class Test_FilteredDocument: XCTestCase {
filter: DocumentFilter(paths: ["/things/a", "/things/b"]),
hasPaths: ["/things/a", "/things/b"],
hasOperations: ["getA", "deleteA", "getB"],
hasSchemas: ["A", "B"]
hasSchemas: ["A", "B"],
hasParameters: ["A"]
)
assert(
filtering: document,
Expand All @@ -117,21 +128,24 @@ final class Test_FilteredDocument: XCTestCase {
filter: DocumentFilter(paths: ["/things/a"], schemas: ["B"]),
hasPaths: ["/things/a"],
hasOperations: ["getA", "deleteA"],
hasSchemas: ["A", "B"]
hasSchemas: ["A", "B"],
hasParameters: ["A"]
)
assert(
filtering: document,
filter: DocumentFilter(tags: ["t"], schemas: ["B"]),
hasPaths: ["/things/a"],
hasOperations: ["getA"],
hasSchemas: ["A", "B"]
hasSchemas: ["A", "B"],
hasParameters: ["A"]
)
assert(
filtering: document,
filter: DocumentFilter(operations: ["deleteA"]),
hasPaths: ["/things/a"],
hasOperations: ["deleteA"],
hasSchemas: []
hasSchemas: [],
hasParameters: ["A"]
)
}

Expand All @@ -141,6 +155,7 @@ final class Test_FilteredDocument: XCTestCase {
hasPaths paths: [OpenAPI.Path.RawValue],
hasOperations operationIDs: [String],
hasSchemas schemas: [String],
hasParameters parameters: [String] = [],
file: StaticString = #filePath,
line: UInt = #line
) {
Expand All @@ -149,11 +164,31 @@ final class Test_FilteredDocument: XCTestCase {
XCTFail("Filter threw error: \(error)", file: file, line: line)
return
}
XCTAssertUnsortedEqual(filteredDocument.paths.keys.map(\.rawValue), paths, file: file, line: line)
XCTAssertUnsortedEqual(filteredDocument.allOperationIds, operationIDs, file: file, line: line)
XCTAssertUnsortedEqual(
filteredDocument.paths.keys.map(\.rawValue),
paths,
"Paths don't match",
file: file,
line: line
)
XCTAssertUnsortedEqual(
filteredDocument.allOperationIds,
operationIDs,
"Operations don't match",
file: file,
line: line
)
XCTAssertUnsortedEqual(
filteredDocument.components.schemas.keys.map(\.rawValue),
schemas,
"Schemas don't match",
file: file,
line: line
)
XCTAssertUnsortedEqual(
filteredDocument.components.parameters.keys.map(\.rawValue),
parameters,
"Parameters don't match",
file: file,
line: line
)
Expand Down

0 comments on commit 7b4dd6b

Please sign in to comment.