Merge pull request #2799 from mateuszmackowiak/obj/ApiClient-Sanitizer

Sanitizer for separating sanitize and service logic
This commit is contained in:
wing328 2016-05-09 18:01:36 +08:00
commit 28c7ea5426
10 changed files with 257 additions and 188 deletions

View File

@ -240,6 +240,8 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
supportingFiles.add(new SupportingFile("JSONRequestSerializer-header.mustache", swaggerFolder, classPrefix + "JSONRequestSerializer.h"));
supportingFiles.add(new SupportingFile("ResponseDeserializer-body.mustache", swaggerFolder, classPrefix + "ResponseDeserializer.m"));
supportingFiles.add(new SupportingFile("ResponseDeserializer-header.mustache", swaggerFolder, classPrefix + "ResponseDeserializer.h"));
supportingFiles.add(new SupportingFile("Sanitizer-body.mustache", swaggerFolder, classPrefix + "Sanitizer.m"));
supportingFiles.add(new SupportingFile("Sanitizer-header.mustache", swaggerFolder, classPrefix + "Sanitizer.h"));
supportingFiles.add(new SupportingFile("JSONValueTransformer+ISO8601.m", swaggerFolder, "JSONValueTransformer+ISO8601.m"));
supportingFiles.add(new SupportingFile("JSONValueTransformer+ISO8601.h", swaggerFolder, "JSONValueTransformer+ISO8601.h"));
supportingFiles.add(new SupportingFile("Configuration-body.mustache", swaggerFolder, classPrefix + "Configuration.m"));

View File

@ -54,6 +54,7 @@ static NSString * {{classPrefix}}__fileNameForResponse(NSURLResponse *response)
self.responseSerializer = [AFJSONResponseSerializer serializer];
self.securityPolicy = [self customSecurityPolicy];
self.responseDeserializer = [[{{classPrefix}}ResponseDeserializer alloc] init];
self.sanitizer = [[{{classPrefix}}Sanitizer alloc] init];
// configure reachability
[self configureCacheReachibility];
}
@ -381,11 +382,11 @@ static NSString * {{classPrefix}}__fileNameForResponse(NSURLResponse *response)
}
// sanitize parameters
pathParams = [self sanitizeForSerialization:pathParams];
queryParams = [self sanitizeForSerialization:queryParams];
headerParams = [self sanitizeForSerialization:headerParams];
formParams = [self sanitizeForSerialization:formParams];
body = [self sanitizeForSerialization:body];
pathParams = [self.sanitizer sanitizeForSerialization:pathParams];
queryParams = [self.sanitizer sanitizeForSerialization:queryParams];
headerParams = [self.sanitizer sanitizeForSerialization:headerParams];
formParams = [self.sanitizer sanitizeForSerialization:formParams];
body = [self.sanitizer sanitizeForSerialization:body];
// auth setting
[self updateHeaderParams:&headerParams queryParams:&queryParams WithAuthSettings:authSettings];
@ -405,12 +406,13 @@ static NSString * {{classPrefix}}__fileNameForResponse(NSURLResponse *response)
NSString* urlString = [[NSURL URLWithString:pathWithQueryParams relativeToURL:self.baseURL] absoluteString];
if (files.count > 0) {
__weak __typeof(self)weakSelf = self;
request = [self.requestSerializer multipartFormRequestWithMethod:@"POST"
URLString:urlString
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formParams enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSString *objString = [self parameterToString:obj];
NSString *objString = [weakSelf.sanitizer parameterToString:obj];
NSData *data = [objString dataUsingEncoding:NSUTF8StringEncoding];
[formData appendPartWithFormData:data name:key];
}];
@ -572,48 +574,6 @@ static NSString * {{classPrefix}}__fileNameForResponse(NSURLResponse *response)
*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:[{{classPrefix}}QueryParamCollection class]]) {
return object;
}
else if ([object isKindOfClass:[NSDate class]]) {
return [object ISO8601String];
}
else if ([object isKindOfClass:[NSArray class]]) {
NSArray *objectArray = object;
NSMutableArray *sanitizedObjs = [NSMutableArray arrayWithCapacity:[objectArray count]];
[object enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (obj) {
[sanitizedObjs addObject:[self sanitizeForSerialization:obj]];
}
}];
return sanitizedObjs;
}
else if ([object isKindOfClass:[NSDictionary class]]) {
NSDictionary *objectDict = object;
NSMutableDictionary *sanitizedObjs = [NSMutableDictionary dictionaryWithCapacity:[objectDict count]];
[object enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if (obj) {
[sanitizedObjs setValue:[self sanitizeForSerialization:obj] forKey:key];
}
}];
return sanitizedObjs;
}
else if ([object isKindOfClass:[{{classPrefix}}Object class]]) {
return [object toDictionary];
}
else {
NSException *e = [NSException
exceptionWithName:@"InvalidObjectArgumentException"
reason:[NSString stringWithFormat:@"*** The argument object: %@ is invalid", object]
userInfo:nil];
@throw e;
}
}
- (AFSecurityPolicy *) customSecurityPolicy {
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
@ -635,30 +595,4 @@ static NSString * {{classPrefix}}__fileNameForResponse(NSURLResponse *response)
return securityPolicy;
}
- (NSString *) parameterToString:(id)param {
if ([param isKindOfClass:[NSString class]]) {
return param;
}
else if ([param isKindOfClass:[NSNumber class]]) {
return [param stringValue];
}
else if ([param isKindOfClass:[NSDate class]]) {
return [param ISO8601String];
}
else if ([param isKindOfClass:[NSArray class]]) {
NSMutableArray *mutableParam;
[param enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[mutableParam addObject:[self parameterToString:obj]];
}];
return [mutableParam componentsJoinedByString:@","];
}
else {
NSException *e = [NSException
exceptionWithName:@"InvalidObjectArgumentException"
reason:[NSString stringWithFormat:@"*** The argument object: %@ is invalid", param]
userInfo:nil];
@throw e;
}
}
@end

View File

@ -6,6 +6,7 @@
#import "{{classPrefix}}QueryParamCollection.h"
#import "{{classPrefix}}Configuration.h"
#import "{{classPrefix}}ResponseDeserializer.h"
#import "{{classPrefix}}Sanitizer.h"
/**
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen
@ -42,6 +43,8 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey;
@property(nonatomic, readonly) NSDictionary* HTTPResponseHeaders;
@property(nonatomic, strong) id<{{classPrefix}}ResponseDeserializer> responseDeserializer;
@property(nonatomic, strong) id<{{classPrefix}}Sanitizer> sanitizer;
/**
* Clears Cache
*/
@ -212,13 +215,6 @@ 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;
/**
* Custom security policy
*
@ -226,11 +222,6 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey;
*/
- (AFSecurityPolicy *) customSecurityPolicy;
/**
* Convert parameter to NSString
*/
- (NSString *) parameterToString: (id) param;
/**
* Log debug message
*/

View File

@ -0,0 +1,82 @@
#import "{{classPrefix}}Sanitizer.h"
#import "{{classPrefix}}Object.h"
#import "{{classPrefix}}QueryParamCollection.h"
#import <ISO8601/ISO8601.h>
@interface {{classPrefix}}Sanitizer ()
@end
@implementation {{classPrefix}}Sanitizer
- (id) sanitizeForSerialization:(id) object {
if (object == nil) {
return nil;
}
else if ([object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSNumber class]] || [object isKindOfClass:[{{classPrefix}}QueryParamCollection class]]) {
return object;
}
else if ([object isKindOfClass:[NSDate class]]) {
return [object ISO8601String];
}
else if ([object isKindOfClass:[NSArray class]]) {
NSArray *objectArray = object;
NSMutableArray *sanitizedObjs = [NSMutableArray arrayWithCapacity:[objectArray count]];
[object enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
id sanitizedObj = [self sanitizeForSerialization:obj];
if (sanitizedObj) {
[sanitizedObjs addObject:sanitizedObj];
}
}];
return sanitizedObjs;
}
else if ([object isKindOfClass:[NSDictionary class]]) {
NSDictionary *objectDict = object;
NSMutableDictionary *sanitizedObjs = [NSMutableDictionary dictionaryWithCapacity:[objectDict count]];
[object enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
id sanitizedObj = [self sanitizeForSerialization:obj];
if (sanitizedObj) {
sanitizedObjs[key] = sanitizedObj;
}
}];
return sanitizedObjs;
}
else if ([object isKindOfClass:[{{classPrefix}}Object class]]) {
return [object toDictionary];
}
else {
NSException *e = [NSException
exceptionWithName:@"InvalidObjectArgumentException"
reason:[NSString stringWithFormat:@"*** The argument object: %@ is invalid", object]
userInfo:nil];
@throw e;
}
}
- (NSString *) parameterToString:(id)param {
if ([param isKindOfClass:[NSString class]]) {
return param;
}
else if ([param isKindOfClass:[NSNumber class]]) {
return [param stringValue];
}
else if ([param isKindOfClass:[NSDate class]]) {
return [param ISO8601String];
}
else if ([param isKindOfClass:[NSArray class]]) {
NSMutableArray *mutableParam = [NSMutableArray array];
[param enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[mutableParam addObject:[self parameterToString:obj]];
}];
return [mutableParam componentsJoinedByString:@","];
}
else {
NSException *e = [NSException
exceptionWithName:@"InvalidObjectArgumentException"
reason:[NSString stringWithFormat:@"*** The argument object: %@ is invalid", param]
userInfo:nil];
@throw e;
}
}
@end

View File

@ -0,0 +1,29 @@
#import <Foundation/Foundation.h>
/**
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen
* Do not edit the class manually.
*/
@protocol {{classPrefix}}Sanitizer <NSObject>
/**
* Sanitize object for request
*
* @param object The query/path/header/form/body param to be sanitized.
*/
- (id) sanitizeForSerialization:(id) object;
/**
* Convert parameter to NSString
*/
- (NSString *) parameterToString: (id) param;
@end
@interface {{classPrefix}}Sanitizer : NSObject <{{classPrefix}}Sanitizer>
@end

View File

@ -6,6 +6,7 @@
#import "SWGQueryParamCollection.h"
#import "SWGConfiguration.h"
#import "SWGResponseDeserializer.h"
#import "SWGSanitizer.h"
/**
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen
@ -46,6 +47,8 @@ extern NSString *const SWGResponseObjectErrorKey;
@property(nonatomic, readonly) NSDictionary* HTTPResponseHeaders;
@property(nonatomic, strong) id<SWGResponseDeserializer> responseDeserializer;
@property(nonatomic, strong) id<SWGSanitizer> sanitizer;
/**
* Clears Cache
*/
@ -216,13 +219,6 @@ 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;
/**
* Custom security policy
*
@ -230,11 +226,6 @@ extern NSString *const SWGResponseObjectErrorKey;
*/
- (AFSecurityPolicy *) customSecurityPolicy;
/**
* Convert parameter to NSString
*/
- (NSString *) parameterToString: (id) param;
/**
* Log debug message
*/

View File

@ -54,6 +54,7 @@ static NSString * SWG__fileNameForResponse(NSURLResponse *response) {
self.responseSerializer = [AFJSONResponseSerializer serializer];
self.securityPolicy = [self customSecurityPolicy];
self.responseDeserializer = [[SWGResponseDeserializer alloc] init];
self.sanitizer = [[SWGSanitizer alloc] init];
// configure reachability
[self configureCacheReachibility];
}
@ -381,11 +382,11 @@ static NSString * SWG__fileNameForResponse(NSURLResponse *response) {
}
// sanitize parameters
pathParams = [self sanitizeForSerialization:pathParams];
queryParams = [self sanitizeForSerialization:queryParams];
headerParams = [self sanitizeForSerialization:headerParams];
formParams = [self sanitizeForSerialization:formParams];
body = [self sanitizeForSerialization:body];
pathParams = [self.sanitizer sanitizeForSerialization:pathParams];
queryParams = [self.sanitizer sanitizeForSerialization:queryParams];
headerParams = [self.sanitizer sanitizeForSerialization:headerParams];
formParams = [self.sanitizer sanitizeForSerialization:formParams];
body = [self.sanitizer sanitizeForSerialization:body];
// auth setting
[self updateHeaderParams:&headerParams queryParams:&queryParams WithAuthSettings:authSettings];
@ -405,12 +406,13 @@ static NSString * SWG__fileNameForResponse(NSURLResponse *response) {
NSString* urlString = [[NSURL URLWithString:pathWithQueryParams relativeToURL:self.baseURL] absoluteString];
if (files.count > 0) {
__weak __typeof(self)weakSelf = self;
request = [self.requestSerializer multipartFormRequestWithMethod:@"POST"
URLString:urlString
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formParams enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSString *objString = [self parameterToString:obj];
NSString *objString = [weakSelf.sanitizer parameterToString:obj];
NSData *data = [objString dataUsingEncoding:NSUTF8StringEncoding];
[formData appendPartWithFormData:data name:key];
}];
@ -572,48 +574,6 @@ static NSString * SWG__fileNameForResponse(NSURLResponse *response) {
*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]]) {
NSArray *objectArray = object;
NSMutableArray *sanitizedObjs = [NSMutableArray arrayWithCapacity:[objectArray count]];
[object enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (obj) {
[sanitizedObjs addObject:[self sanitizeForSerialization:obj]];
}
}];
return sanitizedObjs;
}
else if ([object isKindOfClass:[NSDictionary class]]) {
NSDictionary *objectDict = object;
NSMutableDictionary *sanitizedObjs = [NSMutableDictionary dictionaryWithCapacity:[objectDict 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;
}
}
- (AFSecurityPolicy *) customSecurityPolicy {
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
@ -635,30 +595,4 @@ static NSString * SWG__fileNameForResponse(NSURLResponse *response) {
return securityPolicy;
}
- (NSString *) parameterToString:(id)param {
if ([param isKindOfClass:[NSString class]]) {
return param;
}
else if ([param isKindOfClass:[NSNumber class]]) {
return [param stringValue];
}
else if ([param isKindOfClass:[NSDate class]]) {
return [param ISO8601String];
}
else if ([param isKindOfClass:[NSArray class]]) {
NSMutableArray *mutableParam;
[param enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[mutableParam addObject:[self parameterToString:obj]];
}];
return [mutableParam componentsJoinedByString:@","];
}
else {
NSException *e = [NSException
exceptionWithName:@"InvalidObjectArgumentException"
reason:[NSString stringWithFormat:@"*** The argument object: %@ is invalid", param]
userInfo:nil];
@throw e;
}
}
@end

View File

@ -0,0 +1,29 @@
#import <Foundation/Foundation.h>
/**
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen
* Do not edit the class manually.
*/
@protocol SWGSanitizer <NSObject>
/**
* Sanitize object for request
*
* @param object The query/path/header/form/body param to be sanitized.
*/
- (id) sanitizeForSerialization:(id) object;
/**
* Convert parameter to NSString
*/
- (NSString *) parameterToString: (id) param;
@end
@interface SWGSanitizer : NSObject <SWGSanitizer>
@end

View File

@ -0,0 +1,82 @@
#import "SWGSanitizer.h"
#import "SWGObject.h"
#import "SWGQueryParamCollection.h"
#import <ISO8601/ISO8601.h>
@interface SWGSanitizer ()
@end
@implementation SWGSanitizer
- (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]]) {
NSArray *objectArray = object;
NSMutableArray *sanitizedObjs = [NSMutableArray arrayWithCapacity:[objectArray count]];
[object enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
id sanitizedObj = [self sanitizeForSerialization:obj];
if (sanitizedObj) {
[sanitizedObjs addObject:sanitizedObj];
}
}];
return sanitizedObjs;
}
else if ([object isKindOfClass:[NSDictionary class]]) {
NSDictionary *objectDict = object;
NSMutableDictionary *sanitizedObjs = [NSMutableDictionary dictionaryWithCapacity:[objectDict count]];
[object enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
id sanitizedObj = [self sanitizeForSerialization:obj];
if (sanitizedObj) {
sanitizedObjs[key] = sanitizedObj;
}
}];
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;
}
}
- (NSString *) parameterToString:(id)param {
if ([param isKindOfClass:[NSString class]]) {
return param;
}
else if ([param isKindOfClass:[NSNumber class]]) {
return [param stringValue];
}
else if ([param isKindOfClass:[NSDate class]]) {
return [param ISO8601String];
}
else if ([param isKindOfClass:[NSArray class]]) {
NSMutableArray *mutableParam = [NSMutableArray array];
[param enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[mutableParam addObject:[self parameterToString:obj]];
}];
return [mutableParam componentsJoinedByString:@","];
}
else {
NSException *e = [NSException
exceptionWithName:@"InvalidObjectArgumentException"
reason:[NSString stringWithFormat:@"*** The argument object: %@ is invalid", param]
userInfo:nil];
@throw e;
}
}
@end

View File

@ -2,11 +2,6 @@
#import <XCTest/XCTest.h>
#import <ISO8601/ISO8601.h>
#import <SwaggerClient/SWGApiClient.h>
#import <SwaggerClient/SWGConfiguration.h>
#import <SwaggerClient/SWGQueryParamCollection.h>
#import <SwaggerClient/SWGPet.h>
#import <SwaggerClient/SWGTag.h>
#import <SwaggerClient/SWGCategory.h>
@interface SWGApiClientTest : XCTestCase
@ -109,31 +104,31 @@
// nil
data = nil;
result = [self.apiClient sanitizeForSerialization:data];
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
XCTAssertEqualObjects(result, data);
// NSString
data = @"test string";
result = [self.apiClient sanitizeForSerialization:data];
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
XCTAssertEqualObjects(result, data);
// NSNumber
data = @1;
result = [self.apiClient sanitizeForSerialization:data];
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
XCTAssertEqualObjects(result, data);
// SWGQueryParamCollection
data = [[SWGQueryParamCollection alloc] init];
result = [self.apiClient sanitizeForSerialization:data];
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
XCTAssertEqualObjects(result, data);
// NSDate
data = [NSDate dateWithISO8601String:@"1997-07-16T19:20:30.45+01:00"];
result = [self.apiClient sanitizeForSerialization:data];
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
XCTAssertEqualObjects(result, [data ISO8601String]);
data = [NSDate dateWithISO8601String:@"1997-07-16"];
result = [self.apiClient sanitizeForSerialization:data];
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
XCTAssertEqualObjects(result, [data ISO8601String]);
// model
@ -144,23 +139,23 @@
@"status": @"available",
@"photoUrls": @[@"http://foo.bar.com/3", @"http://foo.bar.com/4"]};
data = [[SWGPet alloc] initWithDictionary:petDict error:nil];
result = [self.apiClient sanitizeForSerialization:data];
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
XCTAssertEqualObjects(result, petDict);
// NSArray
data = @[@1];
result = [self.apiClient sanitizeForSerialization:data];
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
XCTAssertEqualObjects(result, data);
// NSArray of models
NSArray *arrayOfPetDict = @[petDict];
data = [NSArray arrayWithObject:[[SWGPet alloc] initWithDictionary:petDict error:nil]];
result = [self.apiClient sanitizeForSerialization:data];
data = @[[[SWGPet alloc] initWithDictionary:petDict error:nil]];
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
XCTAssertEqualObjects(result, arrayOfPetDict);
// NSDictionary
data = @{@"test key": @"test value"};
result = [self.apiClient sanitizeForSerialization:data];
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
XCTAssertEqualObjects(result, data);
}