From b7cd38d38b28f6be093596b199d5e0af8b15055e Mon Sep 17 00:00:00 2001 From: geekerzp Date: Fri, 21 Aug 2015 09:42:31 +0800 Subject: [PATCH 1/3] Update objc client. Sanitize request parameters (path, query, header, form, body). --- .../resources/objc/ApiClient-body.mustache | 63 +++++++++- .../resources/objc/ApiClient-header.mustache | 9 ++ .../src/main/resources/objc/api-body.mustache | 24 +--- .../objc/SwaggerClient/SWGApiClient.h | 9 ++ .../objc/SwaggerClient/SWGApiClient.m | 63 +++++++++- .../petstore/objc/SwaggerClient/SWGPet.h | 2 +- .../petstore/objc/SwaggerClient/SWGPetApi.m | 82 ++++++------- .../petstore/objc/SwaggerClient/SWGStoreApi.m | 40 +++---- .../petstore/objc/SwaggerClient/SWGUserApi.m | 108 ++++++------------ 9 files changed, 231 insertions(+), 169 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache b/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache index a7d290022f6d..b7643de6ddd6 100644 --- a/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache @@ -467,6 +467,7 @@ static void (^reachabilityChangeBlock)(int); -(NSNumber*) requestWithCompletionBlock: (NSString*) path method: (NSString*) method + pathParams: (NSDictionary *) pathParams queryParams: (NSDictionary*) queryParams formParams: (NSDictionary *) formParams files: (NSDictionary *) files @@ -499,12 +500,25 @@ static void (^reachabilityChangeBlock)(int); self.responseSerializer = [AFHTTPResponseSerializer serializer]; } + // sanitize parameters + pathParams = [self sanitizeForSerialization:pathParams]; + queryParams = [self sanitizeForSerialization:queryParams]; + headerParams = [self sanitizeForSerialization:headerParams]; + formParams = [self sanitizeForSerialization:formParams]; + body = [self sanitizeForSerialization:body]; + // auth setting [self updateHeaderParams:&headerParams queryParams:&queryParams WithAuthSettings:authSettings]; + NSMutableString *resourcePath = [NSMutableString stringWithString:path]; + [pathParams enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { + [resourcePath replaceCharactersInRange:[resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", key, @"}"]] + withString:[SWGApiClient escape:obj]]; + }]; + NSMutableURLRequest * request = nil; - NSString* pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams]; + NSString* pathWithQueryParams = [self pathWithQueryParamsToString:resourcePath queryParams:queryParams]; if ([pathWithQueryParams hasPrefix:@"/"]) { pathWithQueryParams = [pathWithQueryParams substringFromIndex:1]; } @@ -540,20 +554,21 @@ static void (^reachabilityChangeBlock)(int); } } + // request cache BOOL hasHeaderParams = false; if(headerParams != nil && [headerParams count] > 0) { hasHeaderParams = true; } if(offlineState) { - NSLog(@"%@ cache forced", path); + NSLog(@"%@ cache forced", resourcePath); [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; } else if(!hasHeaderParams && [method isEqualToString:@"GET"] && cacheEnabled) { - NSLog(@"%@ cache enabled", path); + NSLog(@"%@ cache enabled", resourcePath); [request setCachePolicy:NSURLRequestUseProtocolCachePolicy]; } else { - NSLog(@"%@ cache disabled", path); + NSLog(@"%@ cache disabled", resourcePath); [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; } @@ -671,4 +686,44 @@ static void (^reachabilityChangeBlock)(int); *querys = [NSDictionary dictionaryWithDictionary:querysWithAuth]; } +- (id) sanitizeForSerialization:(id) object { + if (object == nil) { + return nil; + } + else if ([object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSNumber class]] || [object isKindOfClass:[SWGQueryParamCollection class]]) { + return object; + } + else if ([object isKindOfClass:[NSDate class]]) { + return [object ISO8601String]; + } + else if ([object isKindOfClass:[NSArray class]]) { + NSMutableArray *sanitizedObjs = [NSMutableArray arrayWithCapacity:[object count]]; + [object enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { + if (obj) { + [sanitizedObjs addObject:[self sanitizeForSerialization:obj]]; + } + }]; + return sanitizedObjs; + } + else if ([object isKindOfClass:[NSDictionary class]]) { + NSMutableDictionary *sanitizedObjs = [NSMutableDictionary dictionaryWithCapacity:[object count]]; + [object enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { + if (obj) { + [sanitizedObjs setValue:[self sanitizeForSerialization:obj] forKey:key]; + } + }]; + return sanitizedObjs; + } + else if ([object isKindOfClass:[SWGObject class]]) { + return [object toDictionary]; + } + else { + NSException *e = [NSException + exceptionWithName:@"InvalidObjectArgumentException" + reason:[NSString stringWithFormat:@"*** The argument object: %@ is invalid", object] + userInfo:nil]; + @throw e; + } +} + @end diff --git a/modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache b/modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache index 46d424d735f0..eba627058795 100644 --- a/modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache @@ -168,6 +168,7 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey; * * @param path Request url. * @param method Request method. + * @param pathParams Request path parameters. * @param queryParams Request query parameters. * @param body Request body. * @param headerParams Request header parameters. @@ -180,6 +181,7 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey; */ -(NSNumber*) requestWithCompletionBlock:(NSString*) path method:(NSString*) method + pathParams:(NSDictionary *) pathParams queryParams:(NSDictionary*) queryParams formParams:(NSDictionary *) formParams files:(NSDictionary *) files @@ -191,4 +193,11 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey; responseType:(NSString *) responseType completionBlock:(void (^)(id, NSError *))completionBlock; +/** + * Sanitize object for request + * + * @param object The query/path/header/form/body param to be sanitized. + */ +- (id) sanitizeForSerialization:(id) object; + @end diff --git a/modules/swagger-codegen/src/main/resources/objc/api-body.mustache b/modules/swagger-codegen/src/main/resources/objc/api-body.mustache index 198c5a89fdf3..a47b39c22cd1 100644 --- a/modules/swagger-codegen/src/main/resources/objc/api-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/api-body.mustache @@ -88,8 +88,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - {{#pathParams}}[resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"{{baseName}}", @"}"]] withString: [{{classPrefix}}ApiClient escape:{{paramName}}]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + {{#pathParams}}if ({{paramName}} != nil) { + pathParams[@"{{baseName}}"] = {{paramName}}; + } {{/pathParams}} NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -132,22 +135,6 @@ NSMutableDictionary *files = [[NSMutableDictionary alloc] init]; {{#bodyParam}} bodyParam = {{paramName}}; - - if(bodyParam != nil && [bodyParam isKindOfClass:[NSArray class]]){ - NSMutableArray *objs = [[NSMutableArray alloc] init]; - for (id dict in (NSArray*)bodyParam) { - if([dict respondsToSelector:@selector(toDictionary)]) { - [objs addObject:[({{classPrefix}}Object*)dict toDictionary]]; - } - else{ - [objs addObject:dict]; - } - } - bodyParam = objs; - } - else if([bodyParam respondsToSelector:@selector(toDictionary)]) { - bodyParam = [({{classPrefix}}Object*)bodyParam toDictionary]; - } {{/bodyParam}}{{^bodyParam}} {{#formParams}} {{#notFile}} @@ -167,6 +154,7 @@ {{/requiredParamCount}} return [self.apiClient requestWithCompletionBlock: resourcePath method: @"{{httpMethod}}" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files diff --git a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h index 7802a5b46c0e..84f28faa8c6c 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h +++ b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h @@ -172,6 +172,7 @@ extern NSString *const SWGResponseObjectErrorKey; * * @param path Request url. * @param method Request method. + * @param pathParams Request path parameters. * @param queryParams Request query parameters. * @param body Request body. * @param headerParams Request header parameters. @@ -184,6 +185,7 @@ extern NSString *const SWGResponseObjectErrorKey; */ -(NSNumber*) requestWithCompletionBlock:(NSString*) path method:(NSString*) method + pathParams:(NSDictionary *) pathParams queryParams:(NSDictionary*) queryParams formParams:(NSDictionary *) formParams files:(NSDictionary *) files @@ -195,4 +197,11 @@ extern NSString *const SWGResponseObjectErrorKey; responseType:(NSString *) responseType completionBlock:(void (^)(id, NSError *))completionBlock; +/** + * Sanitize object for request + * + * @param object The query/path/header/form/body param to be sanitized. + */ +- (id) sanitizeForSerialization:(id) object; + @end diff --git a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m index 22b5c256402b..906169f4d129 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m @@ -467,6 +467,7 @@ static void (^reachabilityChangeBlock)(int); -(NSNumber*) requestWithCompletionBlock: (NSString*) path method: (NSString*) method + pathParams: (NSDictionary *) pathParams queryParams: (NSDictionary*) queryParams formParams: (NSDictionary *) formParams files: (NSDictionary *) files @@ -499,12 +500,25 @@ static void (^reachabilityChangeBlock)(int); self.responseSerializer = [AFHTTPResponseSerializer serializer]; } + // sanitize parameters + pathParams = [self sanitizeForSerialization:pathParams]; + queryParams = [self sanitizeForSerialization:queryParams]; + headerParams = [self sanitizeForSerialization:headerParams]; + formParams = [self sanitizeForSerialization:formParams]; + body = [self sanitizeForSerialization:body]; + // auth setting [self updateHeaderParams:&headerParams queryParams:&queryParams WithAuthSettings:authSettings]; + NSMutableString *resourcePath = [NSMutableString stringWithString:path]; + [pathParams enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { + [resourcePath replaceCharactersInRange:[resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", key, @"}"]] + withString:[SWGApiClient escape:obj]]; + }]; + NSMutableURLRequest * request = nil; - NSString* pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams]; + NSString* pathWithQueryParams = [self pathWithQueryParamsToString:resourcePath queryParams:queryParams]; if ([pathWithQueryParams hasPrefix:@"/"]) { pathWithQueryParams = [pathWithQueryParams substringFromIndex:1]; } @@ -540,20 +554,21 @@ static void (^reachabilityChangeBlock)(int); } } + // request cache BOOL hasHeaderParams = false; if(headerParams != nil && [headerParams count] > 0) { hasHeaderParams = true; } if(offlineState) { - NSLog(@"%@ cache forced", path); + NSLog(@"%@ cache forced", resourcePath); [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; } else if(!hasHeaderParams && [method isEqualToString:@"GET"] && cacheEnabled) { - NSLog(@"%@ cache enabled", path); + NSLog(@"%@ cache enabled", resourcePath); [request setCachePolicy:NSURLRequestUseProtocolCachePolicy]; } else { - NSLog(@"%@ cache disabled", path); + NSLog(@"%@ cache disabled", resourcePath); [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; } @@ -671,4 +686,44 @@ static void (^reachabilityChangeBlock)(int); *querys = [NSDictionary dictionaryWithDictionary:querysWithAuth]; } +- (id) sanitizeForSerialization:(id) object { + if (object == nil) { + return nil; + } + else if ([object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSNumber class]] || [object isKindOfClass:[SWGQueryParamCollection class]]) { + return object; + } + else if ([object isKindOfClass:[NSDate class]]) { + return [object ISO8601String]; + } + else if ([object isKindOfClass:[NSArray class]]) { + NSMutableArray *sanitizedObjs = [NSMutableArray arrayWithCapacity:[object count]]; + [object enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { + if (obj) { + [sanitizedObjs addObject:[self sanitizeForSerialization:obj]]; + } + }]; + return sanitizedObjs; + } + else if ([object isKindOfClass:[NSDictionary class]]) { + NSMutableDictionary *sanitizedObjs = [NSMutableDictionary dictionaryWithCapacity:[object count]]; + [object enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { + if (obj) { + [sanitizedObjs setValue:[self sanitizeForSerialization:obj] forKey:key]; + } + }]; + return sanitizedObjs; + } + else if ([object isKindOfClass:[SWGObject class]]) { + return [object toDictionary]; + } + else { + NSException *e = [NSException + exceptionWithName:@"InvalidObjectArgumentException" + reason:[NSString stringWithFormat:@"*** The argument object: %@ is invalid", object] + userInfo:nil]; + @throw e; + } +} + @end diff --git a/samples/client/petstore/objc/SwaggerClient/SWGPet.h b/samples/client/petstore/objc/SwaggerClient/SWGPet.h index e340e0e2b86a..84f10969e5be 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGPet.h +++ b/samples/client/petstore/objc/SwaggerClient/SWGPet.h @@ -7,8 +7,8 @@ * Do not edit the class manually. */ -#import "SWGTag.h" #import "SWGCategory.h" +#import "SWGTag.h" @protocol SWGPet diff --git a/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m b/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m index f2e7fb640307..4f30301c658d 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m @@ -80,7 +80,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -115,27 +116,12 @@ NSMutableDictionary *files = [[NSMutableDictionary alloc] init]; bodyParam = body; - - if(bodyParam != nil && [bodyParam isKindOfClass:[NSArray class]]){ - NSMutableArray *objs = [[NSMutableArray alloc] init]; - for (id dict in (NSArray*)bodyParam) { - if([dict respondsToSelector:@selector(toDictionary)]) { - [objs addObject:[(SWGObject*)dict toDictionary]]; - } - else{ - [objs addObject:dict]; - } - } - bodyParam = objs; - } - else if([bodyParam respondsToSelector:@selector(toDictionary)]) { - bodyParam = [(SWGObject*)bodyParam toDictionary]; - } return [self.apiClient requestWithCompletionBlock: resourcePath method: @"PUT" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -172,7 +158,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -207,27 +194,12 @@ NSMutableDictionary *files = [[NSMutableDictionary alloc] init]; bodyParam = body; - - if(bodyParam != nil && [bodyParam isKindOfClass:[NSArray class]]){ - NSMutableArray *objs = [[NSMutableArray alloc] init]; - for (id dict in (NSArray*)bodyParam) { - if([dict respondsToSelector:@selector(toDictionary)]) { - [objs addObject:[(SWGObject*)dict toDictionary]]; - } - else{ - [objs addObject:dict]; - } - } - bodyParam = objs; - } - else if([bodyParam respondsToSelector:@selector(toDictionary)]) { - bodyParam = [(SWGObject*)bodyParam toDictionary]; - } return [self.apiClient requestWithCompletionBlock: resourcePath method: @"POST" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -264,7 +236,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -310,6 +283,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"GET" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -346,7 +320,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -392,6 +367,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"GET" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -433,8 +409,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + if (petId != nil) { + pathParams[@"petId"] = petId; + } NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -462,7 +441,7 @@ NSString *requestContentType = [SWGApiClient selectHeaderContentType:@[]]; // Authentication setting - NSArray *authSettings = @[@"api_key", @"petstore_auth"]; + NSArray *authSettings = @[@"petstore_auth", @"api_key"]; id bodyParam = nil; NSMutableDictionary *formParams = [[NSMutableDictionary alloc] init]; @@ -474,6 +453,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"GET" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -521,8 +501,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + if (petId != nil) { + pathParams[@"petId"] = petId; + } NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -570,6 +553,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"POST" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -614,8 +598,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + if (petId != nil) { + pathParams[@"petId"] = petId; + } NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -657,6 +644,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"DELETE" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -704,8 +692,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + if (petId != nil) { + pathParams[@"petId"] = petId; + } NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -753,6 +744,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"POST" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files diff --git a/samples/client/petstore/objc/SwaggerClient/SWGStoreApi.m b/samples/client/petstore/objc/SwaggerClient/SWGStoreApi.m index ed792988597f..792147ebed14 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGStoreApi.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGStoreApi.m @@ -77,7 +77,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -117,6 +118,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"GET" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -153,7 +155,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -188,27 +191,12 @@ NSMutableDictionary *files = [[NSMutableDictionary alloc] init]; bodyParam = body; - - if(bodyParam != nil && [bodyParam isKindOfClass:[NSArray class]]){ - NSMutableArray *objs = [[NSMutableArray alloc] init]; - for (id dict in (NSArray*)bodyParam) { - if([dict respondsToSelector:@selector(toDictionary)]) { - [objs addObject:[(SWGObject*)dict toDictionary]]; - } - else{ - [objs addObject:dict]; - } - } - bodyParam = objs; - } - else if([bodyParam respondsToSelector:@selector(toDictionary)]) { - bodyParam = [(SWGObject*)bodyParam toDictionary]; - } return [self.apiClient requestWithCompletionBlock: resourcePath method: @"POST" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -250,8 +238,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"orderId", @"}"]] withString: [SWGApiClient escape:orderId]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + if (orderId != nil) { + pathParams[@"orderId"] = orderId; + } NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -291,6 +282,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"GET" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -332,8 +324,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"orderId", @"}"]] withString: [SWGApiClient escape:orderId]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + if (orderId != nil) { + pathParams[@"orderId"] = orderId; + } NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -373,6 +368,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"DELETE" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files diff --git a/samples/client/petstore/objc/SwaggerClient/SWGUserApi.m b/samples/client/petstore/objc/SwaggerClient/SWGUserApi.m index 46a76b35b577..92dd00fa28da 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGUserApi.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGUserApi.m @@ -80,7 +80,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -115,27 +116,12 @@ NSMutableDictionary *files = [[NSMutableDictionary alloc] init]; bodyParam = body; - - if(bodyParam != nil && [bodyParam isKindOfClass:[NSArray class]]){ - NSMutableArray *objs = [[NSMutableArray alloc] init]; - for (id dict in (NSArray*)bodyParam) { - if([dict respondsToSelector:@selector(toDictionary)]) { - [objs addObject:[(SWGObject*)dict toDictionary]]; - } - else{ - [objs addObject:dict]; - } - } - bodyParam = objs; - } - else if([bodyParam respondsToSelector:@selector(toDictionary)]) { - bodyParam = [(SWGObject*)bodyParam toDictionary]; - } return [self.apiClient requestWithCompletionBlock: resourcePath method: @"POST" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -172,7 +158,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -207,27 +194,12 @@ NSMutableDictionary *files = [[NSMutableDictionary alloc] init]; bodyParam = body; - - if(bodyParam != nil && [bodyParam isKindOfClass:[NSArray class]]){ - NSMutableArray *objs = [[NSMutableArray alloc] init]; - for (id dict in (NSArray*)bodyParam) { - if([dict respondsToSelector:@selector(toDictionary)]) { - [objs addObject:[(SWGObject*)dict toDictionary]]; - } - else{ - [objs addObject:dict]; - } - } - bodyParam = objs; - } - else if([bodyParam respondsToSelector:@selector(toDictionary)]) { - bodyParam = [(SWGObject*)bodyParam toDictionary]; - } return [self.apiClient requestWithCompletionBlock: resourcePath method: @"POST" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -264,7 +236,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -299,27 +272,12 @@ NSMutableDictionary *files = [[NSMutableDictionary alloc] init]; bodyParam = body; - - if(bodyParam != nil && [bodyParam isKindOfClass:[NSArray class]]){ - NSMutableArray *objs = [[NSMutableArray alloc] init]; - for (id dict in (NSArray*)bodyParam) { - if([dict respondsToSelector:@selector(toDictionary)]) { - [objs addObject:[(SWGObject*)dict toDictionary]]; - } - else{ - [objs addObject:dict]; - } - } - bodyParam = objs; - } - else if([bodyParam respondsToSelector:@selector(toDictionary)]) { - bodyParam = [(SWGObject*)bodyParam toDictionary]; - } return [self.apiClient requestWithCompletionBlock: resourcePath method: @"POST" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -359,7 +317,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -407,6 +366,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"GET" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -440,7 +400,8 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -480,6 +441,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"GET" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -521,8 +483,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"username", @"}"]] withString: [SWGApiClient escape:username]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + if (username != nil) { + pathParams[@"username"] = username; + } NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -562,6 +527,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"GET" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -606,8 +572,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"username", @"}"]] withString: [SWGApiClient escape:username]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + if (username != nil) { + pathParams[@"username"] = username; + } NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -642,27 +611,12 @@ NSMutableDictionary *files = [[NSMutableDictionary alloc] init]; bodyParam = body; - - if(bodyParam != nil && [bodyParam isKindOfClass:[NSArray class]]){ - NSMutableArray *objs = [[NSMutableArray alloc] init]; - for (id dict in (NSArray*)bodyParam) { - if([dict respondsToSelector:@selector(toDictionary)]) { - [objs addObject:[(SWGObject*)dict toDictionary]]; - } - else{ - [objs addObject:dict]; - } - } - bodyParam = objs; - } - else if([bodyParam respondsToSelector:@selector(toDictionary)]) { - bodyParam = [(SWGObject*)bodyParam toDictionary]; - } return [self.apiClient requestWithCompletionBlock: resourcePath method: @"PUT" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files @@ -704,8 +658,11 @@ if ([resourcePath rangeOfString:@".{format}"].location != NSNotFound) { [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:@".{format}"] withString:@".json"]; } - - [resourcePath replaceCharactersInRange: [resourcePath rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"username", @"}"]] withString: [SWGApiClient escape:username]]; + + NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; + if (username != nil) { + pathParams[@"username"] = username; + } NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -745,6 +702,7 @@ return [self.apiClient requestWithCompletionBlock: resourcePath method: @"DELETE" + pathParams: pathParams queryParams: queryParams formParams: formParams files: files From 6218ad139ffddf17eaf09ecea700f3d3c6806155 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Fri, 21 Aug 2015 11:20:29 +0800 Subject: [PATCH 2/3] Fix issue that it will throw error if not pass optional form param in objc client. --- .../src/main/resources/objc/api-body.mustache | 4 +++- .../client/petstore/objc/SwaggerClient/SWGPetApi.m | 12 +++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/objc/api-body.mustache b/modules/swagger-codegen/src/main/resources/objc/api-body.mustache index a47b39c22cd1..9c7d8388b231 100644 --- a/modules/swagger-codegen/src/main/resources/objc/api-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/api-body.mustache @@ -138,7 +138,9 @@ {{/bodyParam}}{{^bodyParam}} {{#formParams}} {{#notFile}} - formParams[@"{{paramName}}"] = {{paramName}}; + if ({{paramName}}) { + formParams[@"{{paramName}}"] = {{paramName}}; + } {{/notFile}}{{#isFile}} files[@"{{paramName}}"] = {{paramName}}; {{/isFile}} diff --git a/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m b/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m index 4f30301c658d..d79584f90bfe 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m @@ -541,11 +541,15 @@ - formParams[@"name"] = name; + if (name) { + formParams[@"name"] = name; + } - formParams[@"status"] = status; + if (status) { + formParams[@"status"] = status; + } @@ -732,7 +736,9 @@ - formParams[@"additionalMetadata"] = additionalMetadata; + if (additionalMetadata) { + formParams[@"additionalMetadata"] = additionalMetadata; + } From 4189d0765bbb6214f435b2c9211284ee5387dfd6 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Fri, 21 Aug 2015 11:40:59 +0800 Subject: [PATCH 3/3] Update api body template of objc client --- .../swagger-codegen/src/main/resources/objc/api-body.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/swagger-codegen/src/main/resources/objc/api-body.mustache b/modules/swagger-codegen/src/main/resources/objc/api-body.mustache index 9c7d8388b231..534c887fe7b1 100644 --- a/modules/swagger-codegen/src/main/resources/objc/api-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/api-body.mustache @@ -139,7 +139,7 @@ {{#formParams}} {{#notFile}} if ({{paramName}}) { - formParams[@"{{paramName}}"] = {{paramName}}; + formParams[@"{{baseName}}"] = {{paramName}}; } {{/notFile}}{{#isFile}} files[@"{{paramName}}"] = {{paramName}};