From eb6b575c00c34601dc27cbd385b4c99d43942038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20B=C3=B8geskov?= Date: Wed, 11 May 2022 13:33:24 +0200 Subject: [PATCH] Fix double queries (#92) --- .../APIRequestFactory.swift | 57 ++++--------------- .../Models/QueryElement.swift | 15 +++-- 2 files changed, 19 insertions(+), 53 deletions(-) diff --git a/Sources/SwaggerSwiftCore/API Request Factory/APIRequestFactory.swift b/Sources/SwaggerSwiftCore/API Request Factory/APIRequestFactory.swift index 8ed411b..420f4a3 100644 --- a/Sources/SwaggerSwiftCore/API Request Factory/APIRequestFactory.swift +++ b/Sources/SwaggerSwiftCore/API Request Factory/APIRequestFactory.swift @@ -75,7 +75,7 @@ public struct APIRequestFactory { } } - let queryItems = resolveQueries(forOperation: operation, parameters: allParameters, swagger: swagger) + let queryItems = resolveQueries(parameters: allParameters, swagger: swagger) let apiResponseTypes = apiResponseTypeFactory.make(forResponses: responses, forHTTPMethod: httpMethod, @@ -110,12 +110,16 @@ public struct APIRequestFactory { /// - parameters: the total set of parameters available to the api request /// - swagger: the swagger spec /// - Returns: the list of query elements that should be set in the request - private func resolveQueries(forOperation operation: SwaggerSwiftML.Operation, parameters: [Parameter], swagger: Swagger) -> [QueryElement] { - let operationParameters: [Parameter] = (operation.parameters ?? []).map { - swagger.findParameter(node: $0) - } + parameters + private func resolveQueries(parameters: [Parameter], swagger: Swagger) -> [QueryElement] { + let queryParameters = parameters.filter { + if case ParameterLocation.query = $0.location { + return true + } else { + return false + } + } - let queries: [QueryElement] = operationParameters.compactMap { + let queries: [QueryElement] = queryParameters.compactMap { guard case ParameterLocation.query = $0.location else { return nil } @@ -175,45 +179,8 @@ public struct APIRequestFactory { valueType: .default ) } - case .header: - return QueryElement( - fieldName: $0.name, - fieldValue: $0.name.camelized, - isOptional: $0.required == false, - valueType: .default - ) - case .path(let type): - switch type { - case .string(_, let enumValues, _, _, _): - let valueType: QueryElement.ValueType = (enumValues?.count ?? 0) > 0 ? .enum : .default - return QueryElement( - fieldName: $0.name, - fieldValue: $0.name.camelized, - isOptional: $0.required == false, - valueType: valueType - ) - default: - return QueryElement( - fieldName: $0.name, - fieldValue: $0.name.camelized, - isOptional: $0.required == false, - valueType: .default - ) - } - case .formData: - return QueryElement( - fieldName: $0.name, - fieldValue: $0.name.camelized, - isOptional: $0.required == false, - valueType: .default - ) - case .body: - return QueryElement( - fieldName: $0.name, - fieldValue: $0.name.camelized, - isOptional: $0.required == false, - valueType: .default - ) + default: + fatalError("This should not happen") } } diff --git a/Sources/SwaggerSwiftCore/Models/QueryElement.swift b/Sources/SwaggerSwiftCore/Models/QueryElement.swift index 714d1e3..2289265 100644 --- a/Sources/SwaggerSwiftCore/Models/QueryElement.swift +++ b/Sources/SwaggerSwiftCore/Models/QueryElement.swift @@ -14,6 +14,7 @@ struct QueryElement { extension QueryElement { func toString() -> String { let fieldName = self.fieldName.camelized + if self.isOptional { let fieldValue: String switch self.valueType { @@ -50,18 +51,16 @@ extension QueryElement { extension Sequence where Element == QueryElement { func toQueryItems() -> String { - if self.underestimatedCount > 0 { - let queryItems = self.map { - $0.toString() - }.joined(separator: "\n") + guard self.underestimatedCount > 0 else { return "" } - return """ + let queryItems = self.map { + $0.toString() + }.joined(separator: "\n") + + return """ var queryItems = [URLQueryItem]() \(queryItems) urlComponents.queryItems = queryItems """ - } else { - return "" - } } }