yonaskolb / SwagGen

OpenAPI/Swagger 3.0 Parser and Swift code generator
MIT License
626 stars 146 forks source link

Bool To String question #236

Closed mackoj closed 3 years ago

mackoj commented 4 years ago

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,

paigeyahnke commented 4 years ago

@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: https://github.com/Alamofire/Alamofire/issues/1056#issuecomment-411123543

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

image

mackoj commented 4 years ago

Thanks I will replace my ugly fix