-
Notifications
You must be signed in to change notification settings - Fork 49
[Runtime] Support unexploded query items #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ed40bd4
d938f75
407ee98
4d67877
40de625
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,6 +93,33 @@ extension Array where Element == HeaderField { | |
} | ||
} | ||
|
||
extension ParameterStyle { | ||
|
||
/// Returns the parameter style and explode parameter that should be used | ||
/// based on the provided inputs, taking defaults into considerations. | ||
/// - Parameters: | ||
/// - style: The provided parameter style, if any. | ||
/// - explode: The provided explode value, if any. | ||
/// - Throws: For an unsupported input combination. | ||
static func resolvedQueryStyleAndExplode( | ||
name: String, | ||
style: ParameterStyle?, | ||
explode: Bool? | ||
) throws -> (ParameterStyle, Bool) { | ||
let resolvedStyle = style ?? .defaultForQueryItems | ||
let resolvedExplode = explode ?? ParameterStyle.defaultExplodeFor(forStyle: resolvedStyle) | ||
guard resolvedStyle == .form else { | ||
throw RuntimeError.unsupportedParameterStyle( | ||
name: name, | ||
location: .query, | ||
style: resolvedStyle, | ||
explode: resolvedExplode | ||
) | ||
} | ||
return (resolvedStyle, resolvedExplode) | ||
} | ||
} | ||
|
||
extension Converter { | ||
|
||
// MARK: Common functions for Converter's SPI helper methods | ||
|
@@ -259,36 +286,68 @@ extension Converter { | |
|
||
func setQueryItem<T>( | ||
in request: inout Request, | ||
style: ParameterStyle?, | ||
explode: Bool?, | ||
name: String, | ||
value: T?, | ||
convert: (T) throws -> String | ||
) throws { | ||
guard let value else { | ||
return | ||
} | ||
request.addQueryItem(name: name, value: try convert(value)) | ||
let (_, resolvedExplode) = try ParameterStyle.resolvedQueryStyleAndExplode( | ||
name: name, | ||
style: style, | ||
explode: explode | ||
) | ||
request.addQueryItem( | ||
name: name, | ||
value: try convert(value), | ||
explode: resolvedExplode | ||
) | ||
} | ||
|
||
func setQueryItems<T>( | ||
in request: inout Request, | ||
style: ParameterStyle?, | ||
explode: Bool?, | ||
name: String, | ||
values: [T]?, | ||
convert: (T) throws -> String | ||
) throws { | ||
guard let values else { | ||
return | ||
} | ||
let (_, resolvedExplode) = try ParameterStyle.resolvedQueryStyleAndExplode( | ||
name: name, | ||
style: style, | ||
explode: explode | ||
) | ||
for value in values { | ||
request.addQueryItem(name: name, value: try convert(value)) | ||
request.addQueryItem( | ||
name: name, | ||
value: try convert(value), | ||
explode: resolvedExplode | ||
) | ||
} | ||
} | ||
|
||
func getOptionalQueryItem<T>( | ||
in queryParameters: [URLQueryItem], | ||
style: ParameterStyle?, | ||
explode: Bool?, | ||
name: String, | ||
as type: T.Type, | ||
convert: (String) throws -> T | ||
) throws -> T? { | ||
// Even though the return value isn't used, the validation | ||
// in the method is important for consistently handling | ||
// style+explode combinations in all the helper functions. | ||
let (_, _) = try ParameterStyle.resolvedQueryStyleAndExplode( | ||
name: name, | ||
style: style, | ||
explode: explode | ||
) | ||
guard | ||
let untypedValue = | ||
queryParameters | ||
|
@@ -301,13 +360,17 @@ extension Converter { | |
|
||
func getRequiredQueryItem<T>( | ||
in queryParameters: [URLQueryItem], | ||
style: ParameterStyle?, | ||
explode: Bool?, | ||
name: String, | ||
as type: T.Type, | ||
convert: (String) throws -> T | ||
) throws -> T { | ||
guard | ||
let value = try getOptionalQueryItem( | ||
in: queryParameters, | ||
style: style, | ||
explode: explode, | ||
name: name, | ||
as: type, | ||
convert: convert | ||
|
@@ -320,23 +383,49 @@ extension Converter { | |
|
||
func getOptionalQueryItems<T>( | ||
in queryParameters: [URLQueryItem], | ||
style: ParameterStyle?, | ||
explode: Bool?, | ||
name: String, | ||
as type: [T].Type, | ||
convert: (String) throws -> T | ||
) throws -> [T]? { | ||
let untypedValues = queryParameters.filter { $0.name == name } | ||
return try untypedValues.map { try convert($0.value ?? "") } | ||
let (_, resolvedExplode) = try ParameterStyle.resolvedQueryStyleAndExplode( | ||
name: name, | ||
style: style, | ||
explode: explode | ||
) | ||
let untypedValues = | ||
queryParameters | ||
.filter { $0.name == name } | ||
.map { $0.value ?? "" } | ||
// If explode is false, some of the items might have multiple | ||
// comma-separate values, so we need to split them here. | ||
let processedValues: [String] | ||
if resolvedExplode { | ||
processedValues = untypedValues | ||
} else { | ||
processedValues = untypedValues.flatMap { multiValue in | ||
multiValue | ||
.split(separator: ",", omittingEmptySubsequences: false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry for the late comment, but in theory I assume it would be possible to have values with commas in them. Would the commas be escaped or percent-encoded in this case? Just want to make sure this split doesn't split the string at incorrect places. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah exactly, if the comma is part of the value, it is percent escaped. |
||
.map(String.init) | ||
} | ||
} | ||
return try processedValues.map(convert) | ||
} | ||
|
||
func getRequiredQueryItems<T>( | ||
in queryParameters: [URLQueryItem], | ||
style: ParameterStyle?, | ||
explode: Bool?, | ||
name: String, | ||
as type: [T].Type, | ||
convert: (String) throws -> T | ||
) throws -> [T] { | ||
guard | ||
let values = try getOptionalQueryItems( | ||
in: queryParameters, | ||
style: style, | ||
explode: explode, | ||
name: name, | ||
as: type, | ||
convert: convert | ||
|
Uh oh!
There was an error while loading. Please reload this page.