Skip to content

Commit

Permalink
Fix double queries (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsBogeskov authored May 11, 2022
1 parent ade50f5 commit eb6b575
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
}
}

Expand Down
15 changes: 7 additions & 8 deletions Sources/SwaggerSwiftCore/Models/QueryElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 ""
}
}
}

0 comments on commit eb6b575

Please sign in to comment.