Closed Kastet closed 5 years ago
An object schema for a query param isn't support as it's unclear what the encoded string should be. What would you expect to get written?
The convention for nested dictionary values is to use keys surrounded by square brackets. Before getting URL-encoded the query param part of the URL will look like this
cat[name]=Joe&cat[age]=5&cat[owner][id]=1
Alamofire's URLEncoding.queryString
should be able to do the job (and it's already used to encode query params), in which case, all we need to do is to convert models into [String: Any]
. The first thing that comes to mind is JSONSerialization
.
extension Cat {
func encode() -> [String: Any] {
let jsonData = try! JSONEncoder().encode(self)
return try? JSONSerialization.jsonObject(with: jsonData) ?? [:]
}
}
Thoughts?
@Kastet I see. If you'd like to add that feature, you're more than welcome to. Our Models are already Codable, so you could hook into that. I see it as fairly edge case for a proper REST api.
Reference object isn't supported in query parameter.
generates
thought Cat will be missing
encode()
method.