From 15423b4661192fb052369648bb00a546a4835d97 Mon Sep 17 00:00:00 2001 From: Arthur Crocquevieille Date: Fri, 11 Aug 2023 15:06:44 +0200 Subject: [PATCH] Retain operation level parameters over path level ones (#183) ### Motivation Fixes https://github.com/apple/swift-openapi-generator/issues/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 Co-authored-by: Honza Dvorsky --- .../Operations/OperationDescription.swift | 27 +++- .../Test_OperationDescription.swift | 133 ++++++++++++++++++ 2 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 Tests/OpenAPIGeneratorCoreTests/Translator/Operations/Test_OperationDescription.swift diff --git a/Sources/_OpenAPIGeneratorCore/Translator/Operations/OperationDescription.swift b/Sources/_OpenAPIGeneratorCore/Translator/Operations/OperationDescription.swift index 54707996..b21f63fc 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/Operations/OperationDescription.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/Operations/OperationDescription.swift @@ -144,10 +144,31 @@ extension OperationDescription { ) } - /// Returns parameters from both the path item level - /// and the operation level. + /// Merged parameters from both the path item level and the operation level. + /// If duplicate parameters exist, only the parameters from the operation level are preserved. + /// + /// - Returns: An array of merged path item and operation level parameters without duplicates. + /// - Throws: When an invalid JSON reference is found. var allParameters: [UnresolvedParameter] { - pathParameters + operation.parameters + get throws { + var mergedParameters: [UnresolvedParameter] = [] + var uniqueIdentifiers: Set = [] + + let allParameters = pathParameters + operation.parameters + for parameter in allParameters.reversed() { + let resolvedParameter = try parameter.resolve(in: components) + let identifier = resolvedParameter.location.rawValue + ":" + resolvedParameter.name + + guard !uniqueIdentifiers.contains(identifier) else { + continue + } + + mergedParameters.append(parameter) + uniqueIdentifiers.insert(identifier) + } + + return mergedParameters.reversed() + } } /// Returns all parameters by resolving any parameter references first. diff --git a/Tests/OpenAPIGeneratorCoreTests/Translator/Operations/Test_OperationDescription.swift b/Tests/OpenAPIGeneratorCoreTests/Translator/Operations/Test_OperationDescription.swift new file mode 100644 index 00000000..8f6824af --- /dev/null +++ b/Tests/OpenAPIGeneratorCoreTests/Translator/Operations/Test_OperationDescription.swift @@ -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 + } +}