-
Notifications
You must be signed in to change notification settings - Fork 6k
Alternative swift code generator #3072
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
Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/swagger-api/swagger-codegen/graphs/contributors.
Let me know if you need help fixing it.
Ref: http://stackoverflow.com/questions/3042437/change-commit-author-at-one-specific-commit
@mabedan if the current swift generator can achieve the following, do you agree we should just keep one Swift generator?
Handling for different response data type, per status code
Usage of swift enums with associated values, for cleaner response callback parameters
For Handling for different response data type, per status code, do you mean the response data type is different based on the status code? Can you share your Swagger spec with us?
For Usage of swift enums with associated values, can you also share the spec for this and how that's represented in swift?
if the current swift generator can achieve the following, do you agree we should just keep one Swift generator?
The initial reason I started rewriting the swift layer, was the crashes I was experiencing inside Decoder methods (I assume they're already fixed), and I wasn't able to read/understand what was happening in the code.
ObjectMapper library is widely used in the swift community, and I think it makes sense to make use of it, instead of reimplementing it (and to maintain it). Another advantage is that it supports both serlialization/deserialization out of the box.
So to answer your question, for my own project, moving away from ObjectMapper would be a dealbreaker.
One more point which I forgot to mention, is that body parameters are ignored in the original swift implementation. This one supports body parameters, but still lacks form parameters.
For Handling for different response data type, per status code, do you mean the response data type is different based on the status code? Can you share your Swagger spec with us?
By definition, schemas are defined per status code. Here's a code snippet from swagger spec
responses:
'200':
description: pet response
schema:
type: array
items:
$ref: '#/definitions/Pet'
default:
description: error payload
schema:
$ref: '#/definitions/ErrorModel'
Under responses object you can keep adding different status codes, and define a schema for each.
For Usage of swift enums with associated values, can you also share the spec for this and how that's represented in swift?
enum PostEntryResponse {
case Res200(String, NSData)//default
case Res202(DetailsDataModel, NSData)
case Res400(ErrorDataModel, NSData)
case Failure(Int?, NSData?)
}
....
Raygun.requestEntries() {response in
swift response {
case .Res200(let string, _):
print(string)
break
case .Res202(les detailsDataModel, _):
self.presentDetailsViewController(detailsDataModel)
break
case .Res400(les errorDataModel, _):
self.reportError(errorDataModel)
break
case .Failure:
fatalError("don't do this at home")
}
}
The purpose is to have elegant and simple return/callback values which can support multiple types, and can be used within a switch statement.
That works for me too.
Unfortunately I wont have time to actively invest in changing the default generator given that I'm working on this one as part of my job, but would be happy to help other contributors.
Many thanks, @mabedan, your generator is awesome, pretty clean code. Current one even couldn't pass parameters in headers.
Issues:
- For NSDate I've got error on "value.toString("yyyy-MM-dd")". I easily fixed this by NSDate extension 'toString'. Maybe we should use AFDateHelper, which looks pretty good?
- Not possible to add customize headers e.g. for Authorization
- Why 'DataModel' is added to model class names?
- array parameter (Int) with collectionFormat:csv generates (ids: [[Int]], ...) which then written as [[1, 2, 3 ... ]]
- I'd also liked to be able to write generic response handler, so maybe inherit response enums from some type?
- Perhaps replacing response enums with separate callbacks would allow to implement common handlers for responses with statuses like 403/404? Or maybe generate additional wrapper on your requestFunc with wrapping switch statuscodes with callbacks?
Hi all, it's worth sharing with you that swift3 generator has been added to the latest master.
Please pull the latest to give it a try.
In case it's useful for anyone, I took my own approach here as well https://github.com/yonaskolb/SwagGen
@mabedan thanks for the work. Please kindly try the Swift3 or Swift4 generator to see if it meets your requirement.
Uh oh!
There was an error while loading. Please reload this page.
I've decided to create a separate code generator for swift, given that the approach taken is very different than the existing swift code generator.
Main differentiation points:
Here are a few known issues:
podificationHere's an example of spec file + generated swift files. api, model, spec