diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java index 54fa020922f..b7c38c703ca 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java @@ -43,6 +43,7 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { defaultIncludes = new HashSet( Arrays.asList( "bool", + "BOOL", "int", "NSString", "NSObject", @@ -59,22 +60,32 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { "NSString", "NSObject", "NSDate", - "bool") + "bool", + "BOOL") ); + // ref: http://www.tutorialspoint.com/objective_c/objective_c_basic_syntax.htm reservedWords = new HashSet( Arrays.asList( - "void", "char", "short", "int", "void", "char", "short", "int", - "long", "float", "double", "signed", "unsigned", "id", "const", - "volatile", "in", "out", "inout", "bycopy", "byref", "oneway", - "self", "super", "description" + "auto", "else", "long", "switch", + "break", "enum", "register", "typedef", + "case", "extern", "return", "union", + "char", "float", "short", "unsigned", + "const", "for", "signed", "void", + "continue", "goto", "sizeof", "volatile", + "default", "if", "id", "static", "while", + "do", "int", "struct", "_Packed", + "double", "protocol", "interface", "implementation", + "NSObject", "NSInteger", "NSNumber", "CGFloat", + "property", "nonatomic", "retain", "strong", + "weak", "unsafe_unretained", "readwrite", "readonly" )); typeMapping = new HashMap(); typeMapping.put("enum", "NSString"); typeMapping.put("Date", "NSDate"); typeMapping.put("DateTime", "NSDate"); - typeMapping.put("boolean", "NSNumber"); + typeMapping.put("boolean", "BOOL"); typeMapping.put("string", "NSString"); typeMapping.put("integer", "NSNumber"); typeMapping.put("int", "NSNumber"); @@ -188,7 +199,7 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { return swaggerType; } // In this codition, type of p is objective-c object type, e.g. `SWGPet', - // return type of p with pointer, e.g. `' + // return type of p with pointer, e.g. `SWGPet*' else { return swaggerType + "*"; } @@ -197,18 +208,28 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { @Override public String toModelName(String type) { + type = type.replaceAll("[^0-9a-zA-Z_]", "_"); + + // language build-in classes if(typeMapping.keySet().contains(type) || foundationClasses.contains(type) || importMapping.values().contains(type) || defaultIncludes.contains(type) || languageSpecificPrimitives.contains(type)) { - return Character.toUpperCase(type.charAt(0)) + type.substring(1); + return camelize(type); } + // custom classes else { - return PREFIX + Character.toUpperCase(type.charAt(0)) + type.substring(1); + return PREFIX + camelize(type); } } + @Override + public String toModelFilename(String name) { + // should be the same as the model name + return toModelName(name); + } + @Override protected void setNonArrayMapProperty(CodegenProperty property, String type) { super.setNonArrayMapProperty(property, type); @@ -243,28 +264,34 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { return outputFolder + File.separator + sourceFolder; } - @Override - public String toModelFilename(String name) { - return PREFIX + initialCaps(name); - } - @Override public String toApiName(String name) { - return PREFIX + initialCaps(name) + "Api"; + return PREFIX + camelize(name) + "Api"; } public String toApiFilename(String name) { - return PREFIX + initialCaps(name) + "Api"; + return PREFIX + camelize(name) + "Api"; } @Override public String toVarName(String name) { - String paramName = name.replaceAll("[^a-zA-Z0-9_]",""); - if(paramName.startsWith("new") || reservedWords.contains(paramName)) { - return escapeReservedWord(paramName); - } - else - return paramName; + // replace non-word characters to `_` + // e.g. `created-at` to `created_at` + name = name.replaceAll("[^a-zA-Z0-9_]","_"); + + // if it's all upper case, do noting + if (name.matches("^[A-Z_]$")) + return name; + + // camelize (lower first character) the variable name + // e.g. `pet_id` to `petId` + name = camelize(name, true); + + // for reserved word or word starting with number, prepend `_` + if (reservedWords.contains(name) || name.matches("^\\d.*")) + name = escapeReservedWord(name); + + return name; } @Override diff --git a/samples/client/petstore/objc/PetstoreClient.xcworkspace/contents.xcworkspacedata b/samples/client/petstore/objc/PetstoreClient.xcworkspace/contents.xcworkspacedata index 834f64caf7e..4ea15119171 100644 --- a/samples/client/petstore/objc/PetstoreClient.xcworkspace/contents.xcworkspacedata +++ b/samples/client/petstore/objc/PetstoreClient.xcworkspace/contents.xcworkspacedata @@ -1 +1,10 @@ - \ No newline at end of file + + + + + + + diff --git a/samples/client/petstore/objc/PetstoreClient/PetstoreClient.xcodeproj/project.pbxproj b/samples/client/petstore/objc/PetstoreClient/PetstoreClient.xcodeproj/project.pbxproj index 24cb04d3f7c..c22d57fa0ac 100644 --- a/samples/client/petstore/objc/PetstoreClient/PetstoreClient.xcodeproj/project.pbxproj +++ b/samples/client/petstore/objc/PetstoreClient/PetstoreClient.xcodeproj/project.pbxproj @@ -39,7 +39,6 @@ EAEA85EE1811D3AE00F06E69 /* SWGUser.m in Sources */ = {isa = PBXBuildFile; fileRef = EAEA85E11811D3AE00F06E69 /* SWGUser.m */; }; EAEA85EF1811D3AE00F06E69 /* SWGUserApi.m in Sources */ = {isa = PBXBuildFile; fileRef = EAEA85E31811D3AE00F06E69 /* SWGUserApi.m */; }; EAFBEABB1A925B8500A27431 /* test-1.png in Resources */ = {isa = PBXBuildFile; fileRef = EAFBEABA1A925B8500A27431 /* test-1.png */; }; - EAFBEABE1A92C42700A27431 /* SWGApiResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = EAFBEABD1A92C42700A27431 /* SWGApiResponse.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -105,8 +104,6 @@ EAEA85E21811D3AE00F06E69 /* SWGUserApi.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SWGUserApi.h; sourceTree = ""; }; EAEA85E31811D3AE00F06E69 /* SWGUserApi.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SWGUserApi.m; sourceTree = ""; }; EAFBEABA1A925B8500A27431 /* test-1.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "test-1.png"; sourceTree = ""; }; - EAFBEABC1A92C42700A27431 /* SWGApiResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SWGApiResponse.h; sourceTree = ""; }; - EAFBEABD1A92C42700A27431 /* SWGApiResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SWGApiResponse.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -229,8 +226,6 @@ children = ( EA8B8AA21AC6683700638FBB /* SWGQueryParamCollection.h */, EA8B8AA31AC6683700638FBB /* SWGQueryParamCollection.m */, - EAFBEABC1A92C42700A27431 /* SWGApiResponse.h */, - EAFBEABD1A92C42700A27431 /* SWGApiResponse.m */, EAEA85CC1811D3AE00F06E69 /* SWGApiClient.h */, EAEA85CD1811D3AE00F06E69 /* SWGApiClient.m */, EAEA85CE1811D3AE00F06E69 /* SWGCategory.h */, @@ -400,7 +395,6 @@ EA6699B31811D2FA00A70D03 /* ViewController.m in Sources */, EA6699AA1811D2FA00A70D03 /* AppDelegate.m in Sources */, EAEA85EE1811D3AE00F06E69 /* SWGUser.m in Sources */, - EAFBEABE1A92C42700A27431 /* SWGApiResponse.m in Sources */, EAEA85EF1811D3AE00F06E69 /* SWGUserApi.m in Sources */, EAEA85EB1811D3AE00F06E69 /* SWGPetApi.m in Sources */, EAEA85E61811D3AE00F06E69 /* SWGDate.m in Sources */, diff --git a/samples/client/petstore/objc/client/SWGApiResponse.h b/samples/client/petstore/objc/client/SWGApiResponse.h deleted file mode 100644 index 2021a408f03..00000000000 --- a/samples/client/petstore/objc/client/SWGApiResponse.h +++ /dev/null @@ -1,18 +0,0 @@ -#import -#import "SWGObject.h" - - -@interface SWGApiResponse : SWGObject - -@property(nonatomic) NSNumber* code; -@property(nonatomic) NSString* type; -@property(nonatomic) NSString* message; -- (id) code: (NSNumber*) code - type: (NSString*) type - message: (NSString*) message; - - -- (id) initWithValues: (NSDictionary*)dict; -- (NSDictionary*) asDictionary; - -@end diff --git a/samples/client/petstore/objc/client/SWGApiResponse.m b/samples/client/petstore/objc/client/SWGApiResponse.m deleted file mode 100644 index b6391774790..00000000000 --- a/samples/client/petstore/objc/client/SWGApiResponse.m +++ /dev/null @@ -1,54 +0,0 @@ -#import "SWGDate.h" -#import "SWGApiResponse.h" - -@implementation SWGApiResponse - --(id)code: (NSNumber*) code - type: (NSString*) type - message: (NSString*) message - -{ - _code = code; - _type = type; - _message = message; - - - return self; -} - --(id) initWithValues:(NSDictionary*)dict -{ - self = [super init]; - if(self) { - _code = dict[@"code"]; - - _type = dict[@"type"]; - - _message = dict[@"message"]; - - - } - return self; -} - --(NSDictionary*) asDictionary { - NSMutableDictionary* dict = [[NSMutableDictionary alloc] init]; - - - if(_code != nil) dict[@"code"] = _code ; - - - - if(_type != nil) dict[@"type"] = _type ; - - - - if(_message != nil) dict[@"message"] = _message ; - - - - NSDictionary* output = [dict copy]; - return output; -} - -@end diff --git a/samples/client/petstore/objc/client/SWGOrder.h b/samples/client/petstore/objc/client/SWGOrder.h index 2e71906cd53..c4f4f6e81ad 100644 --- a/samples/client/petstore/objc/client/SWGOrder.h +++ b/samples/client/petstore/objc/client/SWGOrder.h @@ -19,6 +19,6 @@ */ @property(nonatomic) NSString* status; -@property(nonatomic) NSNumber* complete; +@property(nonatomic) BOOL complete; @end diff --git a/samples/client/petstore/objc/client/SWGPetApi.h b/samples/client/petstore/objc/client/SWGPetApi.h index 40d475d5e0a..e7a3a84a9f5 100644 --- a/samples/client/petstore/objc/client/SWGPetApi.h +++ b/samples/client/petstore/objc/client/SWGPetApi.h @@ -116,13 +116,13 @@ Deletes a pet - @param api_key + @param apiKey @param petId Pet id to delete return type: */ --(NSNumber*) deletePetWithCompletionBlock :(NSString*) api_key +-(NSNumber*) deletePetWithCompletionBlock :(NSString*) apiKey petId:(NSNumber*) petId diff --git a/samples/client/petstore/objc/client/SWGPetApi.m b/samples/client/petstore/objc/client/SWGPetApi.m index dca990cb118..62bc34acbee 100644 --- a/samples/client/petstore/objc/client/SWGPetApi.m +++ b/samples/client/petstore/objc/client/SWGPetApi.m @@ -587,11 +587,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; /*! * Deletes a pet * - * \param api_key + * \param apiKey * \param petId Pet id to delete * \returns void */ --(NSNumber*) deletePetWithCompletionBlock: (NSString*) api_key +-(NSNumber*) deletePetWithCompletionBlock: (NSString*) apiKey petId: (NSNumber*) petId @@ -616,8 +616,8 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; NSMutableDictionary* headerParams = [NSMutableDictionary dictionaryWithDictionary:self.defaultHeaders]; - if(api_key != nil) - headerParams[@"api_key"] = api_key; + if(apiKey != nil) + headerParams[@"api_key"] = apiKey; id bodyDictionary = nil;