-
Notifications
You must be signed in to change notification settings - Fork 6k
[Obj-C] Added support for enums in Objective-C #1185
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
Conversation
I ran the integration test for objc petstore sample and got an error as follows:
I wonder if you can take a look. To run the integration test, please run the following under
|
Weird. I'll take a look.
|
So from what I can tell, I think this is actually due to the fact that the swagger.json defines the enum multiple times for different classes. This causes the enum to be generated with the same name (StatusEnum) in multiple different classes (in this case, SWGPet and SWGOrder). Possible solutions/workarounds:
That's all I can think of right now. Do you have any other ideas on how to get around this issue @wing328 ? |
@mad102190 thanks for the detailed investigation. I think your 2nd approach is similar to the existing Java approach. Here is the code for Pet.java:
StatusEnum is put inside a class so it won't conflict with other enum definition. Ideally all enum should be put into a single file SWGEnums.h as you've pointed out in the 3rd approach but it would take too much effort. I would go with your 2nd approach for the time being before anyone has cycle to implement the 3rd approach. |
The thing is, these enums need to be defined in the header files so that they can be accessed externally, and in obj-c, this will cause name conflicts if they're named the same. So I agree that the 2nd option works for now, it just causes the enums' names to be fairly convoluted ( This approach allows it to work (albeit messily) without having to make any modifications to the swagger.json naming conventions already put in place, but takes away the ability to explicitly name the enums to a specific name (for example, if I specifically wanted an enum called |
Furthermore, enums in objective-c can't be represented as NSString objects (or have string values at all), only NSIntegers. So the problem is now that the swagger.json defines them as strings and the API endpoints expect a string parameter to compare against. So if an api is called with a pet whose status property is simply |
Given the limitation of enum in ObjC. I would suggest we implement the enum support using the current Python approach, which adds logic in the property setter to validate the value against the enumerated values. Here is an example: pet.py#L194 and the corresponding template It's not a perfect solution but it serves the purpose of validation against the input value. Not sure how easy/difficult to implement something similar in ObjC but I think this approach is worth considering. |
@mad102190 thanks again for the PR on this. We'll work on providing better support of enum for ObjC client in v2.2.0. |
Hi there, |
Enums are now generated in the model-header for the class that they're part of.