From fbe069cf5cb664d1e08145fe563557c272d5c600 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Thu, 26 Mar 2015 10:07:33 +0800 Subject: [PATCH] add jsonmodel in objective-c client --- .../codegen/languages/ObjcClientCodegen.java | 53 ++++++++++--- .../src/main/resources/objc/Podfile.mustache | 1 + .../src/main/resources/objc/SWGObject.h | 5 +- .../src/main/resources/objc/SWGObject.m | 13 ---- .../main/resources/objc/model-body.mustache | 75 +------------------ .../main/resources/objc/model-header.mustache | 15 ++-- .../src/test/scala/Objc/ObjcModelTest.scala | 11 +-- 7 files changed, 61 insertions(+), 112 deletions(-) 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 2e91cafa992..96d491aea7c 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 @@ -48,6 +48,7 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { "NSObject", "NSArray", "NSNumber", + "NSDate", "NSDictionary", "NSMutableArray", "NSMutableDictionary") @@ -57,6 +58,7 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { "NSNumber", "NSString", "NSObject", + "NSDate", "bool") ); @@ -70,9 +72,8 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { typeMapping = new HashMap(); typeMapping.put("enum", "NSString"); - typeMapping.put("Date", "SWGDate"); - typeMapping.put("DateTime", "SWGDate"); - // typeMapping.put("Date", "SWGDate"); + typeMapping.put("Date", "NSDate"); + typeMapping.put("DateTime", "NSDate"); typeMapping.put("boolean", "NSNumber"); typeMapping.put("string", "NSString"); typeMapping.put("integer", "NSNumber"); @@ -87,13 +88,13 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { typeMapping.put("object", "NSObject"); importMapping = new HashMap (); - importMapping.put("Date", "SWGDate"); foundationClasses = new HashSet ( Arrays.asList( "NSNumber", "NSObject", "NSString", + "NSDate", "NSDictionary") ); @@ -151,11 +152,45 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { @Override public String getTypeDeclaration(Property p) { - String swaggerType = getSwaggerType(p); - if(languageSpecificPrimitives.contains(swaggerType) && !foundationClasses.contains(swaggerType)) - return toModelName(swaggerType); - else - return swaggerType + "*"; + if (p instanceof ArrayProperty) { + ArrayProperty ap = (ArrayProperty) p; + Property inner = ap.getItems(); + String innerType = getSwaggerType(inner); + + // In this codition, type of property p is array of primitive, + // return container type with pointer, e.g. `NSArray*' + if (languageSpecificPrimitives.contains(innerType)) + return getSwaggerType(p) + "*"; + + // In this codition, type of property p is array of model, + // return container type combine inner type with pointer, e.g. `NSArray*' + String innerTypeDeclaration = getTypeDeclaration(inner); + + if (innerTypeDeclaration.endsWith("*")) + innerTypeDeclaration = innerTypeDeclaration.substring(0, innerTypeDeclaration.length() - 1); + + return getSwaggerType(p) + "<" + innerTypeDeclaration + ">*"; + } + else { + String swaggerType = getSwaggerType(p); + + // In this codition, type of p is objective-c primitive type, e.g. `NSSNumber', + // return type of p with pointer, e.g. `NSNumber*' + if (languageSpecificPrimitives.contains(swaggerType) && + foundationClasses.contains(swaggerType)) { + return swaggerType + "*"; + } + // In this codition, type of p is c primitive type, e.g. `bool', + // return type of p, e.g. `bool' + else if (languageSpecificPrimitives.contains(swaggerType)) { + return swaggerType; + } + // In this codition, type of p is objective-c object type, e.g. `SWGPet', + // return type of p with pointer, e.g. `' + else { + return swaggerType + "*"; + } + } } @Override diff --git a/modules/swagger-codegen/src/main/resources/objc/Podfile.mustache b/modules/swagger-codegen/src/main/resources/objc/Podfile.mustache index ea7deab2625..1a9d463c86a 100644 --- a/modules/swagger-codegen/src/main/resources/objc/Podfile.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/Podfile.mustache @@ -1,3 +1,4 @@ platform :ios, '6.0' xcodeproj '{{projectName}}/{{projectName}}.xcodeproj' pod 'AFNetworking', '~> 2.1' +pod 'JSONModel', '~> 1.0' diff --git a/modules/swagger-codegen/src/main/resources/objc/SWGObject.h b/modules/swagger-codegen/src/main/resources/objc/SWGObject.h index 031bae69279..ccec208abc0 100644 --- a/modules/swagger-codegen/src/main/resources/objc/SWGObject.h +++ b/modules/swagger-codegen/src/main/resources/objc/SWGObject.h @@ -1,6 +1,5 @@ #import +#import "JSONModel.h" -@interface SWGObject : NSObject -- (id) initWithValues:(NSDictionary*)dict; -- (NSDictionary*) asDictionary; +@interface SWGObject : JSONModel @end diff --git a/modules/swagger-codegen/src/main/resources/objc/SWGObject.m b/modules/swagger-codegen/src/main/resources/objc/SWGObject.m index 9b37b3592b1..1f897ab1d56 100644 --- a/modules/swagger-codegen/src/main/resources/objc/SWGObject.m +++ b/modules/swagger-codegen/src/main/resources/objc/SWGObject.m @@ -1,17 +1,4 @@ #import "SWGObject.h" @implementation SWGObject - -- (id) initWithValues:(NSDictionary*)dict { - return self; -} - -- (NSDictionary*) asDictionary{ - return [NSDictionary init]; -} - -- (NSString*)description { - return [NSString stringWithFormat:@"%@ %@", [super description], [self asDictionary]]; -} - @end diff --git a/modules/swagger-codegen/src/main/resources/objc/model-body.mustache b/modules/swagger-codegen/src/main/resources/objc/model-body.mustache index 17d2ec80836..f27d1d67dbc 100644 --- a/modules/swagger-codegen/src/main/resources/objc/model-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/model-body.mustache @@ -1,81 +1,12 @@ {{#models}} {{#model}} -#import "SWGDate.h" #import "{{classname}}.h" @implementation {{classname}} - --(id){{#vars}}{{name}}: ({{datatype}}) {{name}} - {{/vars}} -{{newline}}{ - {{#vars}}_{{name}} = {{name}}; - {{/vars}} - - return self; -} - --(id) initWithValues:(NSDictionary*)dict + ++ (JSONKeyMapper *)keyMapper { - self = [super init]; - if(self) { - {{#vars}}{{#isPrimitiveType}}_{{name}} = dict[@"{{baseName}}"];{{/isPrimitiveType}} - {{#complexType}} - id {{name}}_dict = dict[@"{{baseName}}"]; - {{#isContainer}} - if([{{name}}_dict isKindOfClass:[NSArray class]]) { - NSMutableArray * objs = [[NSMutableArray alloc] initWithCapacity:[(NSArray*){{name}}_dict count]]; - if([(NSArray*){{name}}_dict count] > 0) { - for (NSDictionary* dict in (NSArray*){{name}}_dict) { - {{{complexType}}}* d = [[{{#instantiationType}}NSClassFromString(@"{{{instantiationType}}}") {{/instantiationType}}{{^instantiationType}}{{{complexType}}}{{/instantiationType}} alloc] initWithValues:dict]; - [objs addObject:d]; - } - _{{{name}}} = [[NSArray alloc] initWithArray:objs]; - } - else - _{{name}} = [[NSArray alloc] init]; - } - else { - _{{name}} = [[NSArray alloc] init]; - } - {{/isContainer}}{{#isNotContainer}} - if({{name}}_dict != nil) - _{{name}} = [[{{#instantiationType}}NSClassFromString(@"{{{instantiationType}}}") {{/instantiationType}}{{^instantiationType}}{{{complexType}}} {{/instantiationType}} alloc]{{setter}}:{{name}}_dict]; - {{/isNotContainer}} - {{/complexType}} - {{/vars}}{{newline}} - } - return self; -} - --(NSDictionary*) asDictionary { - NSMutableDictionary* dict = [[NSMutableDictionary alloc] init]; - {{#vars}} - {{#complexType}} - if(_{{name}} != nil){ - if([_{{name}} isKindOfClass:[NSArray class]]){ - NSMutableArray * array = [[NSMutableArray alloc] init]; - for( {{complexType}} *{{name}} in (NSArray*)_{{name}}) { - [array addObject:[(SWGObject*){{name}} asDictionary]]; - } - dict[@"{{name}}"] = array; - } - else if(_{{name}} && [_{{name}} isKindOfClass:[SWGDate class]]) { - NSString * dateString = [(SWGDate*)_{{name}} toString]; - if(dateString){ - dict[@"{{name}}"] = dateString; - } - } - else { - {{/complexType}} - if(_{{name}} != nil) dict[@"{{baseName}}"] = {{#complexType}}[(SWGObject*){{/complexType}}_{{name}} {{#complexType}}asDictionary]{{/complexType}}; - {{#complexType}} - } - } - {{/complexType}} - {{/vars}} - - NSDictionary* output = [dict copy]; - return output; + return [[JSONKeyMapper alloc] initWithDictionary:@{ {{#vars}}@"{{baseName}}": @"{{name}}"{{#hasMore}}, {{/hasMore}}{{/vars}} }]; } {{/model}} diff --git a/modules/swagger-codegen/src/main/resources/objc/model-header.mustache b/modules/swagger-codegen/src/main/resources/objc/model-header.mustache index cb954f91210..e1e9a85a2d5 100644 --- a/modules/swagger-codegen/src/main/resources/objc/model-header.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/model-header.mustache @@ -6,17 +6,16 @@ {{#models}} {{#model}} +@protocol {{classname}} +@end + @interface {{classname}} : SWGObject {{#vars}} -@property(nonatomic) {{datatype}} {{name}}; {{#description}}/* {{{description}}} {{#isNotRequired}}[optional]{{/isNotRequired}} */{{/description}}{{newline}} +{{#description}}/* {{{description}}} {{#isNotRequired}}[optional]{{/isNotRequired}} */{{/description}} +@property(nonatomic) {{{ datatype }}}{{#isNotRequired}}{{/isNotRequired}} {{name}}; {{/vars}} -- (id) {{#vars}}{{name}}: ({{datatype}}) {{name}}{{#hasMore}}{{newline}} {{/hasMore}}{{^hasMore}};{{/hasMore}} - {{/vars}} -{{newline}} -- (id) initWithValues: (NSDictionary*)dict; -- (NSDictionary*) asDictionary; -{{newline}} + @end {{/model}} -{{/models}} \ No newline at end of file +{{/models}} diff --git a/modules/swagger-codegen/src/test/scala/Objc/ObjcModelTest.scala b/modules/swagger-codegen/src/test/scala/Objc/ObjcModelTest.scala index e0de2110ddc..069b4a8e359 100644 --- a/modules/swagger-codegen/src/test/scala/Objc/ObjcModelTest.scala +++ b/modules/swagger-codegen/src/test/scala/Objc/ObjcModelTest.scala @@ -56,17 +56,14 @@ class ObjcModelTest extends FlatSpec with Matchers { vars.get(1).isNotContainer should equal (true) vars.get(2).baseName should be ("createdAt") - vars.get(2).complexType should be ("SWGDate") - vars.get(2).datatype should be ("SWGDate*") + vars.get(2).datatype should be ("NSDate*") vars.get(2).name should be ("createdAt") vars.get(2).defaultValue should be (null) - vars.get(2).baseType should be ("SWGDate") + vars.get(2).baseType should be ("NSDate") vars.get(2).hasMore should equal (null) vars.get(2).required should equal (false) vars.get(2).isNotContainer should equal (true) - (cm.imports.asScala.toSet & - Set("SWGDate")).size should be (1) } it should "convert a model with list property" in { @@ -173,7 +170,7 @@ class ObjcModelTest extends FlatSpec with Matchers { val vars = cm.vars vars.get(0).baseName should be ("children") vars.get(0).complexType should be ("SWGChildren") - vars.get(0).datatype should be ("NSArray*") + vars.get(0).datatype should be ("NSArray*") vars.get(0).name should be ("children") vars.get(0).baseType should be ("NSArray") vars.get(0).containerType should be ("array") @@ -259,4 +256,4 @@ class ObjcModelTest extends FlatSpec with Matchers { insectCo.imports.size should be (1) insectCo.imports.contains("SWGInsect") should equal (true) } -} \ No newline at end of file +}