Make sure body params of type Int32/Int64 are correctly encoded for that create JSONEncodable extensions for Int32/Int64. Re-use extensions in Model.encodeToJSON to simplify code.

This commit is contained in:
Christophe Jolif
2016-03-22 22:23:47 +01:00
parent 677890fc08
commit fb56e11470
14 changed files with 39 additions and 18 deletions

View File

@@ -19,6 +19,14 @@ extension Int: JSONEncodable {
func encodeToJSON() -> AnyObject { return self }
}
extension Int32: JSONEncodable {
func encodeToJSON() -> AnyObject { return NSNumber(int: self) }
}
extension Int64: JSONEncodable {
func encodeToJSON() -> AnyObject { return NSNumber(longLong: self) }
}
extension Double: JSONEncodable {
func encodeToJSON() -> AnyObject { return self }
}

View File

@@ -25,10 +25,10 @@ public class {{classname}}: JSONEncodable {
// MARK: JSONEncodable
func encodeToJSON() -> AnyObject {
var nillableDictionary = [String:AnyObject?](){{#vars}}{{#isNotContainer}}{{#isPrimitiveType}}{{^isEnum}}{{#isInteger}}{{^isLong}}
nillableDictionary["{{baseName}}"] = self.{{name}} != nil ? NSNumber(int: self.{{name}}!) : nil{{/isLong}}{{/isInteger}}{{^isInteger}}{{#isLong}}
nillableDictionary["{{baseName}}"] = self.{{name}} != nil ? NSNumber(longLong: self.{{name}}!) : nil{{/isLong}}{{/isInteger}}{{^isInteger}}{{^isLong}}
nillableDictionary["{{baseName}}"] = self.{{name}}{{/isLong}}{{/isInteger}}{{/isEnum}}{{/isPrimitiveType}}{{#isEnum}}
var nillableDictionary = [String:AnyObject?](){{#vars}}{{#isNotContainer}}{{#isPrimitiveType}}{{^isEnum}}{{#isInteger}}
nillableDictionary["{{baseName}}"] = self.{{name}}{{^unwrapRequired}}?{{/unwrapRequired}}{{#unwrapRequired}}{{^required}}?{{/required}}{{/unwrapRequired}}.encodeToJSON(){{/isInteger}}{{#isLong}}
nillableDictionary["{{baseName}}"] = self.{{name}}{{^unwrapRequired}}?{{/unwrapRequired}}{{#unwrapRequired}}{{^required}}?{{/required}}{{/unwrapRequired}}.encodeToJSON(){{/isLong}}{{^isLong}}{{^isInteger}}
nillableDictionary["{{baseName}}"] = self.{{name}}{{/isInteger}}{{/isLong}}{{/isEnum}}{{/isPrimitiveType}}{{#isEnum}}
nillableDictionary["{{baseName}}"] = self.{{name}}{{^unwrapRequired}}?{{/unwrapRequired}}{{#unwrapRequired}}{{^required}}?{{/required}}{{/unwrapRequired}}.rawValue{{/isEnum}}{{^isPrimitiveType}}
nillableDictionary["{{baseName}}"] = self.{{name}}{{^unwrapRequired}}?{{/unwrapRequired}}{{#unwrapRequired}}{{^required}}?{{/required}}{{/unwrapRequired}}.encodeToJSON(){{/isPrimitiveType}}{{/isNotContainer}}{{#isContainer}}
nillableDictionary["{{baseName}}"] = self.{{name}}{{^unwrapRequired}}?{{/unwrapRequired}}{{#unwrapRequired}}{{^required}}?{{/required}}{{/unwrapRequired}}.encodeToJSON(){{/isContainer}}{{/vars}}

View File

@@ -19,6 +19,14 @@ extension Int: JSONEncodable {
func encodeToJSON() -> AnyObject { return self }
}
extension Int32: JSONEncodable {
func encodeToJSON() -> AnyObject { return NSNumber(int: self) }
}
extension Int64: JSONEncodable {
func encodeToJSON() -> AnyObject { return NSNumber(longLong: self) }
}
extension Double: JSONEncodable {
func encodeToJSON() -> AnyObject { return self }
}

View File

@@ -19,7 +19,7 @@ public class Category: JSONEncodable {
// MARK: JSONEncodable
func encodeToJSON() -> AnyObject {
var nillableDictionary = [String:AnyObject?]()
nillableDictionary["id"] = self.id != nil ? NSNumber(longLong: self.id!) : nil
nillableDictionary["id"] = self.id?.encodeToJSON()
nillableDictionary["name"] = self.name
let dictionary: [String:AnyObject] = APIHelper.rejectNil(nillableDictionary) ?? [:]
return dictionary

View File

@@ -32,7 +32,7 @@ public class InlineResponse200: JSONEncodable {
var nillableDictionary = [String:AnyObject?]()
nillableDictionary["photoUrls"] = self.photoUrls?.encodeToJSON()
nillableDictionary["name"] = self.name
nillableDictionary["id"] = self.id != nil ? NSNumber(longLong: self.id!) : nil
nillableDictionary["id"] = self.id?.encodeToJSON()
nillableDictionary["category"] = self.category
nillableDictionary["tags"] = self.tags?.encodeToJSON()
nillableDictionary["status"] = self.status?.rawValue

View File

@@ -18,7 +18,7 @@ public class Model200Response: JSONEncodable {
// MARK: JSONEncodable
func encodeToJSON() -> AnyObject {
var nillableDictionary = [String:AnyObject?]()
nillableDictionary["name"] = self.name != nil ? NSNumber(int: self.name!) : nil
nillableDictionary["name"] = self.name?.encodeToJSON()
let dictionary: [String:AnyObject] = APIHelper.rejectNil(nillableDictionary) ?? [:]
return dictionary
}

View File

@@ -18,7 +18,7 @@ public class ModelReturn: JSONEncodable {
// MARK: JSONEncodable
func encodeToJSON() -> AnyObject {
var nillableDictionary = [String:AnyObject?]()
nillableDictionary["return"] = self._return != nil ? NSNumber(int: self._return!) : nil
nillableDictionary["return"] = self._return?.encodeToJSON()
let dictionary: [String:AnyObject] = APIHelper.rejectNil(nillableDictionary) ?? [:]
return dictionary
}

View File

@@ -19,8 +19,8 @@ public class Name: JSONEncodable {
// MARK: JSONEncodable
func encodeToJSON() -> AnyObject {
var nillableDictionary = [String:AnyObject?]()
nillableDictionary["name"] = self.name != nil ? NSNumber(int: self.name!) : nil
nillableDictionary["snake_case"] = self.snakeCase != nil ? NSNumber(int: self.snakeCase!) : nil
nillableDictionary["name"] = self.name?.encodeToJSON()
nillableDictionary["snake_case"] = self.snakeCase?.encodeToJSON()
let dictionary: [String:AnyObject] = APIHelper.rejectNil(nillableDictionary) ?? [:]
return dictionary
}

View File

@@ -30,9 +30,9 @@ public class Order: JSONEncodable {
// MARK: JSONEncodable
func encodeToJSON() -> AnyObject {
var nillableDictionary = [String:AnyObject?]()
nillableDictionary["id"] = self.id != nil ? NSNumber(longLong: self.id!) : nil
nillableDictionary["petId"] = self.petId != nil ? NSNumber(longLong: self.petId!) : nil
nillableDictionary["quantity"] = self.quantity != nil ? NSNumber(int: self.quantity!) : nil
nillableDictionary["id"] = self.id?.encodeToJSON()
nillableDictionary["petId"] = self.petId?.encodeToJSON()
nillableDictionary["quantity"] = self.quantity?.encodeToJSON()
nillableDictionary["shipDate"] = self.shipDate?.encodeToJSON()
nillableDictionary["status"] = self.status?.rawValue
nillableDictionary["complete"] = self.complete

View File

@@ -30,7 +30,7 @@ public class Pet: JSONEncodable {
// MARK: JSONEncodable
func encodeToJSON() -> AnyObject {
var nillableDictionary = [String:AnyObject?]()
nillableDictionary["id"] = self.id != nil ? NSNumber(longLong: self.id!) : nil
nillableDictionary["id"] = self.id?.encodeToJSON()
nillableDictionary["category"] = self.category?.encodeToJSON()
nillableDictionary["name"] = self.name
nillableDictionary["photoUrls"] = self.photoUrls?.encodeToJSON()

View File

@@ -18,7 +18,7 @@ public class SpecialModelName: JSONEncodable {
// MARK: JSONEncodable
func encodeToJSON() -> AnyObject {
var nillableDictionary = [String:AnyObject?]()
nillableDictionary["$special[property.name]"] = self.specialPropertyName != nil ? NSNumber(longLong: self.specialPropertyName!) : nil
nillableDictionary["$special[property.name]"] = self.specialPropertyName?.encodeToJSON()
let dictionary: [String:AnyObject] = APIHelper.rejectNil(nillableDictionary) ?? [:]
return dictionary
}

View File

@@ -19,7 +19,7 @@ public class Tag: JSONEncodable {
// MARK: JSONEncodable
func encodeToJSON() -> AnyObject {
var nillableDictionary = [String:AnyObject?]()
nillableDictionary["id"] = self.id != nil ? NSNumber(longLong: self.id!) : nil
nillableDictionary["id"] = self.id?.encodeToJSON()
nillableDictionary["name"] = self.name
let dictionary: [String:AnyObject] = APIHelper.rejectNil(nillableDictionary) ?? [:]
return dictionary

View File

@@ -26,14 +26,14 @@ public class User: JSONEncodable {
// MARK: JSONEncodable
func encodeToJSON() -> AnyObject {
var nillableDictionary = [String:AnyObject?]()
nillableDictionary["id"] = self.id != nil ? NSNumber(longLong: self.id!) : nil
nillableDictionary["id"] = self.id?.encodeToJSON()
nillableDictionary["username"] = self.username
nillableDictionary["firstName"] = self.firstName
nillableDictionary["lastName"] = self.lastName
nillableDictionary["email"] = self.email
nillableDictionary["password"] = self.password
nillableDictionary["phone"] = self.phone
nillableDictionary["userStatus"] = self.userStatus != nil ? NSNumber(int: self.userStatus!) : nil
nillableDictionary["userStatus"] = self.userStatus?.encodeToJSON()
let dictionary: [String:AnyObject] = APIHelper.rejectNil(nillableDictionary) ?? [:]
return dictionary
}

View File

@@ -59,6 +59,11 @@
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
}
],
"info" : {