From 4dd793c0ac65c109501c528826fe06f0b828be15 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Thu, 16 Apr 2015 17:34:11 +0800 Subject: [PATCH 1/3] Updated naming convention in objc client. --- .../codegen/languages/ObjcClientCodegen.java | 71 +++++++++++++------ .../petstore/objc/client/SWGApiResponse.h | 18 ----- .../petstore/objc/client/SWGApiResponse.m | 54 -------------- .../client/petstore/objc/client/SWGOrder.h | 2 +- .../client/petstore/objc/client/SWGPetApi.h | 4 +- .../client/petstore/objc/client/SWGPetApi.m | 8 +-- 6 files changed, 56 insertions(+), 101 deletions(-) delete mode 100644 samples/client/petstore/objc/client/SWGApiResponse.h delete mode 100644 samples/client/petstore/objc/client/SWGApiResponse.m 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/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; From 60ab624b23ba1620c09bce847a5d7fee9c2345f3 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Thu, 16 Apr 2015 18:39:21 +0800 Subject: [PATCH 2/3] Updated xcode project for petstore --- .../contents.xcworkspacedata | 11 ++++++++++- .../xcshareddata/PetstoreClient.xccheckout | 6 +++--- .../PetstoreClient.xcodeproj/project.pbxproj | 6 ------ 3 files changed, 13 insertions(+), 10 deletions(-) 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.xcworkspace/xcshareddata/PetstoreClient.xccheckout b/samples/client/petstore/objc/PetstoreClient.xcworkspace/xcshareddata/PetstoreClient.xccheckout index fafd21fb8eb..3d473491e3d 100644 --- a/samples/client/petstore/objc/PetstoreClient.xcworkspace/xcshareddata/PetstoreClient.xccheckout +++ b/samples/client/petstore/objc/PetstoreClient.xcworkspace/xcshareddata/PetstoreClient.xccheckout @@ -5,13 +5,13 @@ IDESourceControlProjectFavoriteDictionaryKey IDESourceControlProjectIdentifier - 81EB09FA-DD8C-4FE1-82D3-1FB6FF0D9C43 + 06538C1C-B57B-41A2-BDB2-686B4A83D75C IDESourceControlProjectName PetstoreClient IDESourceControlProjectOriginsDictionary E5BBF0AA85077C865C95437976D06D819733A208 - ssh://github.com/wordnik/swagger-codegen.git + https://github.com/geekerzp/swagger-codegen.git IDESourceControlProjectPath samples/client/petstore/objc/PetstoreClient.xcworkspace @@ -21,7 +21,7 @@ ../../../../.. IDESourceControlProjectURL - ssh://github.com/wordnik/swagger-codegen.git + https://github.com/geekerzp/swagger-codegen.git IDESourceControlProjectVersion 111 IDESourceControlProjectWCCIdentifier 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 */, From 152eabbd6a8eab9fc0b5858b3498ee7102ad36a0 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Tue, 21 Apr 2015 16:33:28 +0800 Subject: [PATCH 3/3] Fixed `PetstoreClient.xccheckout`. --- .../xcshareddata/PetstoreClient.xccheckout | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/client/petstore/objc/PetstoreClient.xcworkspace/xcshareddata/PetstoreClient.xccheckout b/samples/client/petstore/objc/PetstoreClient.xcworkspace/xcshareddata/PetstoreClient.xccheckout index 3d473491e3d..fafd21fb8eb 100644 --- a/samples/client/petstore/objc/PetstoreClient.xcworkspace/xcshareddata/PetstoreClient.xccheckout +++ b/samples/client/petstore/objc/PetstoreClient.xcworkspace/xcshareddata/PetstoreClient.xccheckout @@ -5,13 +5,13 @@ IDESourceControlProjectFavoriteDictionaryKey IDESourceControlProjectIdentifier - 06538C1C-B57B-41A2-BDB2-686B4A83D75C + 81EB09FA-DD8C-4FE1-82D3-1FB6FF0D9C43 IDESourceControlProjectName PetstoreClient IDESourceControlProjectOriginsDictionary E5BBF0AA85077C865C95437976D06D819733A208 - https://github.com/geekerzp/swagger-codegen.git + ssh://github.com/wordnik/swagger-codegen.git IDESourceControlProjectPath samples/client/petstore/objc/PetstoreClient.xcworkspace @@ -21,7 +21,7 @@ ../../../../.. IDESourceControlProjectURL - https://github.com/geekerzp/swagger-codegen.git + ssh://github.com/wordnik/swagger-codegen.git IDESourceControlProjectVersion 111 IDESourceControlProjectWCCIdentifier