Skip to content

Commit

Permalink
Add additional swift4 initializer for initializing model object with …
Browse files Browse the repository at this point in the history
…properties.

This change fixes this issue: swagger-api#6641

It adds an additional initializer which allows model objects to be initialized using the properties. For exxample, if we had this model:

    "ErrorInfo": {
      "type": "object",
      "properties": {
        "code": {
          "type": "integer",
          "format": "int32"
        },
        "message": {
          "type": "string"
        },
        "details": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "description": "Example Error object"
    },

This we generate an initializer for this model object like this:

    public init(code: Int?, message: String?, details: [String]?) {
        self.code = code
        self.message = message
        self.details = details
    }
  • Loading branch information
Eric Hyche committed Oct 9, 2017
1 parent e3be665 commit 9f885c8
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ open class {{classname}}: {{#parent}}{{{parent}}}{{/parent}}{{^parent}}Codable{{
}
{{/additionalPropertiesType}}

public init({{#vars}}{{name}}: {{{datatypeWithEnum}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/vars}}) {
{{#vars}}
self.{{name}} = {{name}}
{{/vars}}
}

// Encodable protocol methods

public {{#parent}}override {{/parent}}func encode(to encoder: Encoder) throws {
Expand All @@ -79,7 +85,7 @@ open class {{classname}}: {{#parent}}{{{parent}}}{{/parent}}{{^parent}}Codable{{
}

// Decodable protocol methods

public {{#parent}}override {{/parent}}required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: String.self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,33 @@ open class AllPrimitives: Codable {
public var myStringEnumArray: [StringEnum]?


public init(myInteger: Int?, myIntegerArray: [Int]?, myLong: Int64?, myLongArray: [Int64]?, myFloat: Float?, myFloatArray: [Float]?, myDouble: Double?, myDoubleArray: [Double]?, myString: String?, myStringArray: [String]?, myBytes: Data?, myBytesArray: [Data]?, myBoolean: Bool?, myBooleanArray: [Bool]?, myDate: Date?, myDateArray: [Date]?, myDateTime: Date?, myDateTimeArray: [Date]?, myFile: URL?, myFileArray: [URL]?, myUUID: UUID?, myUUIDArray: [UUID]?, myStringEnum: StringEnum?, myStringEnumArray: [StringEnum]?) {
self.myInteger = myInteger
self.myIntegerArray = myIntegerArray
self.myLong = myLong
self.myLongArray = myLongArray
self.myFloat = myFloat
self.myFloatArray = myFloatArray
self.myDouble = myDouble
self.myDoubleArray = myDoubleArray
self.myString = myString
self.myStringArray = myStringArray
self.myBytes = myBytes
self.myBytesArray = myBytesArray
self.myBoolean = myBoolean
self.myBooleanArray = myBooleanArray
self.myDate = myDate
self.myDateArray = myDateArray
self.myDateTime = myDateTime
self.myDateTimeArray = myDateTimeArray
self.myFile = myFile
self.myFileArray = myFileArray
self.myUUID = myUUID
self.myUUIDArray = myUUIDArray
self.myStringEnum = myStringEnum
self.myStringEnumArray = myStringEnumArray
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
Expand Down Expand Up @@ -71,7 +98,7 @@ open class AllPrimitives: Codable {
}

// Decodable protocol methods

public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: String.self)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ open class ErrorInfo: Codable {
public var details: [String]?


public init(code: Int?, message: String?, details: [String]?) {
self.code = code
self.message = message
self.details = details
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
Expand All @@ -29,7 +35,7 @@ open class ErrorInfo: Codable {
}

// Decodable protocol methods

public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: String.self)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ open class GetAllModelsResult: Codable {
public var myVariableNameTest: VariableNameTest?


public init(myPrimitiveArray: [AllPrimitives]?, myPrimitive: AllPrimitives?, myVariableNameTest: VariableNameTest?) {
self.myPrimitiveArray = myPrimitiveArray
self.myPrimitive = myPrimitive
self.myVariableNameTest = myVariableNameTest
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
Expand All @@ -29,7 +35,7 @@ open class GetAllModelsResult: Codable {
}

// Decodable protocol methods

public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: String.self)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ open class ModelWithIntAdditionalPropertiesOnly: Codable {
}
}

public init() {
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
Expand All @@ -38,7 +41,7 @@ open class ModelWithIntAdditionalPropertiesOnly: Codable {
}

// Decodable protocol methods

public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: String.self)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ open class ModelWithPropertiesAndAdditionalProperties: Codable {
}
}

public init(myIntegerReq: Int, myIntegerOpt: Int?, myPrimitiveReq: AllPrimitives, myPrimitiveOpt: AllPrimitives?, myStringArrayReq: [String], myStringArrayOpt: [String]?, myPrimitiveArrayReq: [AllPrimitives], myPrimitiveArrayOpt: [AllPrimitives]?) {
self.myIntegerReq = myIntegerReq
self.myIntegerOpt = myIntegerOpt
self.myPrimitiveReq = myPrimitiveReq
self.myPrimitiveOpt = myPrimitiveOpt
self.myStringArrayReq = myStringArrayReq
self.myStringArrayOpt = myStringArrayOpt
self.myPrimitiveArrayReq = myPrimitiveArrayReq
self.myPrimitiveArrayOpt = myPrimitiveArrayOpt
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
Expand All @@ -54,7 +65,7 @@ open class ModelWithPropertiesAndAdditionalProperties: Codable {
}

// Decodable protocol methods

public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: String.self)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ open class ModelWithStringAdditionalPropertiesOnly: Codable {
}
}

public init() {
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
Expand All @@ -38,7 +41,7 @@ open class ModelWithStringAdditionalPropertiesOnly: Codable {
}

// Decodable protocol methods

public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: String.self)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ open class VariableNameTest: Codable {
public var _for: String?


public init(exampleName: String?, _for: String?) {
self.exampleName = exampleName
self._for = _for
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
Expand All @@ -29,7 +34,7 @@ open class VariableNameTest: Codable {
}

// Decodable protocol methods

public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: String.self)

Expand Down

0 comments on commit 9f885c8

Please sign in to comment.