Skip to content

Commit fbe069c

Browse files
committed
add jsonmodel in objective-c client
1 parent 20add5b commit fbe069c

File tree

7 files changed

+61
-112
lines changed

7 files changed

+61
-112
lines changed

modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public ObjcClientCodegen() {
4848
"NSObject",
4949
"NSArray",
5050
"NSNumber",
51+
"NSDate",
5152
"NSDictionary",
5253
"NSMutableArray",
5354
"NSMutableDictionary")
@@ -57,6 +58,7 @@ public ObjcClientCodegen() {
5758
"NSNumber",
5859
"NSString",
5960
"NSObject",
61+
"NSDate",
6062
"bool")
6163
);
6264

@@ -70,9 +72,8 @@ public ObjcClientCodegen() {
7072

7173
typeMapping = new HashMap<String, String>();
7274
typeMapping.put("enum", "NSString");
73-
typeMapping.put("Date", "SWGDate");
74-
typeMapping.put("DateTime", "SWGDate");
75-
// typeMapping.put("Date", "SWGDate");
75+
typeMapping.put("Date", "NSDate");
76+
typeMapping.put("DateTime", "NSDate");
7677
typeMapping.put("boolean", "NSNumber");
7778
typeMapping.put("string", "NSString");
7879
typeMapping.put("integer", "NSNumber");
@@ -87,13 +88,13 @@ public ObjcClientCodegen() {
8788
typeMapping.put("object", "NSObject");
8889

8990
importMapping = new HashMap<String, String> ();
90-
importMapping.put("Date", "SWGDate");
9191

9292
foundationClasses = new HashSet<String> (
9393
Arrays.asList(
9494
"NSNumber",
9595
"NSObject",
9696
"NSString",
97+
"NSDate",
9798
"NSDictionary")
9899
);
99100

@@ -151,11 +152,45 @@ public String getSwaggerType(Property p) {
151152

152153
@Override
153154
public String getTypeDeclaration(Property p) {
154-
String swaggerType = getSwaggerType(p);
155-
if(languageSpecificPrimitives.contains(swaggerType) && !foundationClasses.contains(swaggerType))
156-
return toModelName(swaggerType);
157-
else
158-
return swaggerType + "*";
155+
if (p instanceof ArrayProperty) {
156+
ArrayProperty ap = (ArrayProperty) p;
157+
Property inner = ap.getItems();
158+
String innerType = getSwaggerType(inner);
159+
160+
// In this codition, type of property p is array of primitive,
161+
// return container type with pointer, e.g. `NSArray*'
162+
if (languageSpecificPrimitives.contains(innerType))
163+
return getSwaggerType(p) + "*";
164+
165+
// In this codition, type of property p is array of model,
166+
// return container type combine inner type with pointer, e.g. `NSArray<SWGTag>*'
167+
String innerTypeDeclaration = getTypeDeclaration(inner);
168+
169+
if (innerTypeDeclaration.endsWith("*"))
170+
innerTypeDeclaration = innerTypeDeclaration.substring(0, innerTypeDeclaration.length() - 1);
171+
172+
return getSwaggerType(p) + "<" + innerTypeDeclaration + ">*";
173+
}
174+
else {
175+
String swaggerType = getSwaggerType(p);
176+
177+
// In this codition, type of p is objective-c primitive type, e.g. `NSSNumber',
178+
// return type of p with pointer, e.g. `NSNumber*'
179+
if (languageSpecificPrimitives.contains(swaggerType) &&
180+
foundationClasses.contains(swaggerType)) {
181+
return swaggerType + "*";
182+
}
183+
// In this codition, type of p is c primitive type, e.g. `bool',
184+
// return type of p, e.g. `bool'
185+
else if (languageSpecificPrimitives.contains(swaggerType)) {
186+
return swaggerType;
187+
}
188+
// In this codition, type of p is objective-c object type, e.g. `SWGPet',
189+
// return type of p with pointer, e.g. `'
190+
else {
191+
return swaggerType + "*";
192+
}
193+
}
159194
}
160195

161196
@Override
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
platform :ios, '6.0'
22
xcodeproj '{{projectName}}/{{projectName}}.xcodeproj'
33
pod 'AFNetworking', '~> 2.1'
4+
pod 'JSONModel', '~> 1.0'
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#import <Foundation/Foundation.h>
2+
#import "JSONModel.h"
23

3-
@interface SWGObject : NSObject
4-
- (id) initWithValues:(NSDictionary*)dict;
5-
- (NSDictionary*) asDictionary;
4+
@interface SWGObject : JSONModel
65
@end
Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
11
#import "SWGObject.h"
22

33
@implementation SWGObject
4-
5-
- (id) initWithValues:(NSDictionary*)dict {
6-
return self;
7-
}
8-
9-
- (NSDictionary*) asDictionary{
10-
return [NSDictionary init];
11-
}
12-
13-
- (NSString*)description {
14-
return [NSString stringWithFormat:@"%@ %@", [super description], [self asDictionary]];
15-
}
16-
174
@end

modules/swagger-codegen/src/main/resources/objc/model-body.mustache

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,12 @@
11
{{#models}}
22
{{#model}}
3-
#import "SWGDate.h"
43
#import "{{classname}}.h"
54

65
@implementation {{classname}}
7-
8-
-(id){{#vars}}{{name}}: ({{datatype}}) {{name}}
9-
{{/vars}}
10-
{{newline}}{
11-
{{#vars}}_{{name}} = {{name}};
12-
{{/vars}}
13-
14-
return self;
15-
}
16-
17-
-(id) initWithValues:(NSDictionary*)dict
6+
7+
+ (JSONKeyMapper *)keyMapper
188
{
19-
self = [super init];
20-
if(self) {
21-
{{#vars}}{{#isPrimitiveType}}_{{name}} = dict[@"{{baseName}}"];{{/isPrimitiveType}}
22-
{{#complexType}}
23-
id {{name}}_dict = dict[@"{{baseName}}"];
24-
{{#isContainer}}
25-
if([{{name}}_dict isKindOfClass:[NSArray class]]) {
26-
NSMutableArray * objs = [[NSMutableArray alloc] initWithCapacity:[(NSArray*){{name}}_dict count]];
27-
if([(NSArray*){{name}}_dict count] > 0) {
28-
for (NSDictionary* dict in (NSArray*){{name}}_dict) {
29-
{{{complexType}}}* d = [[{{#instantiationType}}NSClassFromString(@"{{{instantiationType}}}") {{/instantiationType}}{{^instantiationType}}{{{complexType}}}{{/instantiationType}} alloc] initWithValues:dict];
30-
[objs addObject:d];
31-
}
32-
_{{{name}}} = [[NSArray alloc] initWithArray:objs];
33-
}
34-
else
35-
_{{name}} = [[NSArray alloc] init];
36-
}
37-
else {
38-
_{{name}} = [[NSArray alloc] init];
39-
}
40-
{{/isContainer}}{{#isNotContainer}}
41-
if({{name}}_dict != nil)
42-
_{{name}} = [[{{#instantiationType}}NSClassFromString(@"{{{instantiationType}}}") {{/instantiationType}}{{^instantiationType}}{{{complexType}}} {{/instantiationType}} alloc]{{setter}}:{{name}}_dict];
43-
{{/isNotContainer}}
44-
{{/complexType}}
45-
{{/vars}}{{newline}}
46-
}
47-
return self;
48-
}
49-
50-
-(NSDictionary*) asDictionary {
51-
NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
52-
{{#vars}}
53-
{{#complexType}}
54-
if(_{{name}} != nil){
55-
if([_{{name}} isKindOfClass:[NSArray class]]){
56-
NSMutableArray * array = [[NSMutableArray alloc] init];
57-
for( {{complexType}} *{{name}} in (NSArray*)_{{name}}) {
58-
[array addObject:[(SWGObject*){{name}} asDictionary]];
59-
}
60-
dict[@"{{name}}"] = array;
61-
}
62-
else if(_{{name}} && [_{{name}} isKindOfClass:[SWGDate class]]) {
63-
NSString * dateString = [(SWGDate*)_{{name}} toString];
64-
if(dateString){
65-
dict[@"{{name}}"] = dateString;
66-
}
67-
}
68-
else {
69-
{{/complexType}}
70-
if(_{{name}} != nil) dict[@"{{baseName}}"] = {{#complexType}}[(SWGObject*){{/complexType}}_{{name}} {{#complexType}}asDictionary]{{/complexType}};
71-
{{#complexType}}
72-
}
73-
}
74-
{{/complexType}}
75-
{{/vars}}
76-
77-
NSDictionary* output = [dict copy];
78-
return output;
9+
return [[JSONKeyMapper alloc] initWithDictionary:@{ {{#vars}}@"{{baseName}}": @"{{name}}"{{#hasMore}}, {{/hasMore}}{{/vars}} }];
7910
}
8011

8112
{{/model}}

modules/swagger-codegen/src/main/resources/objc/model-header.mustache

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@
66
{{#models}}
77
{{#model}}
88

9+
@protocol {{classname}}
10+
@end
11+
912
@interface {{classname}} : SWGObject
1013

1114
{{#vars}}
12-
@property(nonatomic) {{datatype}} {{name}}; {{#description}}/* {{{description}}} {{#isNotRequired}}[optional]{{/isNotRequired}} */{{/description}}{{newline}}
15+
{{#description}}/* {{{description}}} {{#isNotRequired}}[optional]{{/isNotRequired}} */{{/description}}
16+
@property(nonatomic) {{{ datatype }}}{{#isNotRequired}}<Optional>{{/isNotRequired}} {{name}};
1317
{{/vars}}
14-
- (id) {{#vars}}{{name}}: ({{datatype}}) {{name}}{{#hasMore}}{{newline}} {{/hasMore}}{{^hasMore}};{{/hasMore}}
15-
{{/vars}}
16-
{{newline}}
17-
- (id) initWithValues: (NSDictionary*)dict;
18-
- (NSDictionary*) asDictionary;
19-
{{newline}}
18+
2019
@end
2120
{{/model}}
22-
{{/models}}
21+
{{/models}}

modules/swagger-codegen/src/test/scala/Objc/ObjcModelTest.scala

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,14 @@ class ObjcModelTest extends FlatSpec with Matchers {
5656
vars.get(1).isNotContainer should equal (true)
5757

5858
vars.get(2).baseName should be ("createdAt")
59-
vars.get(2).complexType should be ("SWGDate")
60-
vars.get(2).datatype should be ("SWGDate*")
59+
vars.get(2).datatype should be ("NSDate*")
6160
vars.get(2).name should be ("createdAt")
6261
vars.get(2).defaultValue should be (null)
63-
vars.get(2).baseType should be ("SWGDate")
62+
vars.get(2).baseType should be ("NSDate")
6463
vars.get(2).hasMore should equal (null)
6564
vars.get(2).required should equal (false)
6665
vars.get(2).isNotContainer should equal (true)
6766

68-
(cm.imports.asScala.toSet &
69-
Set("SWGDate")).size should be (1)
7067
}
7168

7269
it should "convert a model with list property" in {
@@ -173,7 +170,7 @@ class ObjcModelTest extends FlatSpec with Matchers {
173170
val vars = cm.vars
174171
vars.get(0).baseName should be ("children")
175172
vars.get(0).complexType should be ("SWGChildren")
176-
vars.get(0).datatype should be ("NSArray*")
173+
vars.get(0).datatype should be ("NSArray<SWGChildren>*")
177174
vars.get(0).name should be ("children")
178175
vars.get(0).baseType should be ("NSArray")
179176
vars.get(0).containerType should be ("array")
@@ -259,4 +256,4 @@ class ObjcModelTest extends FlatSpec with Matchers {
259256
insectCo.imports.size should be (1)
260257
insectCo.imports.contains("SWGInsect") should equal (true)
261258
}
262-
}
259+
}

0 commit comments

Comments
 (0)