From bdca83e74ec1b8c67d64f6f3d62acf92c2e7cce2 Mon Sep 17 00:00:00 2001 From: Eric Hyche Date: Sun, 8 Oct 2017 21:31:38 -0400 Subject: [PATCH] Fix compilation issue with Swift4 inline enums. This change fixes this issue: https://github.com/swagger-api/swagger-codegen/issues/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") --- .../src/main/resources/swift4/model.mustache | 4 +- .../src/test/resources/2_0/swift4Test.json | 8 ++++ .../Classes/Swaggers/APIs/Swift4TestAPI.swift | 45 ++++++++++--------- .../Swaggers/Models/AllPrimitives.swift | 10 ++++- .../Classes/Swaggers/Models/ErrorInfo.swift | 2 +- .../Swaggers/Models/GetAllModelsResult.swift | 2 +- ...ModelWithIntAdditionalPropertiesOnly.swift | 2 +- ...ithPropertiesAndAdditionalProperties.swift | 2 +- ...elWithStringAdditionalPropertiesOnly.swift | 2 +- .../Swaggers/Models/VariableNameTest.swift | 2 +- 10 files changed, 49 insertions(+), 30 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/swift4/model.mustache b/modules/swagger-codegen/src/main/resources/swift4/model.mustache index 9dedcc85d39f..383b229e447b 100644 --- a/modules/swagger-codegen/src/main/resources/swift4/model.mustache +++ b/modules/swagger-codegen/src/main/resources/swift4/model.mustache @@ -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() diff --git a/modules/swagger-codegen/src/test/resources/2_0/swift4Test.json b/modules/swagger-codegen/src/test/resources/2_0/swift4Test.json index cb13dfccb998..d9de56b3d1aa 100644 --- a/modules/swagger-codegen/src/test/resources/2_0/swift4Test.json +++ b/modules/swagger-codegen/src/test/resources/2_0/swift4Test.json @@ -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" diff --git a/samples/client/test/swift4/default/TestClient/Classes/Swaggers/APIs/Swift4TestAPI.swift b/samples/client/test/swift4/default/TestClient/Classes/Swaggers/APIs/Swift4TestAPI.swift index 1ea35e3db1f0..5e657469a257 100644 --- a/samples/client/test/swift4/default/TestClient/Classes/Swaggers/APIs/Swift4TestAPI.swift +++ b/samples/client/test/swift4/default/TestClient/Classes/Swaggers/APIs/Swift4TestAPI.swift @@ -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" : { @@ -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" ] } ] }}] diff --git a/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/AllPrimitives.swift b/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/AllPrimitives.swift index 6d0bf4eca188..3ad6c11c2fbe 100644 --- a/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/AllPrimitives.swift +++ b/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/AllPrimitives.swift @@ -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? @@ -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 @@ -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) @@ -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") } } diff --git a/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ErrorInfo.swift b/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ErrorInfo.swift index b4713b4c23dc..c313ada307f0 100644 --- a/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ErrorInfo.swift +++ b/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ErrorInfo.swift @@ -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) diff --git a/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/GetAllModelsResult.swift b/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/GetAllModelsResult.swift index 6cb8e4ce7d73..9708ce78e068 100644 --- a/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/GetAllModelsResult.swift +++ b/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/GetAllModelsResult.swift @@ -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) diff --git a/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ModelWithIntAdditionalPropertiesOnly.swift b/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ModelWithIntAdditionalPropertiesOnly.swift index c258745d8e9b..25392820511d 100644 --- a/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ModelWithIntAdditionalPropertiesOnly.swift +++ b/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ModelWithIntAdditionalPropertiesOnly.swift @@ -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) diff --git a/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ModelWithPropertiesAndAdditionalProperties.swift b/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ModelWithPropertiesAndAdditionalProperties.swift index f4aae6cc5e02..67f73da1bd0a 100644 --- a/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ModelWithPropertiesAndAdditionalProperties.swift +++ b/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ModelWithPropertiesAndAdditionalProperties.swift @@ -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) diff --git a/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ModelWithStringAdditionalPropertiesOnly.swift b/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ModelWithStringAdditionalPropertiesOnly.swift index dba82ff7f088..47ad742ae325 100644 --- a/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ModelWithStringAdditionalPropertiesOnly.swift +++ b/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/ModelWithStringAdditionalPropertiesOnly.swift @@ -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) diff --git a/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/VariableNameTest.swift b/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/VariableNameTest.swift index 470de98387ef..d2ec9840a37d 100644 --- a/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/VariableNameTest.swift +++ b/samples/client/test/swift4/default/TestClient/Classes/Swaggers/Models/VariableNameTest.swift @@ -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)