Skip to content

Commit b29c514

Browse files
ehychefvarose
authored andcommitted
Swift4: Fix inline enum issue (swagger-api#6640)
* Add addiitional files from upstream * Remove mis-added files * Fix compilation issue with Swift4 inline enums. 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")
1 parent 6988189 commit b29c514

File tree

10 files changed

+49
-30
lines changed

10 files changed

+49
-30
lines changed

modules/swagger-codegen/src/main/resources/swift4/model.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ open class {{classname}}: {{#parent}}{{{parent}}}{{/parent}}{{^parent}}Codable{{
7979
}
8080

8181
// Decodable protocol methods
82-
82+
8383
public {{#parent}}override {{/parent}}required init(from decoder: Decoder) throws {
8484
let container = try decoder.container(keyedBy: String.self)
8585
8686
{{#vars}}
87-
{{name}} = try container.decode{{#isListContainer}}Array{{/isListContainer}}{{^required}}IfPresent{{/required}}({{#isListContainer}}{{{items.datatype}}}{{/isListContainer}}{{^isListContainer}}{{{datatype}}}{{/isListContainer}}.self, forKey: "{{{baseName}}}")
87+
{{name}} = try container.decode{{#isListContainer}}Array{{/isListContainer}}{{^required}}IfPresent{{/required}}({{#isListContainer}}{{{items.datatype}}}{{/isListContainer}}{{^isListContainer}}{{{datatypeWithEnum}}}{{/isListContainer}}.self, forKey: "{{{baseName}}}")
8888
{{/vars}}
8989
{{#additionalPropertiesType}}
9090
var nonAdditionalPropertyKeys = Set<String>()

modules/swagger-codegen/src/test/resources/2_0/swift4Test.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,14 @@
220220
"items": {
221221
"$ref": "#/definitions/StringEnum"
222222
}
223+
},
224+
"myInlineStringEnum": {
225+
"type": "string",
226+
"enum": [
227+
"inlineStringEnumValue1",
228+
"inlineStringEnumValue2",
229+
"inlineStringEnumValue3"
230+
]
223231
}
224232
},
225233
"description": "Object which contains lots of different primitive Swagger types"

samples/client/test/swift4/default/TestClient/Classes/Swaggers/APIs/Swift4TestAPI.swift

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,27 @@ open class Swift4TestAPI {
3333
"myDateTimeArray" : [ "2000-01-23T04:56:07.000+00:00", "2000-01-23T04:56:07.000+00:00" ],
3434
"myStringArray" : [ "myStringArray", "myStringArray" ],
3535
"myFile" : "",
36-
"myFloatArray" : [ 2.302136, 2.302136 ],
3736
"myBytes" : "myBytes",
3837
"myLong" : 1,
3938
"myBooleanArray" : [ true, true ],
40-
"myDoubleArray" : [ 9.301444243932576, 9.301444243932576 ],
4139
"myInteger" : 0,
42-
"myString" : "myString",
4340
"myBytesArray" : [ "myBytesArray", "myBytesArray" ],
4441
"myDouble" : 7.061401241503109,
42+
"myIntegerArray" : [ 6, 6 ],
43+
"myInlineStringEnum" : "inlineStringEnumValue1",
44+
"myFileArray" : [ "", "" ],
45+
"myFloat" : 5.637377,
46+
"myStringEnumArray" : [ null, null ],
47+
"myFloatArray" : [ 2.302136, 2.302136 ],
48+
"myDoubleArray" : [ 9.301444243932576, 9.301444243932576 ],
49+
"myString" : "myString",
4550
"myDate" : "2000-01-23",
4651
"myDateArray" : [ "2000-01-23", "2000-01-23" ],
4752
"myDateTime" : "2000-01-23T04:56:07.000+00:00",
4853
"myLongArray" : [ 5, 5 ],
49-
"myIntegerArray" : [ 6, 6 ],
5054
"myUUID" : "046b6c7f-0b8a-43b9-b35d-6489e6daee91",
5155
"myBoolean" : true,
52-
"myFileArray" : [ "", "" ],
5356
"myStringEnum" : { },
54-
"myFloat" : 5.637377,
55-
"myStringEnumArray" : [ null, null ],
5657
"myUUIDArray" : [ "046b6c7f-0b8a-43b9-b35d-6489e6daee91", "046b6c7f-0b8a-43b9-b35d-6489e6daee91" ]
5758
},
5859
"myVariableNameTest" : {
@@ -63,51 +64,53 @@ open class Swift4TestAPI {
6364
"myDateTimeArray" : [ "2000-01-23T04:56:07.000+00:00", "2000-01-23T04:56:07.000+00:00" ],
6465
"myStringArray" : [ "myStringArray", "myStringArray" ],
6566
"myFile" : "",
66-
"myFloatArray" : [ 2.302136, 2.302136 ],
6767
"myBytes" : "myBytes",
6868
"myLong" : 1,
6969
"myBooleanArray" : [ true, true ],
70-
"myDoubleArray" : [ 9.301444243932576, 9.301444243932576 ],
7170
"myInteger" : 0,
72-
"myString" : "myString",
7371
"myBytesArray" : [ "myBytesArray", "myBytesArray" ],
7472
"myDouble" : 7.061401241503109,
73+
"myIntegerArray" : [ 6, 6 ],
74+
"myInlineStringEnum" : "inlineStringEnumValue1",
75+
"myFileArray" : [ "", "" ],
76+
"myFloat" : 5.637377,
77+
"myStringEnumArray" : [ null, null ],
78+
"myFloatArray" : [ 2.302136, 2.302136 ],
79+
"myDoubleArray" : [ 9.301444243932576, 9.301444243932576 ],
80+
"myString" : "myString",
7581
"myDate" : "2000-01-23",
7682
"myDateArray" : [ "2000-01-23", "2000-01-23" ],
7783
"myDateTime" : "2000-01-23T04:56:07.000+00:00",
7884
"myLongArray" : [ 5, 5 ],
79-
"myIntegerArray" : [ 6, 6 ],
8085
"myUUID" : "046b6c7f-0b8a-43b9-b35d-6489e6daee91",
8186
"myBoolean" : true,
82-
"myFileArray" : [ "", "" ],
8387
"myStringEnum" : { },
84-
"myFloat" : 5.637377,
85-
"myStringEnumArray" : [ null, null ],
8688
"myUUIDArray" : [ "046b6c7f-0b8a-43b9-b35d-6489e6daee91", "046b6c7f-0b8a-43b9-b35d-6489e6daee91" ]
8789
}, {
8890
"myDateTimeArray" : [ "2000-01-23T04:56:07.000+00:00", "2000-01-23T04:56:07.000+00:00" ],
8991
"myStringArray" : [ "myStringArray", "myStringArray" ],
9092
"myFile" : "",
91-
"myFloatArray" : [ 2.302136, 2.302136 ],
9293
"myBytes" : "myBytes",
9394
"myLong" : 1,
9495
"myBooleanArray" : [ true, true ],
95-
"myDoubleArray" : [ 9.301444243932576, 9.301444243932576 ],
9696
"myInteger" : 0,
97-
"myString" : "myString",
9897
"myBytesArray" : [ "myBytesArray", "myBytesArray" ],
9998
"myDouble" : 7.061401241503109,
99+
"myIntegerArray" : [ 6, 6 ],
100+
"myInlineStringEnum" : "inlineStringEnumValue1",
101+
"myFileArray" : [ "", "" ],
102+
"myFloat" : 5.637377,
103+
"myStringEnumArray" : [ null, null ],
104+
"myFloatArray" : [ 2.302136, 2.302136 ],
105+
"myDoubleArray" : [ 9.301444243932576, 9.301444243932576 ],
106+
"myString" : "myString",
100107
"myDate" : "2000-01-23",
101108
"myDateArray" : [ "2000-01-23", "2000-01-23" ],
102109
"myDateTime" : "2000-01-23T04:56:07.000+00:00",
103110
"myLongArray" : [ 5, 5 ],
104-
"myIntegerArray" : [ 6, 6 ],
105111
"myUUID" : "046b6c7f-0b8a-43b9-b35d-6489e6daee91",
106112
"myBoolean" : true,
107-
"myFileArray" : [ "", "" ],
108113
"myStringEnum" : { },
109-
"myFloat" : 5.637377,
110-
"myStringEnumArray" : [ null, null ],
111114
"myUUIDArray" : [ "046b6c7f-0b8a-43b9-b35d-6489e6daee91", "046b6c7f-0b8a-43b9-b35d-6489e6daee91" ]
112115
} ]
113116
}}]

samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/AllPrimitives.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import Foundation
1212

1313
open class AllPrimitives: Codable {
1414

15+
public enum MyInlineStringEnum: String, Codable {
16+
case inlinestringenumvalue1 = "inlineStringEnumValue1"
17+
case inlinestringenumvalue2 = "inlineStringEnumValue2"
18+
case inlinestringenumvalue3 = "inlineStringEnumValue3"
19+
}
1520
public var myInteger: Int?
1621
public var myIntegerArray: [Int]?
1722
public var myLong: Int64?
@@ -36,6 +41,7 @@ open class AllPrimitives: Codable {
3641
public var myUUIDArray: [UUID]?
3742
public var myStringEnum: StringEnum?
3843
public var myStringEnumArray: [StringEnum]?
44+
public var myInlineStringEnum: MyInlineStringEnum?
3945

4046

4147
// Encodable protocol methods
@@ -68,10 +74,11 @@ open class AllPrimitives: Codable {
6874
try container.encodeArrayIfPresent(myUUIDArray, forKey: "myUUIDArray")
6975
try container.encodeIfPresent(myStringEnum, forKey: "myStringEnum")
7076
try container.encodeArrayIfPresent(myStringEnumArray, forKey: "myStringEnumArray")
77+
try container.encodeIfPresent(myInlineStringEnum, forKey: "myInlineStringEnum")
7178
}
7279

7380
// Decodable protocol methods
74-
81+
7582
public required init(from decoder: Decoder) throws {
7683
let container = try decoder.container(keyedBy: String.self)
7784

@@ -99,6 +106,7 @@ open class AllPrimitives: Codable {
99106
myUUIDArray = try container.decodeArrayIfPresent(UUID.self, forKey: "myUUIDArray")
100107
myStringEnum = try container.decodeIfPresent(StringEnum.self, forKey: "myStringEnum")
101108
myStringEnumArray = try container.decodeArrayIfPresent(StringEnum.self, forKey: "myStringEnumArray")
109+
myInlineStringEnum = try container.decodeIfPresent(MyInlineStringEnum.self, forKey: "myInlineStringEnum")
102110
}
103111
}
104112

samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ErrorInfo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ open class ErrorInfo: Codable {
2929
}
3030

3131
// Decodable protocol methods
32-
32+
3333
public required init(from decoder: Decoder) throws {
3434
let container = try decoder.container(keyedBy: String.self)
3535

samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/GetAllModelsResult.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ open class GetAllModelsResult: Codable {
2929
}
3030

3131
// Decodable protocol methods
32-
32+
3333
public required init(from decoder: Decoder) throws {
3434
let container = try decoder.container(keyedBy: String.self)
3535

samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ModelWithIntAdditionalPropertiesOnly.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ open class ModelWithIntAdditionalPropertiesOnly: Codable {
3838
}
3939

4040
// Decodable protocol methods
41-
41+
4242
public required init(from decoder: Decoder) throws {
4343
let container = try decoder.container(keyedBy: String.self)
4444

samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ModelWithPropertiesAndAdditionalProperties.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ open class ModelWithPropertiesAndAdditionalProperties: Codable {
5454
}
5555

5656
// Decodable protocol methods
57-
57+
5858
public required init(from decoder: Decoder) throws {
5959
let container = try decoder.container(keyedBy: String.self)
6060

samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ModelWithStringAdditionalPropertiesOnly.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ open class ModelWithStringAdditionalPropertiesOnly: Codable {
3838
}
3939

4040
// Decodable protocol methods
41-
41+
4242
public required init(from decoder: Decoder) throws {
4343
let container = try decoder.container(keyedBy: String.self)
4444

samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/VariableNameTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ open class VariableNameTest: Codable {
2929
}
3030

3131
// Decodable protocol methods
32-
32+
3333
public required init(from decoder: Decoder) throws {
3434
let container = try decoder.container(keyedBy: String.self)
3535

0 commit comments

Comments
 (0)