Skip to content

Commit

Permalink
Fix compilation issue with Swift4 inline enums.
Browse files Browse the repository at this point in the history
This change fixes this issue: swagger-api#6607

The problem was that I was using "datatype" instead of "datatypeWithEnum" in the model.mustache file.

When you have a the following model property:

        "myInlineStringEnum": {
          "type": "string",
          "enum": [
            "inlineStringEnumValue1",
            "inlineStringEnumValue2",
            "inlineStringEnumValue3"
          ]
        }

Then we were generating:

    public enum MyInlineStringEnum: String, Codable {
        case inlinestringenumvalue1 = "inlineStringEnumValue1"
        case inlinestringenumvalue2 = "inlineStringEnumValue2"
        case inlinestringenumvalue3 = "inlineStringEnumValue3"
    }

However, when we decode this, we were using type of the enum ("datatype") rather than the enum type itself ("datatypeWithEnum"). So we were generating:

        myInlineStringEnum = try container.decodeIfPresent(String.self, forKey: "myInlineStringEnum")

rather than:

        myInlineStringEnum = try container.decodeIfPresent(MyInlineStringEnum.self, forKey: "myInlineStringEnum")
  • Loading branch information
Eric Hyche committed Oct 9, 2017
1 parent a456eba commit bdca83e
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ 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)
{{#vars}}
{{name}} = try container.decode{{#isListContainer}}Array{{/isListContainer}}{{^required}}IfPresent{{/required}}({{#isListContainer}}{{{items.datatype}}}{{/isListContainer}}{{^isListContainer}}{{{datatype}}}{{/isListContainer}}.self, forKey: "{{{baseName}}}")
{{name}} = try container.decode{{#isListContainer}}Array{{/isListContainer}}{{^required}}IfPresent{{/required}}({{#isListContainer}}{{{items.datatype}}}{{/isListContainer}}{{^isListContainer}}{{{datatypeWithEnum}}}{{/isListContainer}}.self, forKey: "{{{baseName}}}")
{{/vars}}
{{#additionalPropertiesType}}
var nonAdditionalPropertyKeys = Set<String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@
"items": {
"$ref": "#/definitions/StringEnum"
}
},
"myInlineStringEnum": {
"type": "string",
"enum": [
"inlineStringEnumValue1",
"inlineStringEnumValue2",
"inlineStringEnumValue3"
]
}
},
"description": "Object which contains lots of different primitive Swagger types"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,27 @@ open class Swift4TestAPI {
"myDateTimeArray" : [ "2000-01-23T04:56:07.000+00:00", "2000-01-23T04:56:07.000+00:00" ],
"myStringArray" : [ "myStringArray", "myStringArray" ],
"myFile" : "",
"myFloatArray" : [ 2.302136, 2.302136 ],
"myBytes" : "myBytes",
"myLong" : 1,
"myBooleanArray" : [ true, true ],
"myDoubleArray" : [ 9.301444243932576, 9.301444243932576 ],
"myInteger" : 0,
"myString" : "myString",
"myBytesArray" : [ "myBytesArray", "myBytesArray" ],
"myDouble" : 7.061401241503109,
"myIntegerArray" : [ 6, 6 ],
"myInlineStringEnum" : "inlineStringEnumValue1",
"myFileArray" : [ "", "" ],
"myFloat" : 5.637377,
"myStringEnumArray" : [ null, null ],
"myFloatArray" : [ 2.302136, 2.302136 ],
"myDoubleArray" : [ 9.301444243932576, 9.301444243932576 ],
"myString" : "myString",
"myDate" : "2000-01-23",
"myDateArray" : [ "2000-01-23", "2000-01-23" ],
"myDateTime" : "2000-01-23T04:56:07.000+00:00",
"myLongArray" : [ 5, 5 ],
"myIntegerArray" : [ 6, 6 ],
"myUUID" : "046b6c7f-0b8a-43b9-b35d-6489e6daee91",
"myBoolean" : true,
"myFileArray" : [ "", "" ],
"myStringEnum" : { },
"myFloat" : 5.637377,
"myStringEnumArray" : [ null, null ],
"myUUIDArray" : [ "046b6c7f-0b8a-43b9-b35d-6489e6daee91", "046b6c7f-0b8a-43b9-b35d-6489e6daee91" ]
},
"myVariableNameTest" : {
Expand All @@ -63,51 +64,53 @@ open class Swift4TestAPI {
"myDateTimeArray" : [ "2000-01-23T04:56:07.000+00:00", "2000-01-23T04:56:07.000+00:00" ],
"myStringArray" : [ "myStringArray", "myStringArray" ],
"myFile" : "",
"myFloatArray" : [ 2.302136, 2.302136 ],
"myBytes" : "myBytes",
"myLong" : 1,
"myBooleanArray" : [ true, true ],
"myDoubleArray" : [ 9.301444243932576, 9.301444243932576 ],
"myInteger" : 0,
"myString" : "myString",
"myBytesArray" : [ "myBytesArray", "myBytesArray" ],
"myDouble" : 7.061401241503109,
"myIntegerArray" : [ 6, 6 ],
"myInlineStringEnum" : "inlineStringEnumValue1",
"myFileArray" : [ "", "" ],
"myFloat" : 5.637377,
"myStringEnumArray" : [ null, null ],
"myFloatArray" : [ 2.302136, 2.302136 ],
"myDoubleArray" : [ 9.301444243932576, 9.301444243932576 ],
"myString" : "myString",
"myDate" : "2000-01-23",
"myDateArray" : [ "2000-01-23", "2000-01-23" ],
"myDateTime" : "2000-01-23T04:56:07.000+00:00",
"myLongArray" : [ 5, 5 ],
"myIntegerArray" : [ 6, 6 ],
"myUUID" : "046b6c7f-0b8a-43b9-b35d-6489e6daee91",
"myBoolean" : true,
"myFileArray" : [ "", "" ],
"myStringEnum" : { },
"myFloat" : 5.637377,
"myStringEnumArray" : [ null, null ],
"myUUIDArray" : [ "046b6c7f-0b8a-43b9-b35d-6489e6daee91", "046b6c7f-0b8a-43b9-b35d-6489e6daee91" ]
}, {
"myDateTimeArray" : [ "2000-01-23T04:56:07.000+00:00", "2000-01-23T04:56:07.000+00:00" ],
"myStringArray" : [ "myStringArray", "myStringArray" ],
"myFile" : "",
"myFloatArray" : [ 2.302136, 2.302136 ],
"myBytes" : "myBytes",
"myLong" : 1,
"myBooleanArray" : [ true, true ],
"myDoubleArray" : [ 9.301444243932576, 9.301444243932576 ],
"myInteger" : 0,
"myString" : "myString",
"myBytesArray" : [ "myBytesArray", "myBytesArray" ],
"myDouble" : 7.061401241503109,
"myIntegerArray" : [ 6, 6 ],
"myInlineStringEnum" : "inlineStringEnumValue1",
"myFileArray" : [ "", "" ],
"myFloat" : 5.637377,
"myStringEnumArray" : [ null, null ],
"myFloatArray" : [ 2.302136, 2.302136 ],
"myDoubleArray" : [ 9.301444243932576, 9.301444243932576 ],
"myString" : "myString",
"myDate" : "2000-01-23",
"myDateArray" : [ "2000-01-23", "2000-01-23" ],
"myDateTime" : "2000-01-23T04:56:07.000+00:00",
"myLongArray" : [ 5, 5 ],
"myIntegerArray" : [ 6, 6 ],
"myUUID" : "046b6c7f-0b8a-43b9-b35d-6489e6daee91",
"myBoolean" : true,
"myFileArray" : [ "", "" ],
"myStringEnum" : { },
"myFloat" : 5.637377,
"myStringEnumArray" : [ null, null ],
"myUUIDArray" : [ "046b6c7f-0b8a-43b9-b35d-6489e6daee91", "046b6c7f-0b8a-43b9-b35d-6489e6daee91" ]
} ]
}}]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import Foundation

open class AllPrimitives: Codable {

public enum MyInlineStringEnum: String, Codable {
case inlinestringenumvalue1 = "inlineStringEnumValue1"
case inlinestringenumvalue2 = "inlineStringEnumValue2"
case inlinestringenumvalue3 = "inlineStringEnumValue3"
}
public var myInteger: Int?
public var myIntegerArray: [Int]?
public var myLong: Int64?
Expand All @@ -36,6 +41,7 @@ open class AllPrimitives: Codable {
public var myUUIDArray: [UUID]?
public var myStringEnum: StringEnum?
public var myStringEnumArray: [StringEnum]?
public var myInlineStringEnum: MyInlineStringEnum?


// Encodable protocol methods
Expand Down Expand Up @@ -68,10 +74,11 @@ open class AllPrimitives: Codable {
try container.encodeArrayIfPresent(myUUIDArray, forKey: "myUUIDArray")
try container.encodeIfPresent(myStringEnum, forKey: "myStringEnum")
try container.encodeArrayIfPresent(myStringEnumArray, forKey: "myStringEnumArray")
try container.encodeIfPresent(myInlineStringEnum, forKey: "myInlineStringEnum")
}

// Decodable protocol methods

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

Expand Down Expand Up @@ -99,6 +106,7 @@ open class AllPrimitives: Codable {
myUUIDArray = try container.decodeArrayIfPresent(UUID.self, forKey: "myUUIDArray")
myStringEnum = try container.decodeIfPresent(StringEnum.self, forKey: "myStringEnum")
myStringEnumArray = try container.decodeArrayIfPresent(StringEnum.self, forKey: "myStringEnumArray")
myInlineStringEnum = try container.decodeIfPresent(MyInlineStringEnum.self, forKey: "myInlineStringEnum")
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,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 @@ -29,7 +29,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 @@ -38,7 +38,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 @@ -54,7 +54,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 @@ -38,7 +38,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 @@ -29,7 +29,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 bdca83e

Please sign in to comment.