Skip to content

Commit 517c0a4

Browse files
committed
Merge pull request #558 from geekerzp/develop_2.0_objc_jsonmodel
Add JSONModel to objective-c client
2 parents cab50cc + 37118ca commit 517c0a4

File tree

12 files changed

+86
-138
lines changed

12 files changed

+86
-138
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

@@ -153,11 +154,45 @@ public String getSwaggerType(Property p) {
153154

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

163198
@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/api-body.mustache

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static NSString * basePath = @"{{basePath}}";
5656
{{#operation}}
5757
-(NSNumber*) {{nickname}}WithCompletionBlock{{^allParams}}: {{/allParams}}{{#allParams}}{{#secondaryParam}} {{paramName}}{{/secondaryParam}}: ({{{dataType}}}) {{paramName}}
5858
{{/allParams}}
59-
{{#returnBaseType}}{{#hasParams}}completionHandler: {{/hasParams}}(void (^)({{returnType}} output, NSError* error))completionBlock{{/returnBaseType}}
59+
{{#returnBaseType}}{{#hasParams}}completionHandler: {{/hasParams}}(void (^)({{{returnType}}} output, NSError* error))completionBlock{{/returnBaseType}}
6060
{{^returnBaseType}}{{#hasParams}}completionHandler: {{/hasParams}}(void (^)(NSError* error))completionBlock{{/returnBaseType}} {
6161
6262
NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@{{path}}", basePath];
@@ -91,17 +91,17 @@ static NSString * basePath = @"{{basePath}}";
9191
if(__body != nil && [__body isKindOfClass:[NSArray class]]){
9292
NSMutableArray * objs = [[NSMutableArray alloc] init];
9393
for (id dict in (NSArray*)__body) {
94-
if([dict respondsToSelector:@selector(asDictionary)]) {
95-
[objs addObject:[(SWGObject*)dict asDictionary]];
94+
if([dict respondsToSelector:@selector(toDictionary)]) {
95+
[objs addObject:[(SWGObject*)dict toDictionary]];
9696
}
9797
else{
9898
[objs addObject:dict];
9999
}
100100
}
101101
bodyDictionary = objs;
102102
}
103-
else if([__body respondsToSelector:@selector(asDictionary)]) {
104-
bodyDictionary = [(SWGObject*)__body asDictionary];
103+
else if([__body respondsToSelector:@selector(toDictionary)]) {
104+
bodyDictionary = [(SWGObject*)__body toDictionary];
105105
}
106106
else if([__body isKindOfClass:[NSString class]]) {
107107
// convert it to a dictionary

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
{{#allParams}}@param {{paramName}} {{description}}
2222
{{/allParams}}
2323

24-
return type: {{returnType}}
24+
return type: {{{returnType}}}
2525
*/
2626
-(NSNumber*) {{nickname}}WithCompletionBlock {{^allParams}}:{{/allParams}}{{#allParams}}{{#secondaryParam}} {{paramName}}{{/secondaryParam}}:({{{dataType}}}) {{paramName}} {{#hasMore}}
2727
{{/hasMore}}{{/allParams}}
2828
{{#returnBaseType}}{{#hasParams}}
29-
completionHandler: {{/hasParams}}(void (^)({{returnType}} output, NSError* error))completionBlock;{{/returnBaseType}}
29+
completionHandler: {{/hasParams}}(void (^)({{{returnType}}} output, NSError* error))completionBlock;{{/returnBaseType}}
3030
{{^returnBaseType}}{{#hasParams}}
3131
completionHandler: {{/hasParams}}(void (^)(NSError* error))completionBlock;{{/returnBaseType}}
3232

modules/swagger-codegen/src/main/resources/objc/apiBodyResponseWithContainer.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
{{returnBaseType}}* d = [[{{{returnBaseType}}} alloc]initWithString: dict];
2626
{{/returnTypeIsPrimitive}}
2727
{{^returnTypeIsPrimitive}}
28-
{{{returnBaseType}}}* d = [[{{{returnBaseType}}} alloc]initWithValues: dict];
28+
{{{returnBaseType}}}* d = [[{{{returnBaseType}}} alloc] initWithDictionary:dict error:nil];
2929
{{/returnTypeIsPrimitive}}
3030
[objs addObject:d];
3131
}
32-
completionBlock(objs, nil);
32+
completionBlock(({{{returnType}}})objs, nil);
3333
}
3434
{{/returnBaseType}}
3535
{{/isListContainer}}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{{^returnTypeIsPrimitive}}
22
// comples response type
3-
return [client dictionary: requestUrl
4-
method: @"{{httpMethod}}"
5-
queryParams: queryParams
6-
body: bodyDictionary
3+
return [client dictionary: requestUrl
4+
method: @"{{httpMethod}}"
5+
queryParams: queryParams
6+
body: bodyDictionary
77
headerParams: headerParams
88
requestContentType: requestContentType
99
responseContentType: responseContentType
@@ -15,9 +15,9 @@
1515
}
1616
{{#returnType}}{{returnType}} result = nil;
1717
if (data) {
18-
result = [[{{#instantiationType}}NSClassFromString(@"{{{instantiationType}}}") {{/instantiationType}}{{^instantiationType}}{{{returnBaseType}}} {{/instantiationType}} alloc] {{#returnContainer}}{{#isMapContainer}}initWithDictionary{{/isMapContainer}}{{#isListContainer}}initWithValues{{/isListContainer}}{{/returnContainer}}{{^returnContainer}}initWithValues {{/returnContainer}}: data];
18+
result = [[{{#instantiationType}}NSClassFromString(@"{{{instantiationType}}}") {{/instantiationType}}{{^instantiationType}}{{{returnBaseType}}} {{/instantiationType}} alloc] {{#returnContainer}}{{#isMapContainer}}initWithDictionary{{/isMapContainer}}{{#isListContainer}} initWithDictionary{{/isListContainer}}{{/returnContainer}}{{^returnContainer}} initWithDictionary{{/returnContainer}}:data error:nil];
1919
}
2020
{{#returnType}}completionBlock(result , nil);{{/returnType}}
2121
{{/returnType}}
2222
}];
23-
{{/returnTypeIsPrimitive}}
23+
{{/returnTypeIsPrimitive}}

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}}

0 commit comments

Comments
 (0)