Skip to content
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

Added discriminator support to Objective-C #2202

Merged
merged 1 commit into from
Mar 3, 2016

Conversation

traviscollins
Copy link

The swagger spec includes a discriminator field for Models. This discriminator value is the name of the property on the model that contains the name of the intended subtype for a given object. See discriminator under Fixed Fields in this page

http://swagger.io/specification/#schemaObject

This pull request adds a mustache conditional template to the objective-c model-object template for when the discriminator is defined for a given swagger model. The condition adds a custom initWithDictionary method that uses the value found in the dictionary to determine the appropriate class to return. This causes the discriminator value to be automatically applied per the spec. This implementation fails gracefully when the discriminator is defined but not supplied. The implementation fails hard when the discriminator is defined, and the value defines a class that is not present at runtime.

Implementing this support required adding the discriminator value from the Swagger Model to CodeGen Model converter. This property can be used to add inheritance support to other languages.

…hDictionary and returning the appropriate class.
@traviscollins
Copy link
Author

A little more clarification on the intention of discriminators and what this pull request does.

Consider a Swagger definition with three models - Animal, Dog, and Cat. Dog and Cat "inherit" Animal through the "allOf" definition. Animal has a discriminator value of "className" - which means the field "className" on Animal contains either Dog or Cat. This is how you define class inheritance in the Swagger spec. Now imagine the same Swagger spec defines a method GET /pets that returns a list (array) of Animal objects that are all either Dog or Cat.

Prior to this pull request, the Objective-C swagger client would parse all of the JSON returned down to Animal objects - and strip out all Dog and Cat specific properties. It did not obey the discriminator value, and would never return Dog or Cat. Therefore you could not really use inheritance in returned objects with the swagger Objective-C client.

After this pull request, the objective-c client will parse the JSON, use the discriminator field value, and return an array of Dog and Cat objects. The subtype specific properties are preserved, and now inheritance can be used on objects returned from the api.

@wing328
Copy link
Contributor

wing328 commented Feb 22, 2016

@traviscollins thanks for the PR. Do you mind sharing with me the spec so that I can use it for testing? (you will find my email address in my Github page).

Also I notice that in the Commits tab, the commit is not linked to your Github account (@traviscollins) so the contribution won't count toward https://github.com/swagger-api/swagger-codegen/graphs/contributors. I would recommend you to fix it by probably setting up user.email following the instruction here

@wing328 wing328 added this to the v2.1.6 milestone Feb 22, 2016
@traviscollins
Copy link
Author

Thanks for that tip about the email setting. Fixed it for next time.

I included an example discriminator using spec in the pull request.

modules/swagger-codegen/src/test/resources/2_0/discriminatorTest.json

I included a JUnit test for the CodeGenModel discriminator property assignment in the pull request. But I wasn't able to determine a viable option for testing the output of the objective-c codegen config against that spec.

@wing328
Copy link
Contributor

wing328 commented Feb 22, 2016

@traviscollins thanks. We'll review and let you know if we've further questions.

@wing328
Copy link
Contributor

wing328 commented Feb 24, 2016

@traviscollins thanks again for the PR. To manage your expectation, I'll need a little bit more time to review and will provide you another update this weekend.

@traviscollins
Copy link
Author

No problem. Thanks for all your work on this project. Very useful.

On Feb 24, 2016, at 9:54 AM, wing328 [email protected] wrote:

@traviscollins https://github.com/traviscollins thanks again for the PR. To manage your expectation, I'll need a little bit more time to review and will provide you another update this weekend.


Reply to this email directly or view it on GitHub #2202 (comment).

@wing328
Copy link
Contributor

wing328 commented Mar 3, 2016

@traviscollins testings are good. PR merged. Thanks for the contribution on this.

wing328 added a commit that referenced this pull request Mar 3, 2016
Added discriminator support to Objective-C
@wing328 wing328 merged commit d28f860 into swagger-api:master Mar 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants