forked from loafle/openapi-generator-original
Merge pull request #2799 from mateuszmackowiak/obj/ApiClient-Sanitizer
Sanitizer for separating sanitize and service logic
This commit is contained in:
commit
28c7ea5426
@ -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("JSONRequestSerializer-header.mustache", swaggerFolder, classPrefix + "JSONRequestSerializer.h"));
|
||||||
supportingFiles.add(new SupportingFile("ResponseDeserializer-body.mustache", swaggerFolder, classPrefix + "ResponseDeserializer.m"));
|
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("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.m", swaggerFolder, "JSONValueTransformer+ISO8601.m"));
|
||||||
supportingFiles.add(new SupportingFile("JSONValueTransformer+ISO8601.h", swaggerFolder, "JSONValueTransformer+ISO8601.h"));
|
supportingFiles.add(new SupportingFile("JSONValueTransformer+ISO8601.h", swaggerFolder, "JSONValueTransformer+ISO8601.h"));
|
||||||
supportingFiles.add(new SupportingFile("Configuration-body.mustache", swaggerFolder, classPrefix + "Configuration.m"));
|
supportingFiles.add(new SupportingFile("Configuration-body.mustache", swaggerFolder, classPrefix + "Configuration.m"));
|
||||||
|
@ -54,6 +54,7 @@ static NSString * {{classPrefix}}__fileNameForResponse(NSURLResponse *response)
|
|||||||
self.responseSerializer = [AFJSONResponseSerializer serializer];
|
self.responseSerializer = [AFJSONResponseSerializer serializer];
|
||||||
self.securityPolicy = [self customSecurityPolicy];
|
self.securityPolicy = [self customSecurityPolicy];
|
||||||
self.responseDeserializer = [[{{classPrefix}}ResponseDeserializer alloc] init];
|
self.responseDeserializer = [[{{classPrefix}}ResponseDeserializer alloc] init];
|
||||||
|
self.sanitizer = [[{{classPrefix}}Sanitizer alloc] init];
|
||||||
// configure reachability
|
// configure reachability
|
||||||
[self configureCacheReachibility];
|
[self configureCacheReachibility];
|
||||||
}
|
}
|
||||||
@ -381,11 +382,11 @@ static NSString * {{classPrefix}}__fileNameForResponse(NSURLResponse *response)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// sanitize parameters
|
// sanitize parameters
|
||||||
pathParams = [self sanitizeForSerialization:pathParams];
|
pathParams = [self.sanitizer sanitizeForSerialization:pathParams];
|
||||||
queryParams = [self sanitizeForSerialization:queryParams];
|
queryParams = [self.sanitizer sanitizeForSerialization:queryParams];
|
||||||
headerParams = [self sanitizeForSerialization:headerParams];
|
headerParams = [self.sanitizer sanitizeForSerialization:headerParams];
|
||||||
formParams = [self sanitizeForSerialization:formParams];
|
formParams = [self.sanitizer sanitizeForSerialization:formParams];
|
||||||
body = [self sanitizeForSerialization:body];
|
body = [self.sanitizer sanitizeForSerialization:body];
|
||||||
|
|
||||||
// auth setting
|
// auth setting
|
||||||
[self updateHeaderParams:&headerParams queryParams:&queryParams WithAuthSettings:authSettings];
|
[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];
|
NSString* urlString = [[NSURL URLWithString:pathWithQueryParams relativeToURL:self.baseURL] absoluteString];
|
||||||
if (files.count > 0) {
|
if (files.count > 0) {
|
||||||
|
__weak __typeof(self)weakSelf = self;
|
||||||
request = [self.requestSerializer multipartFormRequestWithMethod:@"POST"
|
request = [self.requestSerializer multipartFormRequestWithMethod:@"POST"
|
||||||
URLString:urlString
|
URLString:urlString
|
||||||
parameters:nil
|
parameters:nil
|
||||||
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
|
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
|
||||||
[formParams enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
|
[formParams enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
|
||||||
NSString *objString = [self parameterToString:obj];
|
NSString *objString = [weakSelf.sanitizer parameterToString:obj];
|
||||||
NSData *data = [objString dataUsingEncoding:NSUTF8StringEncoding];
|
NSData *data = [objString dataUsingEncoding:NSUTF8StringEncoding];
|
||||||
[formData appendPartWithFormData:data name:key];
|
[formData appendPartWithFormData:data name:key];
|
||||||
}];
|
}];
|
||||||
@ -572,48 +574,6 @@ static NSString * {{classPrefix}}__fileNameForResponse(NSURLResponse *response)
|
|||||||
*querys = [NSDictionary dictionaryWithDictionary:querysWithAuth];
|
*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 *) customSecurityPolicy {
|
||||||
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
|
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
|
||||||
|
|
||||||
@ -635,30 +595,4 @@ static NSString * {{classPrefix}}__fileNameForResponse(NSURLResponse *response)
|
|||||||
return securityPolicy;
|
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
|
@end
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#import "{{classPrefix}}QueryParamCollection.h"
|
#import "{{classPrefix}}QueryParamCollection.h"
|
||||||
#import "{{classPrefix}}Configuration.h"
|
#import "{{classPrefix}}Configuration.h"
|
||||||
#import "{{classPrefix}}ResponseDeserializer.h"
|
#import "{{classPrefix}}ResponseDeserializer.h"
|
||||||
|
#import "{{classPrefix}}Sanitizer.h"
|
||||||
/**
|
/**
|
||||||
* NOTE: This class is auto generated by the swagger code generator program.
|
* NOTE: This class is auto generated by the swagger code generator program.
|
||||||
* https://github.com/swagger-api/swagger-codegen
|
* https://github.com/swagger-api/swagger-codegen
|
||||||
@ -42,6 +43,8 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey;
|
|||||||
@property(nonatomic, readonly) NSDictionary* HTTPResponseHeaders;
|
@property(nonatomic, readonly) NSDictionary* HTTPResponseHeaders;
|
||||||
|
|
||||||
@property(nonatomic, strong) id<{{classPrefix}}ResponseDeserializer> responseDeserializer;
|
@property(nonatomic, strong) id<{{classPrefix}}ResponseDeserializer> responseDeserializer;
|
||||||
|
|
||||||
|
@property(nonatomic, strong) id<{{classPrefix}}Sanitizer> sanitizer;
|
||||||
/**
|
/**
|
||||||
* Clears Cache
|
* Clears Cache
|
||||||
*/
|
*/
|
||||||
@ -212,13 +215,6 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey;
|
|||||||
responseType:(NSString *) responseType
|
responseType:(NSString *) responseType
|
||||||
completionBlock:(void (^)(id, NSError *))completionBlock;
|
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
|
* Custom security policy
|
||||||
*
|
*
|
||||||
@ -226,11 +222,6 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey;
|
|||||||
*/
|
*/
|
||||||
- (AFSecurityPolicy *) customSecurityPolicy;
|
- (AFSecurityPolicy *) customSecurityPolicy;
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert parameter to NSString
|
|
||||||
*/
|
|
||||||
- (NSString *) parameterToString: (id) param;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Log debug message
|
* Log debug message
|
||||||
*/
|
*/
|
||||||
|
@ -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
|
@ -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
|
@ -6,6 +6,7 @@
|
|||||||
#import "SWGQueryParamCollection.h"
|
#import "SWGQueryParamCollection.h"
|
||||||
#import "SWGConfiguration.h"
|
#import "SWGConfiguration.h"
|
||||||
#import "SWGResponseDeserializer.h"
|
#import "SWGResponseDeserializer.h"
|
||||||
|
#import "SWGSanitizer.h"
|
||||||
/**
|
/**
|
||||||
* NOTE: This class is auto generated by the swagger code generator program.
|
* NOTE: This class is auto generated by the swagger code generator program.
|
||||||
* https://github.com/swagger-api/swagger-codegen
|
* https://github.com/swagger-api/swagger-codegen
|
||||||
@ -46,6 +47,8 @@ extern NSString *const SWGResponseObjectErrorKey;
|
|||||||
@property(nonatomic, readonly) NSDictionary* HTTPResponseHeaders;
|
@property(nonatomic, readonly) NSDictionary* HTTPResponseHeaders;
|
||||||
|
|
||||||
@property(nonatomic, strong) id<SWGResponseDeserializer> responseDeserializer;
|
@property(nonatomic, strong) id<SWGResponseDeserializer> responseDeserializer;
|
||||||
|
|
||||||
|
@property(nonatomic, strong) id<SWGSanitizer> sanitizer;
|
||||||
/**
|
/**
|
||||||
* Clears Cache
|
* Clears Cache
|
||||||
*/
|
*/
|
||||||
@ -216,13 +219,6 @@ extern NSString *const SWGResponseObjectErrorKey;
|
|||||||
responseType:(NSString *) responseType
|
responseType:(NSString *) responseType
|
||||||
completionBlock:(void (^)(id, NSError *))completionBlock;
|
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
|
* Custom security policy
|
||||||
*
|
*
|
||||||
@ -230,11 +226,6 @@ extern NSString *const SWGResponseObjectErrorKey;
|
|||||||
*/
|
*/
|
||||||
- (AFSecurityPolicy *) customSecurityPolicy;
|
- (AFSecurityPolicy *) customSecurityPolicy;
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert parameter to NSString
|
|
||||||
*/
|
|
||||||
- (NSString *) parameterToString: (id) param;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Log debug message
|
* Log debug message
|
||||||
*/
|
*/
|
||||||
|
@ -54,6 +54,7 @@ static NSString * SWG__fileNameForResponse(NSURLResponse *response) {
|
|||||||
self.responseSerializer = [AFJSONResponseSerializer serializer];
|
self.responseSerializer = [AFJSONResponseSerializer serializer];
|
||||||
self.securityPolicy = [self customSecurityPolicy];
|
self.securityPolicy = [self customSecurityPolicy];
|
||||||
self.responseDeserializer = [[SWGResponseDeserializer alloc] init];
|
self.responseDeserializer = [[SWGResponseDeserializer alloc] init];
|
||||||
|
self.sanitizer = [[SWGSanitizer alloc] init];
|
||||||
// configure reachability
|
// configure reachability
|
||||||
[self configureCacheReachibility];
|
[self configureCacheReachibility];
|
||||||
}
|
}
|
||||||
@ -381,11 +382,11 @@ static NSString * SWG__fileNameForResponse(NSURLResponse *response) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// sanitize parameters
|
// sanitize parameters
|
||||||
pathParams = [self sanitizeForSerialization:pathParams];
|
pathParams = [self.sanitizer sanitizeForSerialization:pathParams];
|
||||||
queryParams = [self sanitizeForSerialization:queryParams];
|
queryParams = [self.sanitizer sanitizeForSerialization:queryParams];
|
||||||
headerParams = [self sanitizeForSerialization:headerParams];
|
headerParams = [self.sanitizer sanitizeForSerialization:headerParams];
|
||||||
formParams = [self sanitizeForSerialization:formParams];
|
formParams = [self.sanitizer sanitizeForSerialization:formParams];
|
||||||
body = [self sanitizeForSerialization:body];
|
body = [self.sanitizer sanitizeForSerialization:body];
|
||||||
|
|
||||||
// auth setting
|
// auth setting
|
||||||
[self updateHeaderParams:&headerParams queryParams:&queryParams WithAuthSettings:authSettings];
|
[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];
|
NSString* urlString = [[NSURL URLWithString:pathWithQueryParams relativeToURL:self.baseURL] absoluteString];
|
||||||
if (files.count > 0) {
|
if (files.count > 0) {
|
||||||
|
__weak __typeof(self)weakSelf = self;
|
||||||
request = [self.requestSerializer multipartFormRequestWithMethod:@"POST"
|
request = [self.requestSerializer multipartFormRequestWithMethod:@"POST"
|
||||||
URLString:urlString
|
URLString:urlString
|
||||||
parameters:nil
|
parameters:nil
|
||||||
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
|
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
|
||||||
[formParams enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
|
[formParams enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
|
||||||
NSString *objString = [self parameterToString:obj];
|
NSString *objString = [weakSelf.sanitizer parameterToString:obj];
|
||||||
NSData *data = [objString dataUsingEncoding:NSUTF8StringEncoding];
|
NSData *data = [objString dataUsingEncoding:NSUTF8StringEncoding];
|
||||||
[formData appendPartWithFormData:data name:key];
|
[formData appendPartWithFormData:data name:key];
|
||||||
}];
|
}];
|
||||||
@ -572,48 +574,6 @@ static NSString * SWG__fileNameForResponse(NSURLResponse *response) {
|
|||||||
*querys = [NSDictionary dictionaryWithDictionary:querysWithAuth];
|
*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 *) customSecurityPolicy {
|
||||||
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
|
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
|
||||||
|
|
||||||
@ -635,30 +595,4 @@ static NSString * SWG__fileNameForResponse(NSURLResponse *response) {
|
|||||||
return securityPolicy;
|
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
|
@end
|
||||||
|
29
samples/client/petstore/objc/SwaggerClient/SWGSanitizer.h
Normal file
29
samples/client/petstore/objc/SwaggerClient/SWGSanitizer.h
Normal 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
|
82
samples/client/petstore/objc/SwaggerClient/SWGSanitizer.m
Normal file
82
samples/client/petstore/objc/SwaggerClient/SWGSanitizer.m
Normal 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
|
@ -2,11 +2,6 @@
|
|||||||
#import <XCTest/XCTest.h>
|
#import <XCTest/XCTest.h>
|
||||||
#import <ISO8601/ISO8601.h>
|
#import <ISO8601/ISO8601.h>
|
||||||
#import <SwaggerClient/SWGApiClient.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
|
@interface SWGApiClientTest : XCTestCase
|
||||||
|
|
||||||
@ -109,31 +104,31 @@
|
|||||||
|
|
||||||
// nil
|
// nil
|
||||||
data = nil;
|
data = nil;
|
||||||
result = [self.apiClient sanitizeForSerialization:data];
|
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
|
||||||
XCTAssertEqualObjects(result, data);
|
XCTAssertEqualObjects(result, data);
|
||||||
|
|
||||||
// NSString
|
// NSString
|
||||||
data = @"test string";
|
data = @"test string";
|
||||||
result = [self.apiClient sanitizeForSerialization:data];
|
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
|
||||||
XCTAssertEqualObjects(result, data);
|
XCTAssertEqualObjects(result, data);
|
||||||
|
|
||||||
// NSNumber
|
// NSNumber
|
||||||
data = @1;
|
data = @1;
|
||||||
result = [self.apiClient sanitizeForSerialization:data];
|
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
|
||||||
XCTAssertEqualObjects(result, data);
|
XCTAssertEqualObjects(result, data);
|
||||||
|
|
||||||
// SWGQueryParamCollection
|
// SWGQueryParamCollection
|
||||||
data = [[SWGQueryParamCollection alloc] init];
|
data = [[SWGQueryParamCollection alloc] init];
|
||||||
result = [self.apiClient sanitizeForSerialization:data];
|
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
|
||||||
XCTAssertEqualObjects(result, data);
|
XCTAssertEqualObjects(result, data);
|
||||||
|
|
||||||
// NSDate
|
// NSDate
|
||||||
data = [NSDate dateWithISO8601String:@"1997-07-16T19:20:30.45+01:00"];
|
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]);
|
XCTAssertEqualObjects(result, [data ISO8601String]);
|
||||||
|
|
||||||
data = [NSDate dateWithISO8601String:@"1997-07-16"];
|
data = [NSDate dateWithISO8601String:@"1997-07-16"];
|
||||||
result = [self.apiClient sanitizeForSerialization:data];
|
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
|
||||||
XCTAssertEqualObjects(result, [data ISO8601String]);
|
XCTAssertEqualObjects(result, [data ISO8601String]);
|
||||||
|
|
||||||
// model
|
// model
|
||||||
@ -144,23 +139,23 @@
|
|||||||
@"status": @"available",
|
@"status": @"available",
|
||||||
@"photoUrls": @[@"http://foo.bar.com/3", @"http://foo.bar.com/4"]};
|
@"photoUrls": @[@"http://foo.bar.com/3", @"http://foo.bar.com/4"]};
|
||||||
data = [[SWGPet alloc] initWithDictionary:petDict error:nil];
|
data = [[SWGPet alloc] initWithDictionary:petDict error:nil];
|
||||||
result = [self.apiClient sanitizeForSerialization:data];
|
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
|
||||||
XCTAssertEqualObjects(result, petDict);
|
XCTAssertEqualObjects(result, petDict);
|
||||||
|
|
||||||
// NSArray
|
// NSArray
|
||||||
data = @[@1];
|
data = @[@1];
|
||||||
result = [self.apiClient sanitizeForSerialization:data];
|
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
|
||||||
XCTAssertEqualObjects(result, data);
|
XCTAssertEqualObjects(result, data);
|
||||||
|
|
||||||
// NSArray of models
|
// NSArray of models
|
||||||
NSArray *arrayOfPetDict = @[petDict];
|
NSArray *arrayOfPetDict = @[petDict];
|
||||||
data = [NSArray arrayWithObject:[[SWGPet alloc] initWithDictionary:petDict error:nil]];
|
data = @[[[SWGPet alloc] initWithDictionary:petDict error:nil]];
|
||||||
result = [self.apiClient sanitizeForSerialization:data];
|
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
|
||||||
XCTAssertEqualObjects(result, arrayOfPetDict);
|
XCTAssertEqualObjects(result, arrayOfPetDict);
|
||||||
|
|
||||||
// NSDictionary
|
// NSDictionary
|
||||||
data = @{@"test key": @"test value"};
|
data = @{@"test key": @"test value"};
|
||||||
result = [self.apiClient sanitizeForSerialization:data];
|
result = [self.apiClient.sanitizer sanitizeForSerialization:data];
|
||||||
XCTAssertEqualObjects(result, data);
|
XCTAssertEqualObjects(result, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user