diff --git a/modules/swagger-codegen/src/main/resources/objc/SWGApiClient.m b/modules/swagger-codegen/src/main/resources/objc/SWGApiClient.m index f84d7b3f62..5b7fd03417 100644 --- a/modules/swagger-codegen/src/main/resources/objc/SWGApiClient.m +++ b/modules/swagger-codegen/src/main/resources/objc/SWGApiClient.m @@ -31,13 +31,13 @@ static bool loggingEnabled = true; diskSize: (unsigned long) diskSize { NSAssert(memorySize > 0, @"invalid in-memory cache size"); NSAssert(diskSize >= 0, @"invalid disk cache size"); - + NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:memorySize diskCapacity:diskSize diskPath:@"swagger_url_cache"]; - + [NSURLCache setSharedURLCache:cache]; } @@ -54,17 +54,17 @@ static bool loggingEnabled = true; // setup static vars // create queue sharedQueue = [[NSOperationQueue alloc] init]; - + // create pool _pool = [[NSMutableDictionary alloc] init]; - + // initialize URL cache [SWGApiClient configureCacheWithMemoryAndDiskCapacity:4*1024*1024 diskSize:32*1024*1024]; - + // configure reachability [SWGApiClient configureCacheReachibilityForHost:baseUrl]; } - + @synchronized(self) { SWGApiClient * client = [_pool objectForKey:baseUrl]; if (client == nil) { @@ -128,7 +128,7 @@ static bool loggingEnabled = true; return TRUE; else return FALSE; }]; - + if(matchingItems.count == 1) { if(loggingEnabled) NSLog(@"removing request id %@", requestId); @@ -169,19 +169,19 @@ static bool loggingEnabled = true; NSLog(@"reachability changed to AFNetworkReachabilityStatusUnknown"); [SWGApiClient setOfflineState:true]; break; - + case AFNetworkReachabilityStatusNotReachable: if(loggingEnabled) NSLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable"); [SWGApiClient setOfflineState:true]; break; - + case AFNetworkReachabilityStatusReachableViaWWAN: if(loggingEnabled) NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN"); [SWGApiClient setOfflineState:false]; break; - + case AFNetworkReachabilityStatusReachableViaWiFi: if(loggingEnabled) NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi"); @@ -202,7 +202,7 @@ static bool loggingEnabled = true; queryParams:(NSDictionary*) queryParams { NSString * separator = nil; int counter = 0; - + NSMutableString * requestUrl = [NSMutableString stringWithFormat:@"%@", path]; if(queryParams != nil){ for(NSString * key in [queryParams keyEnumerator]){ @@ -218,7 +218,7 @@ static bool loggingEnabled = true; SWGQueryParamCollection * coll = (SWGQueryParamCollection*) queryParam; NSArray* values = [coll values]; NSString* format = [coll format]; - + if([format isEqualToString:@"csv"]) { [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, [SWGApiClient escape:key], [NSString stringWithFormat:@"%@", [values componentsJoinedByString:@","]]]]; @@ -274,7 +274,28 @@ static bool loggingEnabled = true; requestContentType: (NSString*) requestContentType responseContentType: (NSString*) responseContentType completionBlock: (void (^)(NSDictionary*, NSError *))completionBlock { - + // setting request serializer + if ([requestContentType isEqualToString:@"application/json"]) { + self.requestSerializer = [AFJSONRequestSerializer serializer]; + } + else if ([requestContentType isEqualToString:@"application/x-www-form-urlencoded"]) { + self.requestSerializer = [AFHTTPRequestSerializer serializer]; + } + else if ([requestContentType isEqualToString:@"multipart/form-data"]) { + self.requestSerializer = [AFHTTPRequestSerializer serializer]; + } + else { + NSAssert(false, @"unsupport request type %@", requestContentType); + } + + // setting response serializer + if ([responseContentType isEqualToString:@"application/json"]) { + self.responseSerializer = [AFJSONResponseSerializer serializer]; + } + else { + self.responseSerializer = [AFHTTPResponseSerializer serializer]; + } + NSMutableURLRequest * request = nil; if (body != nil && [body isKindOfClass:[NSArray class]]){ SWGFile * file; @@ -291,7 +312,8 @@ static bool loggingEnabled = true; } } NSString * urlString = [[NSURL URLWithString:path relativeToURL:self.baseURL] absoluteString]; - + + // request with multipart form if(file != nil) { request = [self.requestSerializer multipartFormRequestWithMethod: @"POST" URLString: urlString @@ -302,20 +324,30 @@ static bool loggingEnabled = true; NSData* data = [params[key] dataUsingEncoding:NSUTF8StringEncoding]; [formData appendPartWithFormData: data name: key]; } - + [formData appendPartWithFileData: [file data] name: [file paramName] fileName: [file name] mimeType: [file mimeType]]; - + } error:nil]; } + // request with form parameters + else { + NSString* pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams]; + NSString* urlString = [[NSURL URLWithString:pathWithQueryParams relativeToURL:self.baseURL] absoluteString]; + + request = [self.requestSerializer requestWithMethod:method + URLString:urlString + parameters:params + error:nil]; + } } else { NSString * pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams]; NSString * urlString = [[NSURL URLWithString:pathWithQueryParams relativeToURL:self.baseURL] absoluteString]; - + request = [self.requestSerializer requestWithMethod:method URLString:urlString parameters:body @@ -337,11 +369,9 @@ static bool loggingEnabled = true; [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; } - AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer]; - if(body != nil) { if([body isKindOfClass:[NSDictionary class]] || [body isKindOfClass:[NSArray class]]){ - [requestSerializer setValue:requestContentType forHTTPHeaderField:@"Content-Type"]; + [self.requestSerializer setValue:requestContentType forHTTPHeaderField:@"Content-Type"]; } else if ([body isKindOfClass:[SWGFile class]]) {} else { @@ -353,16 +383,16 @@ static bool loggingEnabled = true; [request setValue:[headerParams valueForKey:key] forHTTPHeaderField:key]; } } - [requestSerializer setValue:responseContentType forHTTPHeaderField:@"Accept"]; - + [self.requestSerializer setValue:responseContentType forHTTPHeaderField:@"Accept"]; + // Always disable cookies! [request setHTTPShouldHandleCookies:NO]; - - + + if (self.logRequests) { [self logRequest:request]; } - + NSNumber* requestId = [SWGApiClient queueRequest]; AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request @@ -380,14 +410,14 @@ static bool loggingEnabled = true; userInfo[SWGResponseObjectErrorKey] = operation.responseObject; } NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; - + if(self.logServerResponses) [self logResponse:nil forRequest:request error:augmentedError]; completionBlock(nil, augmentedError); } } ]; - + [self.operationQueue addOperation:op]; return requestId; } @@ -400,6 +430,28 @@ static bool loggingEnabled = true; requestContentType: (NSString*) requestContentType responseContentType: (NSString*) responseContentType completionBlock: (void (^)(NSString*, NSError *))completionBlock { + // setting request serializer + if ([requestContentType isEqualToString:@"application/json"]) { + self.requestSerializer = [AFJSONRequestSerializer serializer]; + } + else if ([requestContentType isEqualToString:@"application/x-www-form-urlencoded"]) { + self.requestSerializer = [AFHTTPRequestSerializer serializer]; + } + else if ([requestContentType isEqualToString:@"multipart/form-data"]) { + self.requestSerializer = [AFHTTPRequestSerializer serializer]; + } + else { + NSAssert(false, @"unsupport request type %@", requestContentType); + } + + // setting response serializer + if ([responseContentType isEqualToString:@"application/json"]) { + self.responseSerializer = [AFJSONResponseSerializer serializer]; + } + else { + self.responseSerializer = [AFHTTPResponseSerializer serializer]; + } + NSMutableURLRequest * request = nil; if (body != nil && [body isKindOfClass:[NSArray class]]){ SWGFile * file; @@ -416,31 +468,43 @@ static bool loggingEnabled = true; } } NSString * urlString = [[NSURL URLWithString:path relativeToURL:self.baseURL] absoluteString]; - + + // request with multipart form if(file != nil) { request = [self.requestSerializer multipartFormRequestWithMethod: @"POST" URLString: urlString parameters: nil constructingBodyWithBlock: ^(id formData) { - + for(NSString * key in params) { NSData* data = [params[key] dataUsingEncoding:NSUTF8StringEncoding]; [formData appendPartWithFormData: data name: key]; } - + [formData appendPartWithFileData: [file data] name: [file paramName] fileName: [file name] mimeType: [file mimeType]]; - + } error:nil]; } + // request with form parameters + else { + NSString* pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams]; + NSString* urlString = [[NSURL URLWithString:pathWithQueryParams relativeToURL:self.baseURL] absoluteString]; + + request = [self.requestSerializer requestWithMethod:method + URLString:urlString + parameters:params + error:nil]; + } + } else { NSString * pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams]; NSString * urlString = [[NSURL URLWithString:pathWithQueryParams relativeToURL:self.baseURL] absoluteString]; - + request = [self.requestSerializer requestWithMethod: method URLString: urlString parameters: body @@ -461,13 +525,11 @@ static bool loggingEnabled = true; NSLog(@"%@ cache disabled", path); [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; } - - - AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer]; + if(body != nil) { if([body isKindOfClass:[NSDictionary class]] || [body isKindOfClass:[NSArray class]]){ - [requestSerializer setValue:requestContentType forHTTPHeaderField:@"Content-Type"]; + [self.requestSerializer setValue:requestContentType forHTTPHeaderField:@"Content-Type"]; } else if ([body isKindOfClass:[SWGFile class]]){} else { @@ -479,12 +541,12 @@ static bool loggingEnabled = true; [request setValue:[headerParams valueForKey:key] forHTTPHeaderField:key]; } } - [requestSerializer setValue:responseContentType forHTTPHeaderField:@"Accept"]; + [self.requestSerializer setValue:responseContentType forHTTPHeaderField:@"Accept"]; + - // Always disable cookies! [request setHTTPShouldHandleCookies:NO]; - + NSNumber* requestId = [SWGApiClient queueRequest]; AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { @@ -502,15 +564,15 @@ static bool loggingEnabled = true; userInfo[SWGResponseObjectErrorKey] = operation.responseObject; } NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; - + if(self.logServerResponses) [self logResponse:nil forRequest:request error:augmentedError]; completionBlock(nil, augmentedError); } }]; - + [self.operationQueue addOperation:op]; return requestId; } -@end \ No newline at end of file +@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 cafaf6ad2b..068cf5d3e5 100644 --- a/modules/swagger-codegen/src/main/resources/objc/api-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/api-body.mustache @@ -68,8 +68,11 @@ static NSString * basePath = @"{{basePath}}"; {{#pathParams}}[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"{{baseName}}", @"}"]] withString: [SWGApiClient escape:{{paramName}}]]; {{/pathParams}} - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[{{#consumes}}@"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}}]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[{{#produces}}@"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}}]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; {{#queryParams}}if({{paramName}} != nil) { @@ -116,7 +119,7 @@ static NSString * basePath = @"{{basePath}}"; {{/bodyParam}} {{^bodyParam}} - NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; + NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; {{#formParams}} {{#notFile}} @@ -126,8 +129,10 @@ static NSString * basePath = @"{{basePath}}"; if(bodyDictionary == nil) { bodyDictionary = [[NSMutableArray alloc] init]; } - [bodyDictionary addObject:{{paramName}}]; - {{paramName}}.paramName = @"{{baseName}}"; + if({{paramName}} != nil) { + [bodyDictionary addObject:{{paramName}}]; + {{paramName}}.paramName = @"{{baseName}}"; + } {{/isFile}} if(bodyDictionary == nil) { bodyDictionary = [[NSMutableArray alloc] init]; @@ -172,4 +177,4 @@ static NSString * basePath = @"{{basePath}}"; {{newline}} {{/operations}} -@end \ No newline at end of file +@end diff --git a/samples/client/petstore/objc/PetstoreClient/PetstoreClientTests/PetApiTest.m b/samples/client/petstore/objc/PetstoreClient/PetstoreClientTests/PetApiTest.m index 7ad03afee1..6fb2b6e57c 100644 --- a/samples/client/petstore/objc/PetstoreClient/PetstoreClientTests/PetApiTest.m +++ b/samples/client/petstore/objc/PetstoreClient/PetstoreClientTests/PetApiTest.m @@ -154,7 +154,7 @@ } - (void)testDeletePet { - XCTestExpectation *expectation = [self expectationWithDescription:@"testGetPetById"]; + XCTestExpectation *expectation = [self expectationWithDescription:@"testDeletePet"]; SWGPet* pet = [self createPet]; @@ -181,6 +181,42 @@ [self waitForExpectationsWithTimeout:10.0 handler:nil]; } +- (void)testUploadFile { + XCTestExpectation *expectation = [self expectationWithDescription:@"testUploadFile"]; + + NSString* str = @"teststring"; + NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding]; + + SWGFile * file = [[SWGFile alloc] initWithNameData: @"myFile.txt" mimeType:@"text/plain" data:data]; + + [api uploadFileWithCompletionBlock:@1 additionalMetadata:@"special-metadata" file:file completionHandler:^(NSError *error) { + if(error) { + // good + XCTFail(@"expected a failure"); + + } + else { + [expectation fulfill]; + } + }]; + [self waitForExpectationsWithTimeout:10.0 handler:nil]; +} + +- (void)TestUploadWithoutFile { + XCTestExpectation *expectation = [self expectationWithDescription:@"testUploadWithoutFile"]; + + [api uploadFileWithCompletionBlock:@1 additionalMetadata:@"special-metadata" file:nil completionHandler:^(NSError *error) { + if(error) { + XCTFail(@"failed to upload"); + + } + else { + [expectation fulfill]; + } + }]; + [self waitForExpectationsWithTimeout:10.0 handler:nil]; +} + - (SWGPet*) createPet { SWGPet * pet = [[SWGPet alloc] init]; pet._id = [[NSNumber alloc] initWithLong:[[NSDate date] timeIntervalSince1970]]; diff --git a/samples/client/petstore/objc/client/SWGApiClient.m b/samples/client/petstore/objc/client/SWGApiClient.m index f84d7b3f62..5b7fd03417 100644 --- a/samples/client/petstore/objc/client/SWGApiClient.m +++ b/samples/client/petstore/objc/client/SWGApiClient.m @@ -31,13 +31,13 @@ static bool loggingEnabled = true; diskSize: (unsigned long) diskSize { NSAssert(memorySize > 0, @"invalid in-memory cache size"); NSAssert(diskSize >= 0, @"invalid disk cache size"); - + NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:memorySize diskCapacity:diskSize diskPath:@"swagger_url_cache"]; - + [NSURLCache setSharedURLCache:cache]; } @@ -54,17 +54,17 @@ static bool loggingEnabled = true; // setup static vars // create queue sharedQueue = [[NSOperationQueue alloc] init]; - + // create pool _pool = [[NSMutableDictionary alloc] init]; - + // initialize URL cache [SWGApiClient configureCacheWithMemoryAndDiskCapacity:4*1024*1024 diskSize:32*1024*1024]; - + // configure reachability [SWGApiClient configureCacheReachibilityForHost:baseUrl]; } - + @synchronized(self) { SWGApiClient * client = [_pool objectForKey:baseUrl]; if (client == nil) { @@ -128,7 +128,7 @@ static bool loggingEnabled = true; return TRUE; else return FALSE; }]; - + if(matchingItems.count == 1) { if(loggingEnabled) NSLog(@"removing request id %@", requestId); @@ -169,19 +169,19 @@ static bool loggingEnabled = true; NSLog(@"reachability changed to AFNetworkReachabilityStatusUnknown"); [SWGApiClient setOfflineState:true]; break; - + case AFNetworkReachabilityStatusNotReachable: if(loggingEnabled) NSLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable"); [SWGApiClient setOfflineState:true]; break; - + case AFNetworkReachabilityStatusReachableViaWWAN: if(loggingEnabled) NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN"); [SWGApiClient setOfflineState:false]; break; - + case AFNetworkReachabilityStatusReachableViaWiFi: if(loggingEnabled) NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi"); @@ -202,7 +202,7 @@ static bool loggingEnabled = true; queryParams:(NSDictionary*) queryParams { NSString * separator = nil; int counter = 0; - + NSMutableString * requestUrl = [NSMutableString stringWithFormat:@"%@", path]; if(queryParams != nil){ for(NSString * key in [queryParams keyEnumerator]){ @@ -218,7 +218,7 @@ static bool loggingEnabled = true; SWGQueryParamCollection * coll = (SWGQueryParamCollection*) queryParam; NSArray* values = [coll values]; NSString* format = [coll format]; - + if([format isEqualToString:@"csv"]) { [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, [SWGApiClient escape:key], [NSString stringWithFormat:@"%@", [values componentsJoinedByString:@","]]]]; @@ -274,7 +274,28 @@ static bool loggingEnabled = true; requestContentType: (NSString*) requestContentType responseContentType: (NSString*) responseContentType completionBlock: (void (^)(NSDictionary*, NSError *))completionBlock { - + // setting request serializer + if ([requestContentType isEqualToString:@"application/json"]) { + self.requestSerializer = [AFJSONRequestSerializer serializer]; + } + else if ([requestContentType isEqualToString:@"application/x-www-form-urlencoded"]) { + self.requestSerializer = [AFHTTPRequestSerializer serializer]; + } + else if ([requestContentType isEqualToString:@"multipart/form-data"]) { + self.requestSerializer = [AFHTTPRequestSerializer serializer]; + } + else { + NSAssert(false, @"unsupport request type %@", requestContentType); + } + + // setting response serializer + if ([responseContentType isEqualToString:@"application/json"]) { + self.responseSerializer = [AFJSONResponseSerializer serializer]; + } + else { + self.responseSerializer = [AFHTTPResponseSerializer serializer]; + } + NSMutableURLRequest * request = nil; if (body != nil && [body isKindOfClass:[NSArray class]]){ SWGFile * file; @@ -291,7 +312,8 @@ static bool loggingEnabled = true; } } NSString * urlString = [[NSURL URLWithString:path relativeToURL:self.baseURL] absoluteString]; - + + // request with multipart form if(file != nil) { request = [self.requestSerializer multipartFormRequestWithMethod: @"POST" URLString: urlString @@ -302,20 +324,30 @@ static bool loggingEnabled = true; NSData* data = [params[key] dataUsingEncoding:NSUTF8StringEncoding]; [formData appendPartWithFormData: data name: key]; } - + [formData appendPartWithFileData: [file data] name: [file paramName] fileName: [file name] mimeType: [file mimeType]]; - + } error:nil]; } + // request with form parameters + else { + NSString* pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams]; + NSString* urlString = [[NSURL URLWithString:pathWithQueryParams relativeToURL:self.baseURL] absoluteString]; + + request = [self.requestSerializer requestWithMethod:method + URLString:urlString + parameters:params + error:nil]; + } } else { NSString * pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams]; NSString * urlString = [[NSURL URLWithString:pathWithQueryParams relativeToURL:self.baseURL] absoluteString]; - + request = [self.requestSerializer requestWithMethod:method URLString:urlString parameters:body @@ -337,11 +369,9 @@ static bool loggingEnabled = true; [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; } - AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer]; - if(body != nil) { if([body isKindOfClass:[NSDictionary class]] || [body isKindOfClass:[NSArray class]]){ - [requestSerializer setValue:requestContentType forHTTPHeaderField:@"Content-Type"]; + [self.requestSerializer setValue:requestContentType forHTTPHeaderField:@"Content-Type"]; } else if ([body isKindOfClass:[SWGFile class]]) {} else { @@ -353,16 +383,16 @@ static bool loggingEnabled = true; [request setValue:[headerParams valueForKey:key] forHTTPHeaderField:key]; } } - [requestSerializer setValue:responseContentType forHTTPHeaderField:@"Accept"]; - + [self.requestSerializer setValue:responseContentType forHTTPHeaderField:@"Accept"]; + // Always disable cookies! [request setHTTPShouldHandleCookies:NO]; - - + + if (self.logRequests) { [self logRequest:request]; } - + NSNumber* requestId = [SWGApiClient queueRequest]; AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request @@ -380,14 +410,14 @@ static bool loggingEnabled = true; userInfo[SWGResponseObjectErrorKey] = operation.responseObject; } NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; - + if(self.logServerResponses) [self logResponse:nil forRequest:request error:augmentedError]; completionBlock(nil, augmentedError); } } ]; - + [self.operationQueue addOperation:op]; return requestId; } @@ -400,6 +430,28 @@ static bool loggingEnabled = true; requestContentType: (NSString*) requestContentType responseContentType: (NSString*) responseContentType completionBlock: (void (^)(NSString*, NSError *))completionBlock { + // setting request serializer + if ([requestContentType isEqualToString:@"application/json"]) { + self.requestSerializer = [AFJSONRequestSerializer serializer]; + } + else if ([requestContentType isEqualToString:@"application/x-www-form-urlencoded"]) { + self.requestSerializer = [AFHTTPRequestSerializer serializer]; + } + else if ([requestContentType isEqualToString:@"multipart/form-data"]) { + self.requestSerializer = [AFHTTPRequestSerializer serializer]; + } + else { + NSAssert(false, @"unsupport request type %@", requestContentType); + } + + // setting response serializer + if ([responseContentType isEqualToString:@"application/json"]) { + self.responseSerializer = [AFJSONResponseSerializer serializer]; + } + else { + self.responseSerializer = [AFHTTPResponseSerializer serializer]; + } + NSMutableURLRequest * request = nil; if (body != nil && [body isKindOfClass:[NSArray class]]){ SWGFile * file; @@ -416,31 +468,43 @@ static bool loggingEnabled = true; } } NSString * urlString = [[NSURL URLWithString:path relativeToURL:self.baseURL] absoluteString]; - + + // request with multipart form if(file != nil) { request = [self.requestSerializer multipartFormRequestWithMethod: @"POST" URLString: urlString parameters: nil constructingBodyWithBlock: ^(id formData) { - + for(NSString * key in params) { NSData* data = [params[key] dataUsingEncoding:NSUTF8StringEncoding]; [formData appendPartWithFormData: data name: key]; } - + [formData appendPartWithFileData: [file data] name: [file paramName] fileName: [file name] mimeType: [file mimeType]]; - + } error:nil]; } + // request with form parameters + else { + NSString* pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams]; + NSString* urlString = [[NSURL URLWithString:pathWithQueryParams relativeToURL:self.baseURL] absoluteString]; + + request = [self.requestSerializer requestWithMethod:method + URLString:urlString + parameters:params + error:nil]; + } + } else { NSString * pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams]; NSString * urlString = [[NSURL URLWithString:pathWithQueryParams relativeToURL:self.baseURL] absoluteString]; - + request = [self.requestSerializer requestWithMethod: method URLString: urlString parameters: body @@ -461,13 +525,11 @@ static bool loggingEnabled = true; NSLog(@"%@ cache disabled", path); [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; } - - - AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer]; + if(body != nil) { if([body isKindOfClass:[NSDictionary class]] || [body isKindOfClass:[NSArray class]]){ - [requestSerializer setValue:requestContentType forHTTPHeaderField:@"Content-Type"]; + [self.requestSerializer setValue:requestContentType forHTTPHeaderField:@"Content-Type"]; } else if ([body isKindOfClass:[SWGFile class]]){} else { @@ -479,12 +541,12 @@ static bool loggingEnabled = true; [request setValue:[headerParams valueForKey:key] forHTTPHeaderField:key]; } } - [requestSerializer setValue:responseContentType forHTTPHeaderField:@"Accept"]; + [self.requestSerializer setValue:responseContentType forHTTPHeaderField:@"Accept"]; + - // Always disable cookies! [request setHTTPShouldHandleCookies:NO]; - + NSNumber* requestId = [SWGApiClient queueRequest]; AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { @@ -502,15 +564,15 @@ static bool loggingEnabled = true; userInfo[SWGResponseObjectErrorKey] = operation.responseObject; } NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; - + if(self.logServerResponses) [self logResponse:nil forRequest:request error:augmentedError]; completionBlock(nil, augmentedError); } }]; - + [self.operationQueue addOperation:op]; return requestId; } -@end \ No newline at end of file +@end diff --git a/samples/client/petstore/objc/client/SWGPetApi.m b/samples/client/petstore/objc/client/SWGPetApi.m index fe3750119a..321e5cd2da 100644 --- a/samples/client/petstore/objc/client/SWGPetApi.m +++ b/samples/client/petstore/objc/client/SWGPetApi.m @@ -65,8 +65,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[@"application/json", @"application/xml", ]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -146,8 +149,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[@"application/json", @"application/xml", ]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -227,8 +233,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; if(status != nil) { @@ -245,7 +254,7 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; + NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; @@ -304,8 +313,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; if(tags != nil) { @@ -322,7 +334,7 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; + NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; @@ -382,8 +394,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]]; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -394,7 +409,7 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; + NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; @@ -457,8 +472,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]]; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[@"application/x-www-form-urlencoded", ]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -469,7 +487,7 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; + NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; @@ -533,8 +551,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]]; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -547,7 +568,7 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; + NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; @@ -596,8 +617,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]]; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[@"multipart/form-data", ]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -608,7 +632,7 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; + NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; @@ -624,8 +648,10 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; if(bodyDictionary == nil) { bodyDictionary = [[NSMutableArray alloc] init]; } - [bodyDictionary addObject:file]; - file.paramName = @"file"; + if(file != nil) { + [bodyDictionary addObject:file]; + file.paramName = @"file"; + } if(bodyDictionary == nil) { bodyDictionary = [[NSMutableArray alloc] init]; @@ -664,4 +690,4 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; -@end \ No newline at end of file +@end diff --git a/samples/client/petstore/objc/client/SWGStoreApi.m b/samples/client/petstore/objc/client/SWGStoreApi.m index 4380c8e10c..84df4f9bf1 100644 --- a/samples/client/petstore/objc/client/SWGStoreApi.m +++ b/samples/client/petstore/objc/client/SWGStoreApi.m @@ -63,8 +63,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -75,7 +78,7 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; + NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; @@ -127,8 +130,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -223,8 +229,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"orderId", @"}"]] withString: [SWGApiClient escape:orderId]]; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -235,7 +244,7 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; + NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; @@ -296,8 +305,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"orderId", @"}"]] withString: [SWGApiClient escape:orderId]]; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -308,7 +320,7 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; + NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; @@ -343,4 +355,4 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; -@end \ No newline at end of file +@end diff --git a/samples/client/petstore/objc/client/SWGUserApi.m b/samples/client/petstore/objc/client/SWGUserApi.m index 52338d0a3b..58f3291c2a 100644 --- a/samples/client/petstore/objc/client/SWGUserApi.m +++ b/samples/client/petstore/objc/client/SWGUserApi.m @@ -64,8 +64,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -145,8 +148,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -226,8 +232,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -308,8 +317,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; if(username != nil) { @@ -328,7 +340,7 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; + NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; @@ -385,8 +397,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -397,7 +412,7 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; + NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; @@ -444,8 +459,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"username", @"}"]] withString: [SWGApiClient escape:username]]; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -456,7 +474,7 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; + NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; @@ -518,8 +536,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"username", @"}"]] withString: [SWGApiClient escape:username]]; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -600,8 +621,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"username", @"}"]] withString: [SWGApiClient escape:username]]; - NSString* requestContentType = @"application/json"; - NSString* responseContentType = @"application/json"; + NSArray* requestContentTypes = @[]; + NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json"; + + NSArray* responseContentTypes = @[@"application/json", @"application/xml"]; + NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; @@ -612,7 +636,7 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; - NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; + NSMutableDictionary * formParams = [[NSMutableDictionary alloc]init]; @@ -647,4 +671,4 @@ static NSString * basePath = @"http://petstore.swagger.io/v2"; -@end \ No newline at end of file +@end