yonaskolb / SwagGen

OpenAPI/Swagger 3.0 Parser and Swift code generator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bool To String question

mackoj opened this issue · comments

Hi @yonaskolb,

When I send request with query parameter some web-services uses "true"/"false" instead of "0"/"1" for boolean. The way SwagGen works today it's only sending "0"/"1".

In order to fix it I did make this modifications but I find them ugly at best. How can I fix this problem in a better way ?

Templates/Swift/Sources/APIClient.swift(line A10)

protocol BooleanToStringable {
   func toString() -> String
 }

 extension BooleanToStringable where Self == Bool {
   func toString() -> String {
     return self ? "true" : "false"
   }
 }

 extension Bool : BooleanToStringable {}

Templates/Swift/Sources/Request.swift(line 95)

                  if let {{ param.name }} = {{ param.name }} as? BooleanToStringable {
                     params["{{ param.value }}"] = {{ param.name }}.toString()
                   } else {
                     params["{{ param.value }}"] = {{ param.name }}
                   }

Thanks,

@mackoj Not sure if you're still looking for a cleaner solution, but I ran into this issue as well and after some digging I learned that we need to set the Alamofire URL encoding to use .literal for boolean encoding: Alamofire/Alamofire#1056 (comment)

I fixed it on line is 231 in Templates/Swift/Sources/APIClient:
urlRequest = try URLEncoding(destination: .queryString, boolEncoding: .literal).encode(urlRequest, with: queryParams)

image

Thanks I will replace my ugly fix