-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Conversation
…hDictionary and returning the appropriate class.
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. |
@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 |
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. |
@traviscollins thanks. We'll review and let you know if we've further questions. |
@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. |
No problem. Thanks for all your work on this project. Very useful.
|
@traviscollins testings are good. PR merged. Thanks for the contribution on this. |
Added discriminator support to Objective-C
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.