-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
155bd4c
commit 15423b4
Showing
2 changed files
with
157 additions
and
3 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
133 changes: 133 additions & 0 deletions
133
Tests/OpenAPIGeneratorCoreTests/Translator/Operations/Test_OperationDescription.swift
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,133 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
import OpenAPIKit30 | ||
import XCTest | ||
@testable import _OpenAPIGeneratorCore | ||
|
||
final class Test_OperationDescription: Test_Core { | ||
|
||
func testAllParameters_duplicates_retainOnlyOperationParameters() throws { | ||
let pathLevelParameter = UnresolvedParameter.b( | ||
OpenAPI.Parameter( | ||
name: "test", | ||
context: .query(required: false), | ||
schema: .integer | ||
) | ||
) | ||
let operationLevelParameter = UnresolvedParameter.b( | ||
OpenAPI.Parameter( | ||
name: "test", | ||
context: .query(required: false), | ||
schema: .string | ||
) | ||
) | ||
|
||
let pathItem = OpenAPI.PathItem( | ||
parameters: [pathLevelParameter], | ||
get: .init( | ||
parameters: [operationLevelParameter], | ||
requestBody: .b(.init(content: [:])), | ||
responses: [:] | ||
), | ||
vendorExtensions: [:] | ||
) | ||
let allParameters = try _test(pathItem) | ||
|
||
XCTAssertEqual(allParameters, [operationLevelParameter]) | ||
} | ||
|
||
func testAllParameters_duplicates_keepsDuplicatesFromDifferentLocation() throws { | ||
let pathLevelParameter = UnresolvedParameter.b( | ||
OpenAPI.Parameter( | ||
name: "test", | ||
context: .query(required: false), | ||
schema: .integer | ||
) | ||
) | ||
let operationLevelParameter = UnresolvedParameter.b( | ||
OpenAPI.Parameter( | ||
name: "test", | ||
context: .path, | ||
schema: .string | ||
) | ||
) | ||
|
||
let pathItem = OpenAPI.PathItem( | ||
parameters: [pathLevelParameter], | ||
get: .init( | ||
parameters: [operationLevelParameter], | ||
requestBody: .b(.init(content: [:])), | ||
responses: [:] | ||
), | ||
vendorExtensions: [:] | ||
) | ||
let allParameters = try _test(pathItem) | ||
|
||
XCTAssertEqual(allParameters, [pathLevelParameter, operationLevelParameter]) | ||
} | ||
|
||
func testAllParameters_duplicates_ordering() throws { | ||
let pathLevelParameter = UnresolvedParameter.b( | ||
OpenAPI.Parameter( | ||
name: "test1", | ||
context: .query(required: false), | ||
schema: .integer | ||
) | ||
) | ||
let duplicatedParameter = UnresolvedParameter.b( | ||
OpenAPI.Parameter( | ||
name: "test2", | ||
context: .query(required: false), | ||
schema: .integer | ||
) | ||
) | ||
let operationLevelParameter = UnresolvedParameter.b( | ||
OpenAPI.Parameter( | ||
name: "test3", | ||
context: .query(required: false), | ||
schema: .string | ||
) | ||
) | ||
|
||
let pathItem = OpenAPI.PathItem( | ||
parameters: [pathLevelParameter, duplicatedParameter], | ||
get: .init( | ||
parameters: [duplicatedParameter, operationLevelParameter], | ||
requestBody: .b(.init(content: [:])), | ||
responses: [:] | ||
), | ||
vendorExtensions: [:] | ||
) | ||
let allParameters = try _test(pathItem) | ||
|
||
XCTAssertEqual(allParameters, [pathLevelParameter, duplicatedParameter, operationLevelParameter]) | ||
} | ||
|
||
private func _test(_ pathItem: OpenAPI.PathItem) throws -> [UnresolvedParameter] { | ||
guard let endpoint = pathItem.endpoints.first else { | ||
XCTFail("Unable to retrieve the path item first endpoint.") | ||
return [] | ||
} | ||
|
||
let operationDescription = OperationDescription( | ||
path: .init(["/test"]), | ||
endpoint: endpoint, | ||
pathParameters: pathItem.parameters, | ||
components: .init(), | ||
asSwiftSafeName: { $0 } | ||
) | ||
|
||
return try operationDescription.allParameters | ||
} | ||
} |