-
Notifications
You must be signed in to change notification settings - Fork 6k
[Issue 9386] Add Swift 5 code generator and templates #9731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@HugoMario who is the right person to check this PR? |
Great work! I have been waiting for Swift 5 support for so long at #9386. Hopefully this can be merged soon |
thanks a lot, please let me know once you want your changes be merged |
@plam4u , let me find out who in team can help us with that. |
hey @4brunu, would you help us reviewing this PR? |
s.source_files = '{{projectName}}/Classes/**/*.swift'{{#usePromiseKit}} | ||
s.dependency 'PromiseKit/CorePromise', '~> 4.4.0'{{/usePromiseKit}}{{#useRxSwift}} | ||
s.dependency 'RxSwift', '~> 4.0'{{/useRxSwift}} | ||
s.dependency 'Alamofire', '~> 4.5.0' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@plam4u the version of Alomofire included in podspec doesn't compile with Xcode 11 and Swift 5.1.
I needed to upgrade from s.dependency 'Alamofire', '~> 4.5.0'
to s.dependency 'Alamofire', '~> 4.9.0'
.
Can you also update the podspec please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@plam4u the version of Alomofire included in podspec doesn't compile with Xcode 11 and Swift 5.1.
I needed to upgrade from 4.5.0 to 4.9.0.
Can you update the podspec please?
} | ||
{{/hasVars}} | ||
{{#additionalPropertiesType}} | ||
public var additionalProperties: [String:{{{additionalPropertiesType}}}] = [:] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't work with the additionalProperties of the type 'Any' because it outputs two errors.
Protocol type 'Any' cannot conform to 'Encodable' because only concrete types can conform to protocols
Protocol type 'Any' cannot conform to 'Decodable' because only concrete types can conform to protocols
Here is an example of the generated code
import Foundationpublic struct ProblemDetails: Codable {
public var type: String?
public var title: String?
public var status: Int?
public var detail: String?
public var instance: String?
public init(type: String?, title: String?, status: Int?, detail: String?, instance: String?) {
self.type = type
self.title = title
self.status = status
self.detail = detail
self.instance = instance
}
public var additionalProperties: [String:Any] = [:]
public subscript(key: String) -> Any? {
get {
if let value = additionalProperties[key] {
return value
}
return nil
}
set {
additionalProperties[key] = newValue
}
}
// Encodable protocol methods
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: String.self)
try container.encodeIfPresent(type, forKey: "type")
try container.encodeIfPresent(title, forKey: "title")
try container.encodeIfPresent(status, forKey: "status")
try container.encodeIfPresent(detail, forKey: "detail")
try container.encodeIfPresent(instance, forKey: "instance")
try container.encodeMap(additionalProperties) // build error here. `Protocol type 'Any' cannot conform to 'Encodable' because only concrete types can conform to protocols `
}
// Decodable protocol methods
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: String.self)
type = try container.decodeIfPresent(String.self, forKey: "type")
title = try container.decodeIfPresent(String.self, forKey: "title")
status = try container.decodeIfPresent(Int.self, forKey: "status")
detail = try container.decodeIfPresent(String.self, forKey: "detail")
instance = try container.decodeIfPresent(String.self, forKey: "instance")
var nonAdditionalPropertyKeys = Set<String>()
nonAdditionalPropertyKeys.insert("type")
nonAdditionalPropertyKeys.insert("title")
nonAdditionalPropertyKeys.insert("status")
nonAdditionalPropertyKeys.insert("detail")
nonAdditionalPropertyKeys.insert("instance")
additionalProperties = try container.decodeMap(Any.self, excludedKeys: nonAdditionalPropertyKeys) // build error here. `Protocol type 'Any' cannot conform to 'Decodable' because only concrete types can conform to protocols`
}
}
One possible solution is using AnyCodable instead of Any
Make return types Void instead of Any
Cleanup Swift 5 classes
Fix imports in Swift5Codegen
s.source_files = '{{projectName}}/Classes/**/*.swift'{{#usePromiseKit}} | ||
s.dependency 'PromiseKit/CorePromise', '~> 4.4.0'{{/usePromiseKit}}{{#useRxSwift}} | ||
s.dependency 'RxSwift', '~> 4.0'{{/useRxSwift}} | ||
s.dependency 'Alamofire', '~> 4.5.0' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@plam4u the version of Alomofire included in podspec doesn't compile with Xcode 11 and Swift 5.1.
I needed to upgrade from 4.5.0 to 4.9.0.
Can you update the podspec please?
@@ -0,0 +1,3 @@ | |||
github "Alamofire/Alamofire" ~> 4.5.0{{#usePromiseKit}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@plam4u the version of Alomofire included in podspec doesn't compile with Xcode 11 and Swift 5.1.
I needed to upgrade from 4.5.0
to 4.9.0
.
Can you also update the Cartfile please?
The command
|
Update Alamofire dependency
Issue 9386 alamofire
Update swift5 CodableHelper
@4brunu, this last error it's something that can be fixed on codegen side. i can help with that |
what do you guys think about merging this PR now and address new issue in a different tickets? so others developer can propose PR too? |
@HugoMario I think the last error it's now fixed with the last commits and my concernes about the Alamofire version are also fixed. |
oh great, thanks for letting me know @4brunu |
thanks a lot @plam4u !!!!!!!!!!!! for this PR |
Thank you for looking into it, guys! |
It appears that swagger-api#9731 is merged and adds Swift 5 (5.x?) support which it would be nice to call out in the Readme, if accurate. May want to add @plam4u (https://github.com/plam4u) to the Template Creator list, but I didn't want to do that without checking with them.
PR checklist
./bin/
to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.sh
and./bin/security/{LANG}-petstore.sh
if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\
.3.0.0
branch for changes related to OpenAPI spec 3.0. Default:master
.Description of the PR
This PR copies the swift4 code generator and templates to swift5. A new language option for the command-line is registered - swift5. All the compile issues are fixed and the tests are executed to make sure it works. The updates apply to the default implementation only. I haven't touched the RxSwift or ObjC implementations.
Changes done