From 2cf2684f695c4d771c778596c1c1f7bb22ef844e Mon Sep 17 00:00:00 2001 From: Kosuke Ohmura Date: Wed, 15 Mar 2017 16:56:11 +0900 Subject: [PATCH] Support array-ish query parameter convention --- .../APIKit/Serializations/URLEncodedSerialization.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Sources/APIKit/Serializations/URLEncodedSerialization.swift b/Sources/APIKit/Serializations/URLEncodedSerialization.swift index cb996b39..786d2dae 100644 --- a/Sources/APIKit/Serializations/URLEncodedSerialization.swift +++ b/Sources/APIKit/Serializations/URLEncodedSerialization.swift @@ -93,6 +93,14 @@ public final class URLEncodedSerialization { if value is NSNull { return "\(escape(key))" } + + if let valueAsArray = value as? Array { + let escapedKeyForArray = escape(key + "[]") + return valueAsArray.map { (element: Any) -> String in + let elementAsString = (element as? String) ?? "\(element)" + return "\(escapedKeyForArray)=\(escape(elementAsString))" + }.joined(separator: "&") + } let valueAsString = (value as? String) ?? "\(value)" return "\(escape(key))=\(escape(valueAsString))"