swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.01k stars 6.03k forks source link

Swift code generator produces code with compile warnings #4098

Open davidsantiago opened 8 years ago

davidsantiago commented 8 years ago
Description

The code generated by Swagger-codegen for Swift will produce a file, AlamofireImplementations.swift, which will have a number of compile warnings, all of them the same:

Treating a forced downcast to 'T' as optional will never produce 'nil'

There are a few different places that cause this, but here's an example:

                    completion(response: Response(response: response.response!, body: () as! T), error: nil)

where the warning is on "as!". Seems like an optional cast (as?) would fix these.

Swagger-codegen version

Using version 2.2.2-SNAPSHOT on master.

Swagger declaration file content or url

I don't have a swagger file to post here, because I can see already that this is invariant to the swagger declaration file. The file in question is mostly pre-defined as is in the template: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/swift/AlamofireImplementations.mustache

The warnings come on lines 172, 187, and 197.

Command line used for generation

This is run from gradle with a custom task I wrote in groovy to call the DefaultGenerator. Again, I don't believe this makes any difference.

Steps to reproduce

Generate a client library for swift and then try to compile that swift.

Related issues

My searched turned up no related issues.

Suggest a Fix

I think optional casts in the template file in question would fix it.

jaz-ah commented 8 years ago

David I don't see these on the latest swift3 implementation - are you using that one or the older swift2 version?

davidsantiago commented 8 years ago

Using the current swift2 implementation; we can't quite upgrade to swift3 yet.

jaz-ah commented 8 years ago

if you'd like to submit a PR to that, it sounds like a reasonable answer...

wing328 commented 7 years ago

@davebaird let us know if you need help with the PR.

jaz-ah commented 7 years ago

@wing328 I'd like to obsolete any < Swift3 issues at this point so we can focus on Swift3/4

wing328 commented 7 years ago

@jaz-ah totally agree it has been almost a year when Swift3 was released: https://swift.org/blog/swift-3-0-released/ and we should definitely concentrate our efforts on Swift 3.x, 4.x

I'll update the help text for swift generator and add a FAQ on that over the weekend.

cc @jgavris @RobBlairInq @ehyche

jgavris commented 7 years ago

Sounds good

wing328 commented 7 years ago

UPDATE: