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
16.77k stars 6.02k forks source link

[Swift4] Interoperability with Objective C #6763

Open inforeqd512 opened 6 years ago

inforeqd512 commented 6 years ago
Description

We're looking to use Swagger codegen with SWG v3.0 to generate the model and api files. However, we need to use these with existing Objective C project eg. Need the model obejcts returned in the response to be stored in local model to be used with ObjC View controllers.

Swagger-codegen version

verion 2.2.3

Swagger declaration file content or url

any file will do. Nothing specific

Command line used for generation

plain generate commanf for -l swift4

Steps to reproduce

generate any SWG v3.0 file into swift classes

Related issues/PRs
Suggest a fix/enhancement

Along with declaring Codeable, which you already do, the model objects need to be subclassed from NSObject, have @objcMembers annotation etc.

wing328 commented 6 years ago

Can you try the latest master? I remember there were PRs merged into master to improve interoperability with ObjC

The SNAPSHOT version of 2.3.0 (master) can be found in the README.

inforeqd512 commented 6 years ago

Thanks for your reply. Can you pls give an indication of when would 2.3.0 be available as a stable version.

wing328 commented 6 years ago

Sorry no ETA. We hope to release it before year end.

inforeqd512 commented 6 years ago

Thanks for this. :-)

inforeqd512 commented 6 years ago

Ran the YAML against codegen Version 2.3.0 but the files still don't show the objective C compatibility annotations... Am I missing some codegen option that will turn it on?

wing328 commented 6 years ago

You probably need the following option enabled:

    objcCompatible
        Add additional properties and methods for Objective-C compatibility (default: false)

Ref: java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar config-help -l swift4

inforeqd512 commented 6 years ago

this is how we tried it java -jar swagger-codegen-2.3.0.jar generate -c swift4-objc-comp.json -i petstore.yaml -l swift4

swift4-objc-comp.json { "objcCompatible":true }

tried it with a value of 1 still did not get the model classes with the objc compatibility annotations. Would it be possible for you to pls attach a sample that works?

inforeqd512 commented 6 years ago

Or maybe can you let me know some possible things I can try?

wing328 commented 6 years ago

(I'll provide a reply later today. Very busy these days...)

inforeqd512 commented 6 years ago

gentle reminder only....

wing328 commented 6 years ago

@inforeqd512 I would suggest you try the following:

1) Swift 3 Petstore client (objcCompatible): https://github.com/swagger-api/swagger-codegen/tree/master/samples/client/petstore/swift3/objcCompatible

2) Swift 4 Petstore client (objcCompatible): https://github.com/swagger-api/swagger-codegen/tree/master/samples/client/petstore/swift4/objcCompatible

If these work for you, then it's likely due to a problem when generating the Swift client in your machine.

inforeqd512 commented 6 years ago

by objective C compatibility I thought the codegen would generate the swift files with @objc @objcmembers keywords so that the functions/properties are exposed to objective c. At least for the model objects deserialised from the response. Is there a way to do this using config for objects that need to be bridged to objective C?

In your above code, also I could not see this happening....

wing328 commented 6 years ago

@inforeqd512 what about customizing the Swift4 templates to add the @objcmembers keywords and use the templates with the -d option?

inforeqd512 commented 6 years ago

yes. was thinking about this as the last resort ... but wanted to avoid tampering with templates so we can just use the out of box implementations each time.. however will give this a go as it would be nice to know how to use this feature.

wing328 commented 6 years ago

@inforeqd512 we can leverage the objcCompatible option to add the @objcmembers keywords. Something like this:

{{#objcCompatible}}
@objcmembers
{{/objcCompatible}}
tsanidas commented 1 year ago

I know this is an old item, but it's still open. Since this ticket was opened, there has also been an option added (useModelClasses) but then taken away so that Swift4/5 always generates struct instead of class. However, #useModelClasses still appears in the template files and is mentioned in the .java source files, but no longer seems to work as an option. I'd like to propose making the following changes to Swift 4 and 5:

@wing328 / @inforeqd512 , if you're still listening, what do you think? I'm working on a PR for these changes now, since have some Objective-C classes that are unable to use the generated Swift classes as-is.