mirror of
https://github.com/jlengrand/openapi-generator.git
synced 2026-05-17 15:54:36 +00:00
Merge pull request #2799 from mateuszmackowiak/obj/ApiClient-Sanitizer
Sanitizer for separating sanitize and service logic
This commit is contained in:
@@ -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"));
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user