Skip to content

Commit 5a5bb6e

Browse files
committed
Merge branch 'geekerzp-develop_2.0_objc_form_params' into develop_2.0
2 parents dfa2c22 + 4484625 commit 5a5bb6e

File tree

7 files changed

+376
-149
lines changed

7 files changed

+376
-149
lines changed

modules/swagger-codegen/src/main/resources/objc/SWGApiClient.m

Lines changed: 104 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ +(void)configureCacheWithMemoryAndDiskCapacity: (unsigned long) memorySize
3131
diskSize: (unsigned long) diskSize {
3232
NSAssert(memorySize > 0, @"invalid in-memory cache size");
3333
NSAssert(diskSize >= 0, @"invalid disk cache size");
34-
34+
3535
NSURLCache *cache =
3636
[[NSURLCache alloc]
3737
initWithMemoryCapacity:memorySize
3838
diskCapacity:diskSize
3939
diskPath:@"swagger_url_cache"];
40-
40+
4141
[NSURLCache setSharedURLCache:cache];
4242
}
4343

@@ -54,17 +54,17 @@ +(SWGApiClient *)sharedClientFromPool:(NSString *)baseUrl {
5454
// setup static vars
5555
// create queue
5656
sharedQueue = [[NSOperationQueue alloc] init];
57-
57+
5858
// create pool
5959
_pool = [[NSMutableDictionary alloc] init];
60-
60+
6161
// initialize URL cache
6262
[SWGApiClient configureCacheWithMemoryAndDiskCapacity:4*1024*1024 diskSize:32*1024*1024];
63-
63+
6464
// configure reachability
6565
[SWGApiClient configureCacheReachibilityForHost:baseUrl];
6666
}
67-
67+
6868
@synchronized(self) {
6969
SWGApiClient * client = [_pool objectForKey:baseUrl];
7070
if (client == nil) {
@@ -128,7 +128,7 @@ -(Boolean) executeRequestWithId:(NSNumber*) requestId {
128128
return TRUE;
129129
else return FALSE;
130130
}];
131-
131+
132132
if(matchingItems.count == 1) {
133133
if(loggingEnabled)
134134
NSLog(@"removing request id %@", requestId);
@@ -169,19 +169,19 @@ +(void) configureCacheReachibilityForHost:(NSString*)host {
169169
NSLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
170170
[SWGApiClient setOfflineState:true];
171171
break;
172-
172+
173173
case AFNetworkReachabilityStatusNotReachable:
174174
if(loggingEnabled)
175175
NSLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
176176
[SWGApiClient setOfflineState:true];
177177
break;
178-
178+
179179
case AFNetworkReachabilityStatusReachableViaWWAN:
180180
if(loggingEnabled)
181181
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
182182
[SWGApiClient setOfflineState:false];
183183
break;
184-
184+
185185
case AFNetworkReachabilityStatusReachableViaWiFi:
186186
if(loggingEnabled)
187187
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
@@ -202,7 +202,7 @@ -(NSString*) pathWithQueryParamsToString:(NSString*) path
202202
queryParams:(NSDictionary*) queryParams {
203203
NSString * separator = nil;
204204
int counter = 0;
205-
205+
206206
NSMutableString * requestUrl = [NSMutableString stringWithFormat:@"%@", path];
207207
if(queryParams != nil){
208208
for(NSString * key in [queryParams keyEnumerator]){
@@ -218,7 +218,7 @@ -(NSString*) pathWithQueryParamsToString:(NSString*) path
218218
SWGQueryParamCollection * coll = (SWGQueryParamCollection*) queryParam;
219219
NSArray* values = [coll values];
220220
NSString* format = [coll format];
221-
221+
222222
if([format isEqualToString:@"csv"]) {
223223
[requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator,
224224
[SWGApiClient escape:key], [NSString stringWithFormat:@"%@", [values componentsJoinedByString:@","]]]];
@@ -274,7 +274,28 @@ -(NSNumber*) dictionary: (NSString*) path
274274
requestContentType: (NSString*) requestContentType
275275
responseContentType: (NSString*) responseContentType
276276
completionBlock: (void (^)(NSDictionary*, NSError *))completionBlock {
277-
277+
// setting request serializer
278+
if ([requestContentType isEqualToString:@"application/json"]) {
279+
self.requestSerializer = [AFJSONRequestSerializer serializer];
280+
}
281+
else if ([requestContentType isEqualToString:@"application/x-www-form-urlencoded"]) {
282+
self.requestSerializer = [AFHTTPRequestSerializer serializer];
283+
}
284+
else if ([requestContentType isEqualToString:@"multipart/form-data"]) {
285+
self.requestSerializer = [AFHTTPRequestSerializer serializer];
286+
}
287+
else {
288+
NSAssert(false, @"unsupport request type %@", requestContentType);
289+
}
290+
291+
// setting response serializer
292+
if ([responseContentType isEqualToString:@"application/json"]) {
293+
self.responseSerializer = [AFJSONResponseSerializer serializer];
294+
}
295+
else {
296+
self.responseSerializer = [AFHTTPResponseSerializer serializer];
297+
}
298+
278299
NSMutableURLRequest * request = nil;
279300
if (body != nil && [body isKindOfClass:[NSArray class]]){
280301
SWGFile * file;
@@ -291,7 +312,8 @@ -(NSNumber*) dictionary: (NSString*) path
291312
}
292313
}
293314
NSString * urlString = [[NSURL URLWithString:path relativeToURL:self.baseURL] absoluteString];
294-
315+
316+
// request with multipart form
295317
if(file != nil) {
296318
request = [self.requestSerializer multipartFormRequestWithMethod: @"POST"
297319
URLString: urlString
@@ -302,20 +324,30 @@ -(NSNumber*) dictionary: (NSString*) path
302324
NSData* data = [params[key] dataUsingEncoding:NSUTF8StringEncoding];
303325
[formData appendPartWithFormData: data name: key];
304326
}
305-
327+
306328
[formData appendPartWithFileData: [file data]
307329
name: [file paramName]
308330
fileName: [file name]
309331
mimeType: [file mimeType]];
310-
332+
311333
}
312334
error:nil];
313335
}
336+
// request with form parameters
337+
else {
338+
NSString* pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams];
339+
NSString* urlString = [[NSURL URLWithString:pathWithQueryParams relativeToURL:self.baseURL] absoluteString];
340+
341+
request = [self.requestSerializer requestWithMethod:method
342+
URLString:urlString
343+
parameters:params
344+
error:nil];
345+
}
314346
}
315347
else {
316348
NSString * pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams];
317349
NSString * urlString = [[NSURL URLWithString:pathWithQueryParams relativeToURL:self.baseURL] absoluteString];
318-
350+
319351
request = [self.requestSerializer requestWithMethod:method
320352
URLString:urlString
321353
parameters:body
@@ -337,11 +369,9 @@ -(NSNumber*) dictionary: (NSString*) path
337369
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
338370
}
339371

340-
AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
341-
342372
if(body != nil) {
343373
if([body isKindOfClass:[NSDictionary class]] || [body isKindOfClass:[NSArray class]]){
344-
[requestSerializer setValue:requestContentType forHTTPHeaderField:@"Content-Type"];
374+
[self.requestSerializer setValue:requestContentType forHTTPHeaderField:@"Content-Type"];
345375
}
346376
else if ([body isKindOfClass:[SWGFile class]]) {}
347377
else {
@@ -353,16 +383,16 @@ -(NSNumber*) dictionary: (NSString*) path
353383
[request setValue:[headerParams valueForKey:key] forHTTPHeaderField:key];
354384
}
355385
}
356-
[requestSerializer setValue:responseContentType forHTTPHeaderField:@"Accept"];
357-
386+
[self.requestSerializer setValue:responseContentType forHTTPHeaderField:@"Accept"];
387+
358388
// Always disable cookies!
359389
[request setHTTPShouldHandleCookies:NO];
360-
361-
390+
391+
362392
if (self.logRequests) {
363393
[self logRequest:request];
364394
}
365-
395+
366396
NSNumber* requestId = [SWGApiClient queueRequest];
367397
AFHTTPRequestOperation *op =
368398
[self HTTPRequestOperationWithRequest:request
@@ -380,14 +410,14 @@ -(NSNumber*) dictionary: (NSString*) path
380410
userInfo[SWGResponseObjectErrorKey] = operation.responseObject;
381411
}
382412
NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo];
383-
413+
384414
if(self.logServerResponses)
385415
[self logResponse:nil forRequest:request error:augmentedError];
386416
completionBlock(nil, augmentedError);
387417
}
388418
}
389419
];
390-
420+
391421
[self.operationQueue addOperation:op];
392422
return requestId;
393423
}
@@ -400,6 +430,28 @@ -(NSNumber*) stringWithCompletionBlock: (NSString*) path
400430
requestContentType: (NSString*) requestContentType
401431
responseContentType: (NSString*) responseContentType
402432
completionBlock: (void (^)(NSString*, NSError *))completionBlock {
433+
// setting request serializer
434+
if ([requestContentType isEqualToString:@"application/json"]) {
435+
self.requestSerializer = [AFJSONRequestSerializer serializer];
436+
}
437+
else if ([requestContentType isEqualToString:@"application/x-www-form-urlencoded"]) {
438+
self.requestSerializer = [AFHTTPRequestSerializer serializer];
439+
}
440+
else if ([requestContentType isEqualToString:@"multipart/form-data"]) {
441+
self.requestSerializer = [AFHTTPRequestSerializer serializer];
442+
}
443+
else {
444+
NSAssert(false, @"unsupport request type %@", requestContentType);
445+
}
446+
447+
// setting response serializer
448+
if ([responseContentType isEqualToString:@"application/json"]) {
449+
self.responseSerializer = [AFJSONResponseSerializer serializer];
450+
}
451+
else {
452+
self.responseSerializer = [AFHTTPResponseSerializer serializer];
453+
}
454+
403455
NSMutableURLRequest * request = nil;
404456
if (body != nil && [body isKindOfClass:[NSArray class]]){
405457
SWGFile * file;
@@ -416,31 +468,43 @@ -(NSNumber*) stringWithCompletionBlock: (NSString*) path
416468
}
417469
}
418470
NSString * urlString = [[NSURL URLWithString:path relativeToURL:self.baseURL] absoluteString];
419-
471+
472+
// request with multipart form
420473
if(file != nil) {
421474
request = [self.requestSerializer multipartFormRequestWithMethod: @"POST"
422475
URLString: urlString
423476
parameters: nil
424477
constructingBodyWithBlock: ^(id<AFMultipartFormData> formData) {
425-
478+
426479
for(NSString * key in params) {
427480
NSData* data = [params[key] dataUsingEncoding:NSUTF8StringEncoding];
428481
[formData appendPartWithFormData: data name: key];
429482
}
430-
483+
431484
[formData appendPartWithFileData: [file data]
432485
name: [file paramName]
433486
fileName: [file name]
434487
mimeType: [file mimeType]];
435-
488+
436489
}
437490
error:nil];
438491
}
492+
// request with form parameters
493+
else {
494+
NSString* pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams];
495+
NSString* urlString = [[NSURL URLWithString:pathWithQueryParams relativeToURL:self.baseURL] absoluteString];
496+
497+
request = [self.requestSerializer requestWithMethod:method
498+
URLString:urlString
499+
parameters:params
500+
error:nil];
501+
}
502+
439503
}
440504
else {
441505
NSString * pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams];
442506
NSString * urlString = [[NSURL URLWithString:pathWithQueryParams relativeToURL:self.baseURL] absoluteString];
443-
507+
444508
request = [self.requestSerializer requestWithMethod: method
445509
URLString: urlString
446510
parameters: body
@@ -461,13 +525,11 @@ -(NSNumber*) stringWithCompletionBlock: (NSString*) path
461525
NSLog(@"%@ cache disabled", path);
462526
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
463527
}
464-
465-
466-
AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
528+
467529

468530
if(body != nil) {
469531
if([body isKindOfClass:[NSDictionary class]] || [body isKindOfClass:[NSArray class]]){
470-
[requestSerializer setValue:requestContentType forHTTPHeaderField:@"Content-Type"];
532+
[self.requestSerializer setValue:requestContentType forHTTPHeaderField:@"Content-Type"];
471533
}
472534
else if ([body isKindOfClass:[SWGFile class]]){}
473535
else {
@@ -479,12 +541,12 @@ -(NSNumber*) stringWithCompletionBlock: (NSString*) path
479541
[request setValue:[headerParams valueForKey:key] forHTTPHeaderField:key];
480542
}
481543
}
482-
[requestSerializer setValue:responseContentType forHTTPHeaderField:@"Accept"];
544+
[self.requestSerializer setValue:responseContentType forHTTPHeaderField:@"Accept"];
545+
483546

484-
485547
// Always disable cookies!
486548
[request setHTTPShouldHandleCookies:NO];
487-
549+
488550
NSNumber* requestId = [SWGApiClient queueRequest];
489551
AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request
490552
success:^(AFHTTPRequestOperation *operation, id responseObject) {
@@ -502,15 +564,15 @@ -(NSNumber*) stringWithCompletionBlock: (NSString*) path
502564
userInfo[SWGResponseObjectErrorKey] = operation.responseObject;
503565
}
504566
NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo];
505-
567+
506568
if(self.logServerResponses)
507569
[self logResponse:nil forRequest:request error:augmentedError];
508570
completionBlock(nil, augmentedError);
509571
}
510572
}];
511-
573+
512574
[self.operationQueue addOperation:op];
513575
return requestId;
514576
}
515577

516-
@end
578+
@end

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ static NSString * basePath = @"{{basePath}}";
6868
{{#pathParams}}[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"{{baseName}}", @"}"]] withString: [SWGApiClient escape:{{paramName}}]];
6969
{{/pathParams}}
7070

71-
NSString* requestContentType = @"application/json";
72-
NSString* responseContentType = @"application/json";
71+
NSArray* requestContentTypes = @[{{#consumes}}@"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}}];
72+
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
73+
74+
NSArray* responseContentTypes = @[{{#produces}}@"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}}];
75+
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
7376

7477
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
7578
{{#queryParams}}if({{paramName}} != nil) {
@@ -116,7 +119,7 @@ static NSString * basePath = @"{{basePath}}";
116119
{{/bodyParam}}
117120
{{^bodyParam}}
118121

119-
NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init];
122+
NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init];
120123

121124
{{#formParams}}
122125
{{#notFile}}
@@ -126,8 +129,10 @@ static NSString * basePath = @"{{basePath}}";
126129
if(bodyDictionary == nil) {
127130
bodyDictionary = [[NSMutableArray alloc] init];
128131
}
129-
[bodyDictionary addObject:{{paramName}}];
130-
{{paramName}}.paramName = @"{{baseName}}";
132+
if({{paramName}} != nil) {
133+
[bodyDictionary addObject:{{paramName}}];
134+
{{paramName}}.paramName = @"{{baseName}}";
135+
}
131136
{{/isFile}}
132137
if(bodyDictionary == nil) {
133138
bodyDictionary = [[NSMutableArray alloc] init];
@@ -172,4 +177,4 @@ static NSString * basePath = @"{{basePath}}";
172177

173178
{{newline}}
174179
{{/operations}}
175-
@end
180+
@end

0 commit comments

Comments
 (0)