-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Add a swift4 client generator #6010
Conversation
@ehyche this is super cool - i'll have time a little later today to take a look - thx! |
Thanks, @jaz-ah ! If you decide to pull down the branch and try it out running the unit tests yourself in Xcode, keep in mind you'll need to do that with the Xcode 9 beta. |
understood - i'm using Xcode9 Beta2 right now - is that ok? |
s.source_files = '{{projectName}}/Classes/Swaggers/**/*.swift'{{#usePromiseKit}} | ||
s.dependency 'PromiseKit', '~> 4.2.2'{{/usePromiseKit}}{{#useRxSwift}} | ||
s.dependency 'RxSwift', '~> 3.4.1'{{/useRxSwift}} | ||
s.dependency 'Alamofire', '~> 4.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.
I would make this 4.5 since that is when Alamofire fixed Xcode9 related issues.
@jaz-ah : Yes, Xcode 9 beta 2 works - that is what I used to develop this. |
"ErrorResponse", "Response", | ||
|
||
// swift keywords | ||
"Int", "Int32", "Int64", "Int64", "Float", "Double", "Bool", "Void", "String", "Character", "AnyObject", "Any", "Error", "URL", |
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.
let's add Codable as a reserved word here
@ehyche just a couple minor comments if you want to add those I think we're good to merge! |
@jaz-ah : thanks for the review! I'm updating and re-generating the samples now. Will push up changes shortly. |
- Changed Alamofire dependency from 4.0 to 4.5 - Added "Codable", "Encodable", and "Decodable" to list of reserved words in generator - Ran "pod update" in default, promisekit, and rxswift samples test projects
@jaz-ah : I have made updates per your review comments and pushed changes up. |
FYI. Added @ehyche as the template creator of https://twitter.com/wing328/status/883748380718977024 (please help retweet) |
* Add a swift4 client generator * Updates per review comments: - Changed Alamofire dependency from 4.0 to 4.5 - Added "Codable", "Encodable", and "Decodable" to list of reserved words in generator - Ran "pod update" in default, promisekit, and rxswift samples test projects
This is great work, thanks. Let's make sure to keep cherry-picking the patches to the Swift 3 generator up to Swift 4 as we make them. |
Hi all, First off let me just say great work so far @ehyche — glad to see someone jumping on this after the WWDC announcement. A couple of quick comments:
A lot of these seem to be related to the fact that while the Codable protocol is referenced, it's not conformed to properly, and both Encodable and Decodable must be conformed to correctly. This is mostly a problem when you have custom objects in your Model rather than basic types like strings and bools. Anyways, great work again and I look forward to seeing the generator further improved/fixed before the GM hits! |
@amelnychuck : Thanks! Yes, there is only one place that JSONEncodable is still used, and it is on my ToDo list to remove that last reference. Thanks for the reminder. Regarding the errors:
as long as RelayCommand also conforms to Codable. Is RelayCommand also a generated model object? If so, it should also conform to Codable.
Is your schema publicly accessible? If so, I'd be glad to take a look at it, and find and fix the issues in the current swift4 generator. |
@ehyche RelayCommand.swift (in the Models folder of the swift 4 output) contains the following:
Looks like the generator only makes it a type alias of Any. (Possibly this is an issue with my swagger specification?) I'm guessing this is also related to point 2. Unfortunately my schema isn't publicly accessible (I can see if I can create a sample that reproduces the issue though), but I'll keep looking into it and if I can give you any more information to help diagnose the issue with the generation, let me know what I can do to help. |
@amelnychuck : Looks like RelayCommand was generated by this part of model.mustache:
So I would expect {{arrayModelType}} to be something other than "Any" - something like another generated model, or a primitive Swift type. |
Still looking into the issue, but I agree. On further examination, it looks like removing all references to RelayCommand in the swift3 version of the SDK solved that issue and I am able to build a test project containing the swift3 sdk and Alamofire successfully. Looks like that's something I'll need to solve as to why the generator is making that type Any... That being said, when I remove all the references to RelayCommand in my Swift4SDK and sample project (at which point swift3 worked correctly), errors related to that seem to go away, but I still get the list of errors for every instance of |
I've got a question, I need to populate the CodingKeys enum in swift4 classes, to map response keys to variables. I can see it in the mustache template (line 75), but I can't find documentation on how to define my yaml to generate it. |
https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml contains various edge cases (enum models, enum properties, etc) You may want to use it to generate the Swift 4 client to see how different mustache tags are populated. |
Found my issue, i need to use the 2.3.0 Generator not the 2.2.3 generator |
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)2.3.0
branch for breaking (non-backward compatible) changes.Description of the PR
This PR implements the issue referenced here: #6002
This adds a new "swift4" generator. Swift 4 added the "Codable" protocol, which is just a combination of the "Encodable" and "Decodable" protocols. These protocols greatly simplify serialization to JSON and deserialization from JSON in Swift. For example, the Pet model object now just looks like:
As compared to the "swift" and "swift3" generators, there is now no need for all of the generated Decoders in Models.mustache, and there is now no need for the JSONEncodable protocol encodeToJSON() method that previously each model object needed to implement. So this greatly simplified the model objects and reduced the size of the generated code.
NOTE: As of this writing, Swift 4 is only present in the Xcode 9 betas. The swift4 generator samples will not compile in Xcode 8, as it does not have support for Codable.
With this PR, I have:
Future work: