Skip to content

Commit 15423b4

Browse files
arthurcroczechboy0
andauthored
Retain operation level parameters over path level ones (#183)
### Motivation Fixes #168. ### Modifications Previously, an operation's parameters were returned as a concatenation of the parameters from the path item level with those at operation level. However, this is incorrect according to https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#fixed-fields-7. > A list of parameters that are applicable for all the operations described under this path. These parameters can be overridden at the operation level, but cannot be removed there. The list MUST NOT include duplicated parameters. With this change, we are merging the two arrays of parameters using a unique identifier of the location + name of parameters. If duplicate parameters exist, only the parameters from the operation level are preserved. ### Result After this change, an operation's parameters won't contain any duplicate. ### Test Plan Add tests for `OperationDescription.allParameters` at `Tests/OpenAPIGeneratorCoreTests/Translator/Operations/Test_OperationDescription.swift`. --------- Co-authored-by: Honza Dvorsky <[email protected]> Co-authored-by: Honza Dvorsky <[email protected]>
1 parent 155bd4c commit 15423b4

File tree

2 files changed

+157
-3
lines changed

2 files changed

+157
-3
lines changed

Sources/_OpenAPIGeneratorCore/Translator/Operations/OperationDescription.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,31 @@ extension OperationDescription {
144144
)
145145
}
146146

147-
/// Returns parameters from both the path item level
148-
/// and the operation level.
147+
/// Merged parameters from both the path item level and the operation level.
148+
/// If duplicate parameters exist, only the parameters from the operation level are preserved.
149+
///
150+
/// - Returns: An array of merged path item and operation level parameters without duplicates.
151+
/// - Throws: When an invalid JSON reference is found.
149152
var allParameters: [UnresolvedParameter] {
150-
pathParameters + operation.parameters
153+
get throws {
154+
var mergedParameters: [UnresolvedParameter] = []
155+
var uniqueIdentifiers: Set<String> = []
156+
157+
let allParameters = pathParameters + operation.parameters
158+
for parameter in allParameters.reversed() {
159+
let resolvedParameter = try parameter.resolve(in: components)
160+
let identifier = resolvedParameter.location.rawValue + ":" + resolvedParameter.name
161+
162+
guard !uniqueIdentifiers.contains(identifier) else {
163+
continue
164+
}
165+
166+
mergedParameters.append(parameter)
167+
uniqueIdentifiers.insert(identifier)
168+
}
169+
170+
return mergedParameters.reversed()
171+
}
151172
}
152173

153174
/// Returns all parameters by resolving any parameter references first.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
import OpenAPIKit30
15+
import XCTest
16+
@testable import _OpenAPIGeneratorCore
17+
18+
final class Test_OperationDescription: Test_Core {
19+
20+
func testAllParameters_duplicates_retainOnlyOperationParameters() throws {
21+
let pathLevelParameter = UnresolvedParameter.b(
22+
OpenAPI.Parameter(
23+
name: "test",
24+
context: .query(required: false),
25+
schema: .integer
26+
)
27+
)
28+
let operationLevelParameter = UnresolvedParameter.b(
29+
OpenAPI.Parameter(
30+
name: "test",
31+
context: .query(required: false),
32+
schema: .string
33+
)
34+
)
35+
36+
let pathItem = OpenAPI.PathItem(
37+
parameters: [pathLevelParameter],
38+
get: .init(
39+
parameters: [operationLevelParameter],
40+
requestBody: .b(.init(content: [:])),
41+
responses: [:]
42+
),
43+
vendorExtensions: [:]
44+
)
45+
let allParameters = try _test(pathItem)
46+
47+
XCTAssertEqual(allParameters, [operationLevelParameter])
48+
}
49+
50+
func testAllParameters_duplicates_keepsDuplicatesFromDifferentLocation() throws {
51+
let pathLevelParameter = UnresolvedParameter.b(
52+
OpenAPI.Parameter(
53+
name: "test",
54+
context: .query(required: false),
55+
schema: .integer
56+
)
57+
)
58+
let operationLevelParameter = UnresolvedParameter.b(
59+
OpenAPI.Parameter(
60+
name: "test",
61+
context: .path,
62+
schema: .string
63+
)
64+
)
65+
66+
let pathItem = OpenAPI.PathItem(
67+
parameters: [pathLevelParameter],
68+
get: .init(
69+
parameters: [operationLevelParameter],
70+
requestBody: .b(.init(content: [:])),
71+
responses: [:]
72+
),
73+
vendorExtensions: [:]
74+
)
75+
let allParameters = try _test(pathItem)
76+
77+
XCTAssertEqual(allParameters, [pathLevelParameter, operationLevelParameter])
78+
}
79+
80+
func testAllParameters_duplicates_ordering() throws {
81+
let pathLevelParameter = UnresolvedParameter.b(
82+
OpenAPI.Parameter(
83+
name: "test1",
84+
context: .query(required: false),
85+
schema: .integer
86+
)
87+
)
88+
let duplicatedParameter = UnresolvedParameter.b(
89+
OpenAPI.Parameter(
90+
name: "test2",
91+
context: .query(required: false),
92+
schema: .integer
93+
)
94+
)
95+
let operationLevelParameter = UnresolvedParameter.b(
96+
OpenAPI.Parameter(
97+
name: "test3",
98+
context: .query(required: false),
99+
schema: .string
100+
)
101+
)
102+
103+
let pathItem = OpenAPI.PathItem(
104+
parameters: [pathLevelParameter, duplicatedParameter],
105+
get: .init(
106+
parameters: [duplicatedParameter, operationLevelParameter],
107+
requestBody: .b(.init(content: [:])),
108+
responses: [:]
109+
),
110+
vendorExtensions: [:]
111+
)
112+
let allParameters = try _test(pathItem)
113+
114+
XCTAssertEqual(allParameters, [pathLevelParameter, duplicatedParameter, operationLevelParameter])
115+
}
116+
117+
private func _test(_ pathItem: OpenAPI.PathItem) throws -> [UnresolvedParameter] {
118+
guard let endpoint = pathItem.endpoints.first else {
119+
XCTFail("Unable to retrieve the path item first endpoint.")
120+
return []
121+
}
122+
123+
let operationDescription = OperationDescription(
124+
path: .init(["/test"]),
125+
endpoint: endpoint,
126+
pathParameters: pathItem.parameters,
127+
components: .init(),
128+
asSwiftSafeName: { $0 }
129+
)
130+
131+
return try operationDescription.allParameters
132+
}
133+
}

0 commit comments

Comments
 (0)