Skip to content

[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

Merged
merged 5 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Sources/OpenAPIRuntime/Conversion/Converter+Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ extension Converter {
// | client | set | request query | text | string-convertible | both | setQueryItemAsText |
public func setQueryItemAsText<T: _StringConvertible>(
in request: inout Request,
style: ParameterStyle?,
explode: Bool?,
name: String,
value: T?
) throws {
try setQueryItem(
in: &request,
style: style,
explode: explode,
name: name,
value: value,
convert: convertStringConvertibleToText
Expand All @@ -50,11 +54,15 @@ extension Converter {
// | client | set | request query | text | array of string-convertibles | both | setQueryItemAsText |
public func setQueryItemAsText<T: _StringConvertible>(
in request: inout Request,
style: ParameterStyle?,
explode: Bool?,
name: String,
value: [T]?
) throws {
try setQueryItems(
in: &request,
style: style,
explode: explode,
name: name,
values: value,
convert: convertStringConvertibleToText
Expand All @@ -64,11 +72,15 @@ extension Converter {
// | client | set | request query | text | date | both | setQueryItemAsText |
public func setQueryItemAsText(
in request: inout Request,
style: ParameterStyle?,
explode: Bool?,
name: String,
value: Date?
) throws {
try setQueryItem(
in: &request,
style: style,
explode: explode,
name: name,
value: value,
convert: convertDateToText
Expand All @@ -78,11 +90,15 @@ extension Converter {
// | client | set | request query | text | array of dates | both | setQueryItemAsText |
public func setQueryItemAsText(
in request: inout Request,
style: ParameterStyle?,
explode: Bool?,
name: String,
value: [Date]?
) throws {
try setQueryItems(
in: &request,
style: style,
explode: explode,
name: name,
values: value,
convert: convertDateToText
Expand Down
32 changes: 32 additions & 0 deletions Sources/OpenAPIRuntime/Conversion/Converter+Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ public extension Converter {
// | server | get | request query | text | string-convertible | optional | getOptionalQueryItemAsText |
func getOptionalQueryItemAsText<T: _StringConvertible>(
in queryParameters: [URLQueryItem],
style: ParameterStyle?,
explode: Bool?,
name: String,
as type: T.Type
) throws -> T? {
try getOptionalQueryItem(
in: queryParameters,
style: style,
explode: explode,
name: name,
as: type,
convert: convertTextToStringConvertible
Expand All @@ -88,11 +92,15 @@ public extension Converter {
// | server | get | request query | text | string-convertible | required | getRequiredQueryItemAsText |
func getRequiredQueryItemAsText<T: _StringConvertible>(
in queryParameters: [URLQueryItem],
style: ParameterStyle?,
explode: Bool?,
name: String,
as type: T.Type
) throws -> T {
try getRequiredQueryItem(
in: queryParameters,
style: style,
explode: explode,
name: name,
as: type,
convert: convertTextToStringConvertible
Expand All @@ -102,11 +110,15 @@ public extension Converter {
// | server | get | request query | text | array of string-convertibles | optional | getOptionalQueryItemAsText |
func getOptionalQueryItemAsText<T: _StringConvertible>(
in queryParameters: [URLQueryItem],
style: ParameterStyle?,
explode: Bool?,
name: String,
as type: [T].Type
) throws -> [T]? {
try getOptionalQueryItems(
in: queryParameters,
style: style,
explode: explode,
name: name,
as: type,
convert: convertTextToStringConvertible
Expand All @@ -116,11 +128,15 @@ public extension Converter {
// | server | get | request query | text | array of string-convertibles | required | getRequiredQueryItemAsText |
func getRequiredQueryItemAsText<T: _StringConvertible>(
in queryParameters: [URLQueryItem],
style: ParameterStyle?,
explode: Bool?,
name: String,
as type: [T].Type
) throws -> [T] {
try getRequiredQueryItems(
in: queryParameters,
style: style,
explode: explode,
name: name,
as: type,
convert: convertTextToStringConvertible
Expand All @@ -130,11 +146,15 @@ public extension Converter {
// | server | get | request query | text | date | optional | getOptionalQueryItemAsText |
func getOptionalQueryItemAsText(
in queryParameters: [URLQueryItem],
style: ParameterStyle?,
explode: Bool?,
name: String,
as type: Date.Type
) throws -> Date? {
try getOptionalQueryItem(
in: queryParameters,
style: style,
explode: explode,
name: name,
as: type,
convert: convertTextToDate
Expand All @@ -144,11 +164,15 @@ public extension Converter {
// | server | get | request query | text | date | required | getRequiredQueryItemAsText |
func getRequiredQueryItemAsText(
in queryParameters: [URLQueryItem],
style: ParameterStyle?,
explode: Bool?,
name: String,
as type: Date.Type
) throws -> Date {
try getRequiredQueryItem(
in: queryParameters,
style: style,
explode: explode,
name: name,
as: type,
convert: convertTextToDate
Expand All @@ -158,11 +182,15 @@ public extension Converter {
// | server | get | request query | text | array of dates | optional | getOptionalQueryItemAsText |
func getOptionalQueryItemAsText(
in queryParameters: [URLQueryItem],
style: ParameterStyle?,
explode: Bool?,
name: String,
as type: [Date].Type
) throws -> [Date]? {
try getOptionalQueryItems(
in: queryParameters,
style: style,
explode: explode,
name: name,
as: type,
convert: convertTextToDate
Expand All @@ -172,11 +200,15 @@ public extension Converter {
// | server | get | request query | text | array of dates | required | getRequiredQueryItemAsText |
func getRequiredQueryItemAsText(
in queryParameters: [URLQueryItem],
style: ParameterStyle?,
explode: Bool?,
name: String,
as type: [Date].Type
) throws -> [Date] {
try getRequiredQueryItems(
in: queryParameters,
style: style,
explode: explode,
name: name,
as: type,
convert: convertTextToDate
Expand Down
97 changes: 93 additions & 4 deletions Sources/OpenAPIRuntime/Conversion/CurrencyExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Copy link

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down
Loading