swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[Objective-C] Code generator is not creating enums

Open MrMatthewDavis opened this issue 11 years ago • 16 comments

Enums defined in json models get codegen'd as NSString properties, and no enums are created. This can be observed in the petstore example, which has enum properties but don't get translated in the objective-c codegen.

MrMatthewDavis avatar Aug 31 '15 18:08 MrMatthewDavis

@mad102190 yes, enum is not supported in ObjC at the moment. Do you have cycle to help implement it for ObjC client? You can find enum-related mustache tag in https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/Java/enumClass.mustache (Java client).

wing328 avatar Sep 02 '15 03:09 wing328

@mad102190 by the way, for #971 if you need help with the rebase or the integration test with the ObjC client, please let me know (and thanks again for the PR to add the support of status code and HTTP response header)

FYI. We (the community) have implemented similar feature via https://github.com/swagger-api/swagger-codegen/pull/1127 for Java and C# client.

wing328 avatar Sep 02 '15 03:09 wing328

@wing328 I've submitted a PR that enables support for enums in Objective-C, so we can close this issue once the PR is merged in. As for #971, did we not determine that there could be possible concurrency issues with the responseHeaders property if running on multiple threads? If that's not a problem, I can rebase and re-open the PR. Let me know. I'll take a look at the C# and Java implementation and see how the thread concurrency issue was addressed there.

MrMatthewDavis avatar Sep 03 '15 02:09 MrMatthewDavis

@mad102190 it would be a problem if the developer is using the same ApiClient across all threads. The recommended way is to use an instance of ApiClient per thread (same for C# and Java)

wing328 avatar Sep 03 '15 14:09 wing328

That makes perfect sense. I'll work on rebasing that today.

On Sep 3, 2015, at 7:45 AM, wing328 [email protected] wrote:

@mad102190 it would be a problem if the developer is using the same ApiClient across all threads. The recommended way is to use an instance of ApiClient per thread (same for C# and Java)

— Reply to this email directly or view it on GitHub.

MrMatthewDavis avatar Sep 03 '15 14:09 MrMatthewDavis

@mad102190 take your time. Once again appreciate your contribution to the project.

wing328 avatar Sep 03 '15 15:09 wing328

@wing328 No problem, thank you for being so helpful! I've created a brand new PR for the response headers. Please see #1189.

MrMatthewDavis avatar Sep 03 '15 19:09 MrMatthewDavis

I think that what we want is different from enum types. If you take an example like such:

definitions:
  Example:
    type: string
    enum:
      - First
      - Second

We could easily create an enum class:

typedef NS_ENUM(NSInteger, Example) {
    First, Second
};

But what we really want is a NSString which has the acceptable values of First, Second. Thus creating an enum class is actually a bit odd for the codegen. If you, for example, referenced the enum in another model:

  Example2:
    # references an enum class
    type: object
    properties:
      status:
        $ref: '#/definitions/Example'

What would you expect the payload to be? I'm thinking this:

{
  "status": "First"
}

Am I missing something?

fehguy avatar Dec 18 '15 18:12 fehguy

Has this been worked on by anyone?

ClayAtWork avatar Oct 25 '16 21:10 ClayAtWork

@ClayAtWork I don't think so.

If you've time to work on this, I can show you some good starting points.

wing328 avatar Oct 26 '16 02:10 wing328

@wing328 I'll take a look. Since I am new to the codegen any pointers would be helpful!

sharonjl avatar Dec 09 '16 02:12 sharonjl

@sharonjl thanks for offering help on this. I should have something ready for your review by coming Wed. Will keep you posted.

wing328 avatar Dec 12 '16 09:12 wing328

Is there a config option that affects this? because enums are appearing as useless types which are blocking me from using the generated client NOT NSStrings. Am I missing something? or is this new?

original swagger definition:

"AccountType": {
            "type": "string",
            "x-enumNames": [
                "Checking",
                "Savings"
            ],
            "enum": [
                "Checking",
                "Savings"
            ],
            "description": ""
        }

Generates this objective c

@protocol ACHAccountType
@end

@interface ACHAccountType : ACHObject


@end
#import "ACHAccountType.h"

@implementation ACHAccountType

- (instancetype)init {
  self = [super init];
  if (self) {
    // initialize property's default value, if any
    
  }
  return self;
}


/**
 * Maps json key to property name.
 * This method is used by `JSONModel`.
 */
+ (JSONKeyMapper *)keyMapper {
  return [[JSONKeyMapper alloc] initWithDictionary:@{  }];
}

/**
 * Indicates whether the property with the given name is optional.
 * If `propertyName` is optional, then return `YES`, otherwise return `NO`.
 * This method is used by `JSONModel`.
 */
+ (BOOL)propertyIsOptional:(NSString *)propertyName {

  NSArray *optionalProperties = @[];
  return [optionalProperties containsObject:propertyName];
}

@end

ryanackley avatar Feb 22 '17 19:02 ryanackley

Any update on this?

Xzya avatar Nov 10 '17 09:11 Xzya

@Xzya I don't think anyone is working on it. Do you want to give it a try?

wing328 avatar Nov 13 '17 12:11 wing328

Are there any changes for Objc enums?

MelanyG avatar Mar 15 '21 12:03 MelanyG