Skip to content

Commit f459982

Browse files
committed
Merge pull request #549 from swagger-api/feature/ios-templates
Feature/ios templates
2 parents db14a3f + 5610456 commit f459982

File tree

11 files changed

+349
-137
lines changed

11 files changed

+349
-137
lines changed

modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,8 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
745745
op.isMapContainer = Boolean.TRUE;
746746
else if ("list".equalsIgnoreCase(cm.containerType))
747747
op.isListContainer = Boolean.TRUE;
748+
else if ("array".equalsIgnoreCase(cm.containerType))
749+
op.isListContainer = Boolean.TRUE;
748750
}
749751
else
750752
op.returnSimpleType = true;

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

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public ObjcClientCodegen() {
4848
"NSObject",
4949
"NSArray",
5050
"NSNumber",
51-
"NSDate",
5251
"NSDictionary",
5352
"NSMutableArray",
5453
"NSMutableDictionary")
@@ -57,7 +56,6 @@ public ObjcClientCodegen() {
5756
Arrays.asList(
5857
"NSNumber",
5958
"NSString",
60-
"NSDate",
6159
"NSObject",
6260
"bool")
6361
);
@@ -72,8 +70,9 @@ public ObjcClientCodegen() {
7270

7371
typeMapping = new HashMap<String, String>();
7472
typeMapping.put("enum", "NSString");
75-
typeMapping.put("Date", "NSDate");
76-
typeMapping.put("DateTime", "NSDate");
73+
typeMapping.put("Date", "SWGDate");
74+
typeMapping.put("DateTime", "SWGDate");
75+
// typeMapping.put("Date", "SWGDate");
7776
typeMapping.put("boolean", "NSNumber");
7877
typeMapping.put("string", "NSString");
7978
typeMapping.put("integer", "NSNumber");
@@ -88,13 +87,13 @@ public ObjcClientCodegen() {
8887
typeMapping.put("object", "NSObject");
8988

9089
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",
9897
"NSDictionary")
9998
);
10099

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

153152
@Override
154153
public String getTypeDeclaration(Property p) {
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-
}
154+
String swaggerType = getSwaggerType(p);
155+
if(languageSpecificPrimitives.contains(swaggerType) && !foundationClasses.contains(swaggerType))
156+
return toModelName(swaggerType);
157+
else
158+
return swaggerType + "*";
194159
}
195160

196161
@Override

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,25 @@ static NSString * basePath = @"{{basePath}}";
110110
}
111111
{{/bodyParam}}
112112
{{^bodyParam}}
113-
bodyDictionary = [[NSMutableArray alloc] init];
114113

115114
NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init];
116115

117-
{{#formParams}}{{#notFile}}
116+
{{#formParams}}
117+
{{#notFile}}
118118
formParams[@"{{paramName}}"] = {{paramName}};
119119
{{/notFile}}{{#isFile}}
120120
requestContentType = @"multipart/form-data";
121+
if(bodyDictionary == nil) {
122+
bodyDictionary = [[NSMutableArray alloc] init];
123+
}
121124
[bodyDictionary addObject:{{paramName}}];
122125
{{paramName}}.paramName = @"{{baseName}}";
123-
{{/isFile}}{{/formParams}}
126+
{{/isFile}}
127+
if(bodyDictionary == nil) {
128+
bodyDictionary = [[NSMutableArray alloc] init];
129+
}
124130
[bodyDictionary addObject:formParams];
131+
{{/formParams}}
125132
{{/bodyParam}}
126133

127134
{{#requiredParamCount}}
@@ -134,11 +141,26 @@ static NSString * basePath = @"{{basePath}}";
134141

135142
SWGApiClient* client = [SWGApiClient sharedClientFromPool:basePath];
136143

137-
{{#returnContainer}}{{>apiBodyResponseWithContainer}}{{/returnContainer}}
144+
{{#returnContainer}}
145+
// response is in a container
146+
{{>apiBodyResponseWithContainer}}{{/returnContainer}}
147+
138148
{{#returnSimpleType}}
139-
{{#returnTypeIsPrimitive}}{{>apiPrimitiveResponse}}{{/returnTypeIsPrimitive}}
140-
{{#returnBaseType}}{{>apiNonPrimitiveResponse}}{{/returnBaseType}}
149+
// non container response
150+
151+
{{#returnTypeIsPrimitive}}
152+
// primitive response
153+
{{>apiPrimitiveResponse}}{{/returnTypeIsPrimitive}}
154+
155+
{{#returnBaseType}}
156+
// complex response
157+
{{>apiNonPrimitiveResponse}}{{/returnBaseType}}
141158
{{/returnSimpleType}}
159+
160+
{{^returnSimpleType}}{{^returnContainer}}
161+
// it's void
162+
{{>voidResponse}}
163+
{{/returnContainer}}{{/returnSimpleType}}
142164
}
143165

144166
{{/operation}}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
{{^returnBaseType}}completionBlock(error);{{/returnBaseType}}
1414
return;
1515
}
16-
{{#returnBaseType}}
17-
{{returnBaseType}} *result = nil;
16+
{{#returnType}}{{returnType}} result = nil;
1817
if (data) {
19-
result = [[{{#instantiationType}}NSClassFromString(@"{{{instantiationType}}}") {{/instantiationType}}{{^instantiationType}}{{{returnBaseType}}} {{/instantiationType}} alloc]initWithValues: data];
18+
result = [[{{#instantiationType}}NSClassFromString(@"{{{instantiationType}}}") {{/instantiationType}}{{^instantiationType}}{{{returnBaseType}}} {{/instantiationType}} alloc] {{#returnContainer}}{{#isMapContainer}}initWithDictionary{{/isMapContainer}}{{#isListContainer}}initWithValues{{/isListContainer}}{{/returnContainer}}{{^returnContainer}}initWithValues {{/returnContainer}}: data];
2019
}
21-
{{#returnBaseType}}completionBlock(result , nil);{{/returnBaseType}}
22-
{{/returnBaseType}}
20+
{{#returnType}}completionBlock(result , nil);{{/returnType}}
21+
{{/returnType}}
2322
}];
2423
{{/returnTypeIsPrimitive}}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
return [client stringWithCompletionBlock: requestUrl
2+
method: @"{{httpMethod}}"
3+
queryParams: queryParams
4+
body: bodyDictionary
5+
headerParams: headerParams
6+
requestContentType: requestContentType
7+
responseContentType: responseContentType
8+
completionBlock: ^(NSString *data, NSError *error) {
9+
if (error) {
10+
completionBlock(error);
11+
return;
12+
}
13+
completionBlock(nil);
14+
}];

samples/client/petstore/objc/PetstoreClient/PetstoreClient/ViewController.m

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ - (void)viewDidLoad
2222

2323
SWGPetApi * api = [[SWGPetApi alloc] init];
2424

25-
// [api getPetByIdWithCompletionBlock:@10 completionHandler:^(SWGPet *output, NSError *error) {
26-
// NSLog(@"%@", [output asDictionary]);
27-
// [output set_id:@101];
28-
// [api addPetWithCompletionBlock:output completionHandler:^(NSError *error) {
29-
// NSLog(@"Done!");
30-
// }];
31-
32-
// load data into file
33-
// }];
25+
[api getPetByIdWithCompletionBlock:@10 completionHandler:^(SWGPet *output, NSError *error) {
26+
NSLog(@"%@", [output asDictionary]);
27+
[output set_id:@101];
28+
[api addPetWithCompletionBlock:output completionHandler:^(NSError *error) {
29+
NSLog(@"Done!");
30+
}];
31+
32+
// load data into file
33+
}];
3434
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test-1" ofType:@"png"];
3535
NSData *myData = [NSData dataWithContentsOfFile:filePath];
3636

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
platform :ios, '6.0'
2-
xcodeproj 'swaggerClient/swaggerClient.xcodeproj'
2+
xcodeproj 'PetstoreClient/PetstoreClient.xcodeproj'
33
pod 'AFNetworking', '~> 2.1'

samples/client/petstore/objc/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ DEPENDENCIES:
2727
SPEC CHECKSUMS:
2828
AFNetworking: 8bee59492a6ff15d69130efa4d0dc67e0094a52a
2929

30-
COCOAPODS: 0.35.0
30+
COCOAPODS: 0.36.0

0 commit comments

Comments
 (0)