Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Closed
mabedan wants to merge 14 commits into swagger-api:master from CHECK24:master
Closed

Alternative swift code generator #3072

mabedan wants to merge 14 commits into swagger-api:master from CHECK24:master

Conversation

@mabedan
Copy link

@mabedan mabedan commented Jun 7, 2016
edited
Loading

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:

  • Handling for different response data type, per status code
  • Usage of swift enums with associated values, for cleaner response callback parameters

Here are a few known issues:

  • handling of NSDate in query parameters needs to be improved
  • Inclusion of query parameters, in case there're body parameters as well
  • Lack of unit tests
  • possibility of class name collision, in case of generating multiple spec files. Could be solved by podification

Here's an example of spec file + generated swift files. api, model, spec

badeleux and KaushalElsewhere reacted with thumbs up emoji
Copy link
Contributor

wing328 commented Jun 7, 2016

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 reacted with thumbs up emoji

Copy link
Contributor

wing328 commented Jun 7, 2016

Copy link
Contributor

wing328 commented Jun 7, 2016

@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?

Copy link
Author

mabedan commented Jun 7, 2016
edited
Loading

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.

@mabedan mabedan changed the title (削除) alternative swift code generator (削除ここまで) (追記) Alternative swift code generator (追記ここまで) Jun 7, 2016
Copy link
Contributor

Edubits commented Jun 8, 2016

Hi @mabedan, thanks for your contribution. I would prefer to see if we can use your changes to improve the default generator. Using ObjectMapper was something suggested earlier by @wing328 in #2194 and would be a great improvement.

wing328 reacted with thumbs up emoji

Copy link
Author

mabedan commented Jun 8, 2016

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.

@wing328 wing328 modified the milestones: v2.3.0, v2.2.0 Jul 7, 2016
Copy link

Erdemus commented Jul 21, 2016
edited
Loading

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?

@wing328 wing328 modified the milestones: v2.2.1, v2.3.0 Aug 8, 2016
Copy link
Contributor

wing328 commented Oct 15, 2016

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.

Copy link
Contributor

yonaskolb commented May 4, 2017
edited
Loading

In case it's useful for anyone, I took my own approach here as well https://github.com/yonaskolb/SwagGen

Copy link
Contributor

wing328 commented Sep 7, 2017

@mabedan thanks for the work. Please kindly try the Swift3 or Swift4 generator to see if it meets your requirement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Projects

None yet

Milestone

v2.3.0

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /