Skip to content

Commit

Permalink
Retain operation level parameters over path level ones (#183)
Browse files Browse the repository at this point in the history
### 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
3 people authored Aug 11, 2023
1 parent 155bd4c commit 15423b4
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = []

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.
Expand Down
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
}
}

0 comments on commit 15423b4

Please sign in to comment.