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
17.05k stars 6.03k forks source link

[Objective-C] Code generator is not creating enums #1167

Open MrMatthewDavis opened 9 years ago

MrMatthewDavis commented 9 years ago

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.

wing328 commented 9 years ago

@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 commented 9 years ago

@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.

MrMatthewDavis commented 9 years ago

@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.

wing328 commented 9 years ago

@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)

MrMatthewDavis commented 9 years ago

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

On Sep 3, 2015, at 7:45 AM, wing328 notifications@github.com 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.

wing328 commented 9 years ago

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

MrMatthewDavis commented 9 years ago

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

fehguy commented 8 years ago

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?

ClayAtWork commented 8 years ago

Has this been worked on by anyone?

wing328 commented 8 years ago

@ClayAtWork I don't think so.

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

sharonjl commented 7 years ago

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

wing328 commented 7 years ago

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

ryanackley commented 7 years ago

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
Xzya commented 7 years ago

Any update on this?

wing328 commented 7 years ago

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

MelanyG commented 3 years ago

Are there any changes for Objc enums?