forked from loafle/openapi-generator-original
Merge pull request #1096 from geekerzp/objc-param-to-str
[Objc] Sanitize request parameters in objc client
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,26 +135,12 @@
|
||||
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}}
|
||||
formParams[@"{{paramName}}"] = {{paramName}};
|
||||
if ({{paramName}}) {
|
||||
formParams[@"{{baseName}}"] = {{paramName}};
|
||||
}
|
||||
{{/notFile}}{{#isFile}}
|
||||
files[@"{{paramName}}"] = {{paramName}};
|
||||
{{/isFile}}
|
||||
@@ -167,6 +156,7 @@
|
||||
{{/requiredParamCount}}
|
||||
return [self.apiClient requestWithCompletionBlock: resourcePath
|
||||
method: @"{{httpMethod}}"
|
||||
pathParams: pathParams
|
||||
queryParams: queryParams
|
||||
formParams: formParams
|
||||
files: files
|
||||
|
||||
Reference in New Issue
Block a user