Skip to content
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

[Swift] Bool value conversion #2446

Closed
orkenstein opened this issue Mar 24, 2016 · 9 comments
Closed

[Swift] Bool value conversion #2446

orkenstein opened this issue Mar 24, 2016 · 9 comments

Comments

@orkenstein
Copy link

There is an issue with Bool value conversion to GET URL parameters.
It's described here, as well as the solution:
http://stackoverflow.com/questions/36185530/how-to-check-dictionary-value-to-be-exactly-a-bool/36186159#36186159

Swagger server implementation accepts Bool as true/false only. So this breaks some logic.

@wing328
Copy link
Contributor

wing328 commented Mar 29, 2016

@orkenstein thanks for sharing the fix. May I know if you've cycle to contribute a fix?

A good starting point is to add convertBoolToString to APIHelper: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/swift/APIHelper.mustache

@orkenstein
Copy link
Author

@wing328
Yep, no problem, I will, whet I get some time.
Swift templates requires 2 more crucial improvements:

  • support array as root object of http body
  • support body and query parameters at the same time.

Alamofire does not allow to do this by default. I've already found the solution, but I use custom templates and need somebody to review my code.

@wing328
Copy link
Contributor

wing328 commented Mar 31, 2016

@orkenstein thanks!. To run the integration test for Swift Petstore after making the change, please do the following:

mvn clean && ./bin/swift-petstore.sh
cd samples/client/petstore/swift/SwaggerClientTests && mvn integration-test

@cjolif
Copy link
Contributor

cjolif commented Apr 1, 2016

support body and query parameters at the same time.

That's a pretty big issue indeed :( is there a specific issue on this repo for that?

@orkenstein
Copy link
Author

@cjolif looks like no.

@cjolif
Copy link
Contributor

cjolif commented Apr 1, 2016

thanks @orkenstein, I created #2483 to track this one...

@wing328
Copy link
Contributor

wing328 commented Apr 1, 2016

@cjolif thanks for opening an issue to track it and hopefully they will add the support soon.

@wing328
Copy link
Contributor

wing328 commented May 21, 2016

PR merged. Issue closed.

@wing328 wing328 closed this as completed May 21, 2016
@bull-xu
Copy link

bull-xu commented Mar 5, 2017

For Swift 3 version:

static func convertBoolToString(_ source: [String: Any]?) -> [String:Any]? {
	guard let source = source else {
		return nil
	}
	var destination = [String:Any]()
	let theTrue = NSNumber(value: true)
	let theFalse = NSNumber(value: false)
	for (key, value) in source {
		switch value {
		case let x as NSNumber where x === theTrue || x === theFalse:
			destination[key] = "\(x.boolValue)"
		default:
			destination[key] = value
		}
	}
	return destination
}

Sample:

var quantity: Int64? = 4
var formParams: [String:Any?] = [:]
formParams["type"] = "abc"
formParams["quantity"] = quantity
formParams["hidden"] = NSNumber(value: true)
formParams["enabled"] = NSNumber(value: false)
formParams["easy"] = true

let nonNullParameters = APIHelper.rejectNil(formParams)
// output: ["hidden": 1, "quantity": 4, "enabled": 0, "easy": true, "delivery_type": "abc"]
let parameters = APIHelper.convertBoolToString(nonNullParameters)
// output: ["hidden": "true", "quantity": 4, "enabled": "false", "easy": "true", "type": "abc"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants