From 236a1147b7b1cbb9f506b26b955c4603091d15dd Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Fri, 27 Mar 2015 09:53:40 -0700 Subject: [PATCH 1/3] minor comment --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 4f01fcc8687..29d0ec0b8d9 100644 --- a/pom.xml +++ b/pom.xml @@ -277,8 +277,8 @@ + - java-client From c9a2a14da51679ba2d9bf0625a4cc11606af98fe Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Fri, 27 Mar 2015 22:17:50 -0700 Subject: [PATCH 2/3] added queryParamCollection --- bin/objc-petstore.sh | 2 +- .../codegen/languages/ObjcClientCodegen.java | 2 + .../resources/objc/SWGQueryParamCollection.h | 9 + .../resources/objc/SWGQueryParamCollection.m | 16 ++ .../src/main/resources/objc/api-body.mustache | 9 +- .../PetstoreClient.xcodeproj/project.pbxproj | 6 + .../PetstoreClientTests/PetApiTest.m | 160 ++++++++---------- .../petstore/objc/client/SWGApiClient.m | 35 +++- .../client/petstore/objc/client/SWGPetApi.m | 17 +- .../objc/client/SWGQueryParamCollection.h | 9 + .../objc/client/SWGQueryParamCollection.m | 16 ++ .../client/petstore/objc/client/SWGStoreApi.m | 1 + .../client/petstore/objc/client/SWGUserApi.m | 9 +- 13 files changed, 193 insertions(+), 98 deletions(-) create mode 100644 modules/swagger-codegen/src/main/resources/objc/SWGQueryParamCollection.h create mode 100644 modules/swagger-codegen/src/main/resources/objc/SWGQueryParamCollection.m create mode 100644 samples/client/petstore/objc/client/SWGQueryParamCollection.h create mode 100644 samples/client/petstore/objc/client/SWGQueryParamCollection.m diff --git a/bin/objc-petstore.sh b/bin/objc-petstore.sh index feefc99a28c..513b7535786 100755 --- a/bin/objc-petstore.sh +++ b/bin/objc-petstore.sh @@ -26,6 +26,6 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l objc -o samples/client/petstore/objc" +ags="$@ generate -t modules/swagger-codegen/src/main/resources/objc -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l objc -o samples/client/petstore/objc" java -DappName=PetstoreClient $JAVA_OPTS -jar $executable $ags diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java index 2e91cafa992..76d9e419359 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java @@ -102,6 +102,8 @@ public ObjcClientCodegen() { supportingFiles.add(new SupportingFile("SWGObject.h", sourceFolder, "SWGObject.h")); supportingFiles.add(new SupportingFile("SWGObject.m", sourceFolder, "SWGObject.m")); + supportingFiles.add(new SupportingFile("SWGQueryParamCollection.h", sourceFolder, "SWGQueryParamCollection.h")); + supportingFiles.add(new SupportingFile("SWGQueryParamCollection.m", sourceFolder, "SWGQueryParamCollection.m")); supportingFiles.add(new SupportingFile("SWGApiClient.h", sourceFolder, "SWGApiClient.h")); supportingFiles.add(new SupportingFile("SWGApiClient.m", sourceFolder, "SWGApiClient.m")); supportingFiles.add(new SupportingFile("SWGFile.h", sourceFolder, "SWGFile.h")); diff --git a/modules/swagger-codegen/src/main/resources/objc/SWGQueryParamCollection.h b/modules/swagger-codegen/src/main/resources/objc/SWGQueryParamCollection.h new file mode 100644 index 00000000000..26b830875f0 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/objc/SWGQueryParamCollection.h @@ -0,0 +1,9 @@ +@interface SWGQueryParamCollection : NSObject + +@property(nonatomic, readonly) NSArray* values; +@property(nonatomic, readonly) NSString* format; + +- (id) initWithValuesAndFormat: (NSArray*) values + format: (NSString*) format; + +@end diff --git a/modules/swagger-codegen/src/main/resources/objc/SWGQueryParamCollection.m b/modules/swagger-codegen/src/main/resources/objc/SWGQueryParamCollection.m new file mode 100644 index 00000000000..9ce319940dd --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/objc/SWGQueryParamCollection.m @@ -0,0 +1,16 @@ +#import "SWGQueryParamCollection.h" + +@implementation SWGQueryParamCollection + +@synthesize values = _values; +@synthesize format = _format; + +- (id) initWithValuesAndFormat: (NSArray*) values + format: (NSString*) format { + _values = values; + _format = format; + + return self; +} + +@end \ No newline at end of file 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 ffead406a86..e079df918ac 100644 --- a/modules/swagger-codegen/src/main/resources/objc/api-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/api-body.mustache @@ -1,6 +1,7 @@ {{#operations}} #import "{{classname}}.h" #import "SWGFile.h" +#import "SWGQueryParamCollection.h" #import "SWGApiClient.h" {{#imports}}#import "{{import}}.h" {{/imports}} @@ -71,8 +72,12 @@ static NSString * basePath = @"{{basePath}}"; NSString* responseContentType = @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; - {{#queryParams}}if({{paramName}} != nil) - queryParams[@"{{baseName}}"] = {{paramName}}; + {{#queryParams}}if({{paramName}} != nil) { + {{#collectionFormat}} + queryParams[@"{{baseName}}"] = [[SWGQueryParamCollection alloc] initWithValuesAndFormat: {{baseName}} format: @"{{collectionFormat}}"]; + {{/collectionFormat}} + {{^collectionFormat}}queryParams[@"{{baseName}}"] = {{paramName}};{{/collectionFormat}} + } {{/queryParams}} NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; {{#headerParams}}if({{paramName}} != nil) diff --git a/samples/client/petstore/objc/PetstoreClient/PetstoreClient.xcodeproj/project.pbxproj b/samples/client/petstore/objc/PetstoreClient/PetstoreClient.xcodeproj/project.pbxproj index 66a244ebf3a..bf04a276caa 100644 --- a/samples/client/petstore/objc/PetstoreClient/PetstoreClient.xcodeproj/project.pbxproj +++ b/samples/client/petstore/objc/PetstoreClient/PetstoreClient.xcodeproj/project.pbxproj @@ -23,6 +23,7 @@ EA6699BE1811D2FB00A70D03 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EA66999D1811D2FA00A70D03 /* UIKit.framework */; }; EA6699C61811D2FB00A70D03 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = EA6699C41811D2FB00A70D03 /* InfoPlist.strings */; }; EA6699C81811D2FB00A70D03 /* PetApiTest.m in Sources */ = {isa = PBXBuildFile; fileRef = EA6699C71811D2FB00A70D03 /* PetApiTest.m */; }; + EA8B8AA41AC6683700638FBB /* SWGQueryParamCollection.m in Sources */ = {isa = PBXBuildFile; fileRef = EA8B8AA31AC6683700638FBB /* SWGQueryParamCollection.m */; }; EA8CD3ED1AC2763600C47D0B /* SenTestingKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EA8CD3EC1AC2763600C47D0B /* SenTestingKit.framework */; }; EAEA85E41811D3AE00F06E69 /* SWGApiClient.m in Sources */ = {isa = PBXBuildFile; fileRef = EAEA85CD1811D3AE00F06E69 /* SWGApiClient.m */; }; EAEA85E51811D3AE00F06E69 /* SWGCategory.m in Sources */ = {isa = PBXBuildFile; fileRef = EAEA85CF1811D3AE00F06E69 /* SWGCategory.m */; }; @@ -75,6 +76,8 @@ EA6699C31811D2FB00A70D03 /* PetstoreClientTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "PetstoreClientTests-Info.plist"; sourceTree = ""; }; EA6699C51811D2FB00A70D03 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; EA6699C71811D2FB00A70D03 /* PetApiTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PetApiTest.m; sourceTree = ""; }; + EA8B8AA21AC6683700638FBB /* SWGQueryParamCollection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SWGQueryParamCollection.h; sourceTree = ""; }; + EA8B8AA31AC6683700638FBB /* SWGQueryParamCollection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SWGQueryParamCollection.m; sourceTree = ""; }; EA8CD3EB1AC274BE00C47D0B /* PetApiTest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PetApiTest.h; sourceTree = ""; }; EA8CD3EC1AC2763600C47D0B /* SenTestingKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SenTestingKit.framework; path = Library/Frameworks/SenTestingKit.framework; sourceTree = DEVELOPER_DIR; }; EAEA85CC1811D3AE00F06E69 /* SWGApiClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SWGApiClient.h; sourceTree = ""; }; @@ -227,6 +230,8 @@ EAEA85CB1811D3AE00F06E69 /* client */ = { isa = PBXGroup; children = ( + EA8B8AA21AC6683700638FBB /* SWGQueryParamCollection.h */, + EA8B8AA31AC6683700638FBB /* SWGQueryParamCollection.m */, EAFBEABC1A92C42700A27431 /* SWGApiResponse.h */, EAFBEABD1A92C42700A27431 /* SWGApiResponse.m */, EAEA85CC1811D3AE00F06E69 /* SWGApiClient.h */, @@ -407,6 +412,7 @@ EAEA85EC1811D3AE00F06E69 /* SWGStoreApi.m in Sources */, EAEA85E91811D3AE00F06E69 /* SWGOrder.m in Sources */, EAEA85E81811D3AE00F06E69 /* SWGObject.m in Sources */, + EA8B8AA41AC6683700638FBB /* SWGQueryParamCollection.m in Sources */, EAEA85E71811D3AE00F06E69 /* SWGFile.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/samples/client/petstore/objc/PetstoreClient/PetstoreClientTests/PetApiTest.m b/samples/client/petstore/objc/PetstoreClient/PetstoreClientTests/PetApiTest.m index f21e8fc5162..8c5d597748c 100644 --- a/samples/client/petstore/objc/PetstoreClient/PetstoreClientTests/PetApiTest.m +++ b/samples/client/petstore/objc/PetstoreClient/PetstoreClientTests/PetApiTest.m @@ -6,96 +6,45 @@ @implementation PetApiTest - (void)setUp { [super setUp]; api = [[SWGPetApi alloc ]init]; - [SWGPetApi setBasePath:@"http://localhost:8080/api"]; } - (void)tearDown { [super tearDown]; } -- (void)testGetPetById { +- (void)testCreateAndGetPet { XCTestExpectation *expectation = [self expectationWithDescription:@"testGetPetById"]; - [api getPetByIdWithCompletionBlock:@1 completionHandler:^(SWGPet *output, NSError *error) { - if(error){ - XCTFail(@"got error %@", error); - } - if(output){ - XCTAssertNotNil([output _id], @"token was nil"); - } - [expectation fulfill]; - }]; - [self waitForExpectationsWithTimeout:2.0 handler:nil]; -} -- (void) testAddPet { - XCTestExpectation *expectation = [self expectationWithDescription:@"testAddPet"]; - - SWGPet * petToAdd = [[SWGPet alloc] init]; - [petToAdd set_id:@1000]; - NSMutableArray* tags = [[NSMutableArray alloc] init]; - for(int i = 0; i < 5; i++){ - SWGTag * tag = [[SWGTag alloc] init]; - [tag set_id:[NSNumber numberWithInt:i]]; - [tag setName:[NSString stringWithFormat:@"tag-%d", i]]; - [tags addObject:tag]; - } - [petToAdd setTags:tags]; - [petToAdd setStatus:@"lost"]; - - SWGCategory * category = [[SWGCategory alloc] init]; - [category setName:@"sold"]; - [petToAdd setCategory:category]; - [petToAdd setName:@"dragon"]; + SWGPet* pet = [self createPet]; - NSMutableArray* photos = [[NSMutableArray alloc] init]; - for(int i = 0; i < 10; i++){ - NSString * url = [NSString stringWithFormat:@"http://foo.com/photo/%d", i]; - [photos addObject:url]; - } - [petToAdd setPhotoUrls:photos]; - - [api addPetWithCompletionBlock:petToAdd completionHandler:^(NSError *error) { + [api addPetWithCompletionBlock:pet completionHandler:^(NSError *error) { if(error){ XCTFail(@"got error %@", error); } - [expectation fulfill]; + NSLog(@"%@", [pet _id]); + [api getPetByIdWithCompletionBlock:[pet _id] completionHandler:^(SWGPet *output, NSError *error) { + if(error){ + XCTFail(@"got error %@", error); + } + if(output){ + XCTAssertNotNil([output _id], @"token was nil"); + } + [expectation fulfill]; + }]; }]; - [self waitForExpectationsWithTimeout:2.0 handler:nil]; } - (void) testUpdatePet { XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdatePet"]; - SWGPet * petToAdd = [[SWGPet alloc] init]; - [petToAdd set_id:[NSNumber numberWithInt:1000]]; - NSMutableArray* tags = [[NSMutableArray alloc] init]; - for(int i = 0; i < 5; i++){ - SWGTag * tag = [[SWGTag alloc] init]; - [tag set_id:[NSNumber numberWithInt:i]]; - [tag setName:[NSString stringWithFormat:@"tag-%d", i]]; - [tags addObject:tag]; - } - [petToAdd setTags:tags]; - [petToAdd setStatus:@"lost"]; + SWGPet* pet = [self createPet]; - SWGCategory * category = [[SWGCategory alloc] init]; - [category setName:@"sold"]; - [petToAdd setCategory:category]; - [petToAdd setName:@"dragon"]; - - NSMutableArray* photos = [[NSMutableArray alloc] init]; - for(int i = 0; i < 10; i++){ - NSString * url = [NSString stringWithFormat:@"http://foo.com/photo/%d", i]; - [photos addObject:url]; - } - [petToAdd setPhotoUrls:photos]; - - [api addPetWithCompletionBlock:petToAdd completionHandler:^(NSError *error) { + [api addPetWithCompletionBlock:pet completionHandler:^(NSError *error) { if(error) { XCTFail(@"got error %@", error); } else { - [api getPetByIdWithCompletionBlock:[NSString stringWithFormat:@"%@",[petToAdd _id]] completionHandler:^(SWGPet *output, NSError *error) { + [api getPetByIdWithCompletionBlock:[NSString stringWithFormat:@"%@",[pet _id]] completionHandler:^(SWGPet *output, NSError *error) { if(error) { XCTFail(@"got error %@", error); } @@ -140,39 +89,78 @@ - (void) testUpdatePet { - (void)testGetPetByStatus { XCTestExpectation *expectation = [self expectationWithDescription:@"testGetPetByStatus"]; - [api findPetsByStatusWithCompletionBlock:@"available" completionHandler:^(NSArray *output, NSError *error) { + SWGPet* pet = [self createPet]; + + [api addPetWithCompletionBlock:pet completionHandler:^(NSError *error) { if(error) { XCTFail(@"got error %@", error); } - if(output == nil){ - XCTFail(@"failed to fetch pets"); - } - else { - [expectation fulfill]; - } + NSArray* status = [[NSArray alloc] initWithObjects:@"available", nil]; + [api findPetsByStatusWithCompletionBlock:status completionHandler:^(NSArray *output, NSError *error) { + if(error) { + XCTFail(@"got error %@", error); + } + if(output == nil){ + XCTFail(@"failed to fetch pets"); + } + else { + for(SWGPet* fetched in output) { + if([pet _id] == [fetched _id]) { + [expectation fulfill]; + } + } + } + }]; }]; [self waitForExpectationsWithTimeout:2.0 handler:nil]; } - (void)testGetPetByTags { XCTestExpectation *expectation = [self expectationWithDescription:@"testGetPetByTags"]; - [api findPetsByTagsWithCompletionBlock:@"tag1,tag2" completionHandler:^(NSArray *output, NSError *error) { - if(error){ + SWGPet* pet = [self createPet]; + SWGTag* tag = [[SWGTag alloc] init]; + tag.name = @"tony"; + pet.tags = [[NSArray alloc] initWithObjects:tag, nil]; + + [api addPetWithCompletionBlock:pet completionHandler:^(NSError *error) { + if(error) { XCTFail(@"got error %@", error); } - if(output){ - for(SWGPet * pet in output) { - bool hasTag = false; - for(SWGTag * tag in [pet tags]){ - if([[tag name] isEqualToString:@"tag1"] || [[tag name] isEqualToString:@"tag2"]) - hasTag = true; + NSArray* tags = [[NSArray alloc] initWithObjects:@"tony", nil]; + + [api findPetsByTagsWithCompletionBlock:tags completionHandler:^(NSArray *output, NSError *error) { + if(error){ + XCTFail(@"got error %@", error); + } + if(output){ + for(SWGPet * fetched in output) { + bool hasTag = false; + for(SWGTag * tag in [fetched tags]){ + if(fetched._id == pet._id && [[tag name] isEqualToString:@"tony"]) + hasTag = true; + } + if(!hasTag) + XCTFail(@"failed to find tag in pet"); } - if(!hasTag) - XCTFail(@"failed to find tag in pet"); } - } - [expectation fulfill]; + [expectation fulfill]; + }]; }]; [self waitForExpectationsWithTimeout:2.0 handler:nil]; } + +- (SWGPet*) createPet { + SWGPet * pet = [[SWGPet alloc] init]; + pet._id = [[NSNumber alloc] initWithLong:[[NSDate date] timeIntervalSince1970]]; + pet.name = @"monkey"; + SWGCategory * category = [[SWGCategory alloc] init]; + category.name = @"super-happy"; + + pet.category = category; + pet.status = @"available"; + + NSArray * photos = [[NSArray alloc] initWithObjects:@"http://foo.bar.com/3", @"http://foo.bar.com/4", nil]; + pet.photoUrls = photos; + return pet; +} @end \ No newline at end of file diff --git a/samples/client/petstore/objc/client/SWGApiClient.m b/samples/client/petstore/objc/client/SWGApiClient.m index ff454970cf0..36af6ea44c0 100644 --- a/samples/client/petstore/objc/client/SWGApiClient.m +++ b/samples/client/petstore/objc/client/SWGApiClient.m @@ -1,5 +1,6 @@ #import "SWGApiClient.h" #import "SWGFile.h" +#import "SWGQueryParamCollection.h" @implementation SWGApiClient @@ -208,14 +209,42 @@ -(NSString*) pathWithQueryParamsToString:(NSString*) path if(counter == 0) separator = @"?"; else separator = @"&"; NSString * value; - if([[queryParams valueForKey:key] isKindOfClass:[NSString class]]){ + id queryParamValue = [queryParams valueForKey:key]; + if([queryParamValue isKindOfClass:[NSString class]]){ value = [SWGApiClient escape:[queryParams valueForKey:key]]; + [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, + [SWGApiClient escape:key], value]]; + } + else if ([queryParamValue isKindOfClass:[SWGQueryParamCollection class]]) { + SWGQueryParamCollection* c = (SWGQueryParamCollection* ) queryParamValue; + NSString * format = [c format]; + NSArray * values = [c values]; + if([format isEqualToString:@"csv"]) { + [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, + [SWGApiClient escape:key], [values componentsJoinedByString:@","]]]; + } + else if([format isEqualToString:@"tsv"]) { + [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, + [SWGApiClient escape:key], [values componentsJoinedByString:@"\t"]]]; + } + else if ([format isEqualToString:@"pipes"]) { + [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, + [SWGApiClient escape:key], [values componentsJoinedByString:@"|"]]]; + } + else if([format isEqualToString:@"multi"]) { + for(id obj in values) { + + [requestUrl appendString: [NSString stringWithFormat:@"%@%@=%@", separator, [SWGApiClient escape:key], obj]]; + if(counter > 0) separator = @"&"; + } + } } else { value = [NSString stringWithFormat:@"%@", [queryParams valueForKey:key]]; + [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, + [SWGApiClient escape:key], value]]; } - [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, - [SWGApiClient escape:key], value]]; + counter += 1; } } diff --git a/samples/client/petstore/objc/client/SWGPetApi.m b/samples/client/petstore/objc/client/SWGPetApi.m index 5d134bb1f27..a52077eeab3 100644 --- a/samples/client/petstore/objc/client/SWGPetApi.m +++ b/samples/client/petstore/objc/client/SWGPetApi.m @@ -1,5 +1,6 @@ #import "SWGPetApi.h" #import "SWGFile.h" +#import "SWGQueryParamCollection.h" #import "SWGApiClient.h" #import "SWGPet.h" #import "SWGFile.h" @@ -230,8 +231,12 @@ -(NSNumber*) findPetsByStatusWithCompletionBlock: (NSArray*) status NSString* responseContentType = @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; - if(status != nil) - queryParams[@"status"] = status; + if(status != nil) { + + queryParams[@"status"] = [[SWGQueryParamCollection alloc] initWithValuesAndFormat: status format: @"multi"]; + + + } NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; @@ -303,8 +308,12 @@ -(NSNumber*) findPetsByTagsWithCompletionBlock: (NSArray*) tags NSString* responseContentType = @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; - if(tags != nil) - queryParams[@"tags"] = tags; + if(tags != nil) { + + queryParams[@"tags"] = [[SWGQueryParamCollection alloc] initWithValuesAndFormat: tags format: @"multi"]; + + + } NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; diff --git a/samples/client/petstore/objc/client/SWGQueryParamCollection.h b/samples/client/petstore/objc/client/SWGQueryParamCollection.h new file mode 100644 index 00000000000..26b830875f0 --- /dev/null +++ b/samples/client/petstore/objc/client/SWGQueryParamCollection.h @@ -0,0 +1,9 @@ +@interface SWGQueryParamCollection : NSObject + +@property(nonatomic, readonly) NSArray* values; +@property(nonatomic, readonly) NSString* format; + +- (id) initWithValuesAndFormat: (NSArray*) values + format: (NSString*) format; + +@end diff --git a/samples/client/petstore/objc/client/SWGQueryParamCollection.m b/samples/client/petstore/objc/client/SWGQueryParamCollection.m new file mode 100644 index 00000000000..9ce319940dd --- /dev/null +++ b/samples/client/petstore/objc/client/SWGQueryParamCollection.m @@ -0,0 +1,16 @@ +#import "SWGQueryParamCollection.h" + +@implementation SWGQueryParamCollection + +@synthesize values = _values; +@synthesize format = _format; + +- (id) initWithValuesAndFormat: (NSArray*) values + format: (NSString*) format { + _values = values; + _format = format; + + return self; +} + +@end \ No newline at end of file diff --git a/samples/client/petstore/objc/client/SWGStoreApi.m b/samples/client/petstore/objc/client/SWGStoreApi.m index 3eb015ebc0a..65d47519438 100644 --- a/samples/client/petstore/objc/client/SWGStoreApi.m +++ b/samples/client/petstore/objc/client/SWGStoreApi.m @@ -1,5 +1,6 @@ #import "SWGStoreApi.h" #import "SWGFile.h" +#import "SWGQueryParamCollection.h" #import "SWGApiClient.h" #import "SWGOrder.h" diff --git a/samples/client/petstore/objc/client/SWGUserApi.m b/samples/client/petstore/objc/client/SWGUserApi.m index 6e33862d111..85037247af7 100644 --- a/samples/client/petstore/objc/client/SWGUserApi.m +++ b/samples/client/petstore/objc/client/SWGUserApi.m @@ -1,5 +1,6 @@ #import "SWGUserApi.h" #import "SWGFile.h" +#import "SWGQueryParamCollection.h" #import "SWGApiClient.h" #import "SWGUser.h" @@ -311,10 +312,14 @@ -(NSNumber*) loginUserWithCompletionBlock: (NSString*) username NSString* responseContentType = @"application/json"; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; - if(username != nil) + if(username != nil) { + queryParams[@"username"] = username; - if(password != nil) + } + if(password != nil) { + queryParams[@"password"] = password; + } NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; From 99271e77ab66624fd6ba05d11374e34864ec4f25 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Fri, 27 Mar 2015 22:55:06 -0700 Subject: [PATCH 3/3] added multi-param args --- .../src/main/resources/objc/SWGApiClient.m | 42 +++++++++++++++-- .../main/resources/objc/api-header.mustache | 7 +-- .../PetstoreClientTests/PetApiTest.m | 46 ++++++++++++++++--- .../petstore/objc/client/SWGApiClient.m | 37 ++++++++------- .../client/petstore/objc/client/SWGPetApi.h | 29 ------------ .../client/petstore/objc/client/SWGStoreApi.h | 11 ----- .../client/petstore/objc/client/SWGUserApi.h | 25 ---------- 7 files changed, 98 insertions(+), 99 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/objc/SWGApiClient.m b/modules/swagger-codegen/src/main/resources/objc/SWGApiClient.m index ff454970cf0..f84d7b3f62d 100644 --- a/modules/swagger-codegen/src/main/resources/objc/SWGApiClient.m +++ b/modules/swagger-codegen/src/main/resources/objc/SWGApiClient.m @@ -1,5 +1,6 @@ #import "SWGApiClient.h" #import "SWGFile.h" +#import "SWGQueryParamCollection.h" @implementation SWGApiClient @@ -208,14 +209,45 @@ -(NSString*) pathWithQueryParamsToString:(NSString*) path if(counter == 0) separator = @"?"; else separator = @"&"; NSString * value; - if([[queryParams valueForKey:key] isKindOfClass:[NSString class]]){ - value = [SWGApiClient escape:[queryParams valueForKey:key]]; + id queryParam = [queryParams valueForKey:key]; + if([queryParam isKindOfClass:[NSString class]]){ + [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, + [SWGApiClient escape:key], [SWGApiClient escape:[queryParams valueForKey:key]]]]; + } + else if([queryParam isKindOfClass:[SWGQueryParamCollection class]]){ + 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:@","]]]]; + + } + else if([format isEqualToString:@"tsv"]) { + [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, + [SWGApiClient escape:key], [NSString stringWithFormat:@"%@", [values componentsJoinedByString:@"\t"]]]]; + + } + else if([format isEqualToString:@"pipes"]) { + [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, + [SWGApiClient escape:key], [NSString stringWithFormat:@"%@", [values componentsJoinedByString:@"|"]]]]; + + } + else if([format isEqualToString:@"multi"]) { + for(id obj in values) { + [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, + [SWGApiClient escape:key], [NSString stringWithFormat:@"%@", obj]]]; + counter += 1; + } + + } } else { - value = [NSString stringWithFormat:@"%@", [queryParams valueForKey:key]]; + [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, + [SWGApiClient escape:key], [NSString stringWithFormat:@"%@", [queryParams valueForKey:key]]]]; } - [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, - [SWGApiClient escape:key], value]]; + counter += 1; } } diff --git a/modules/swagger-codegen/src/main/resources/objc/api-header.mustache b/modules/swagger-codegen/src/main/resources/objc/api-header.mustache index c42c02d5aff..c5ef04f18ae 100644 --- a/modules/swagger-codegen/src/main/resources/objc/api-header.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/api-header.mustache @@ -16,12 +16,9 @@ /** {{{summary}}} - {{#notes}} - {{{notes}}} - {{/notes}} + {{#notes}}{{{notes}}}{{/notes}} - {{#allParams}} - @param {{paramName}} {{description}} + {{#allParams}}@param {{paramName}} {{description}} {{/allParams}} return type: {{returnType}} diff --git a/samples/client/petstore/objc/PetstoreClient/PetstoreClientTests/PetApiTest.m b/samples/client/petstore/objc/PetstoreClient/PetstoreClientTests/PetApiTest.m index 8c5d597748c..d9ae3017105 100644 --- a/samples/client/petstore/objc/PetstoreClient/PetstoreClientTests/PetApiTest.m +++ b/samples/client/petstore/objc/PetstoreClient/PetstoreClientTests/PetApiTest.m @@ -32,7 +32,7 @@ - (void)testCreateAndGetPet { [expectation fulfill]; }]; }]; - [self waitForExpectationsWithTimeout:2.0 handler:nil]; + [self waitForExpectationsWithTimeout:10.0 handler:nil]; } - (void) testUpdatePet { @@ -84,7 +84,7 @@ - (void) testUpdatePet { }]; } }]; - [self waitForExpectationsWithTimeout:2.0 handler:nil]; + [self waitForExpectationsWithTimeout:10.0 handler:nil]; } - (void)testGetPetByStatus { @@ -104,15 +104,18 @@ - (void)testGetPetByStatus { XCTFail(@"failed to fetch pets"); } else { + bool found = false; for(SWGPet* fetched in output) { if([pet _id] == [fetched _id]) { - [expectation fulfill]; + found = true; } } + if(found) + [expectation fulfill]; } }]; }]; - [self waitForExpectationsWithTimeout:2.0 handler:nil]; + [self waitForExpectationsWithTimeout:10.0 handler:nil]; } - (void)testGetPetByTags { @@ -133,8 +136,8 @@ - (void)testGetPetByTags { XCTFail(@"got error %@", error); } if(output){ + bool hasTag = false; for(SWGPet * fetched in output) { - bool hasTag = false; for(SWGTag * tag in [fetched tags]){ if(fetched._id == pet._id && [[tag name] isEqualToString:@"tony"]) hasTag = true; @@ -142,11 +145,40 @@ - (void)testGetPetByTags { if(!hasTag) XCTFail(@"failed to find tag in pet"); } + if(hasTag) + [expectation fulfill]; } - [expectation fulfill]; }]; }]; - [self waitForExpectationsWithTimeout:2.0 handler:nil]; + [self waitForExpectationsWithTimeout:10.0 handler:nil]; +} + +- (void)testDeletePet { + XCTestExpectation *expectation = [self expectationWithDescription:@"testGetPetById"]; + + SWGPet* pet = [self createPet]; + + [api addPetWithCompletionBlock:pet completionHandler:^(NSError *error) { + if(error){ + XCTFail(@"got error %@", error); + } + [api deletePetWithCompletionBlock:@"" petId:[NSString stringWithFormat:@"%@", [pet _id]] completionHandler:^(NSError *error) { + if(error){ + XCTFail(@"got error %@", error); + } + [api getPetByIdWithCompletionBlock:[pet _id] completionHandler:^(SWGPet *output, NSError *error) { + if(error) { + // good + [expectation fulfill]; + + } + else { + XCTFail(@"expected a failure"); + } + }]; + }]; + }]; + [self waitForExpectationsWithTimeout:10.0 handler:nil]; } - (SWGPet*) createPet { diff --git a/samples/client/petstore/objc/client/SWGApiClient.m b/samples/client/petstore/objc/client/SWGApiClient.m index 36af6ea44c0..f84d7b3f62d 100644 --- a/samples/client/petstore/objc/client/SWGApiClient.m +++ b/samples/client/petstore/objc/client/SWGApiClient.m @@ -209,40 +209,43 @@ -(NSString*) pathWithQueryParamsToString:(NSString*) path if(counter == 0) separator = @"?"; else separator = @"&"; NSString * value; - id queryParamValue = [queryParams valueForKey:key]; - if([queryParamValue isKindOfClass:[NSString class]]){ - value = [SWGApiClient escape:[queryParams valueForKey:key]]; + id queryParam = [queryParams valueForKey:key]; + if([queryParam isKindOfClass:[NSString class]]){ [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, - [SWGApiClient escape:key], value]]; + [SWGApiClient escape:key], [SWGApiClient escape:[queryParams valueForKey:key]]]]; } - else if ([queryParamValue isKindOfClass:[SWGQueryParamCollection class]]) { - SWGQueryParamCollection* c = (SWGQueryParamCollection* ) queryParamValue; - NSString * format = [c format]; - NSArray * values = [c values]; + else if([queryParam isKindOfClass:[SWGQueryParamCollection class]]){ + SWGQueryParamCollection * coll = (SWGQueryParamCollection*) queryParam; + NSArray* values = [coll values]; + NSString* format = [coll format]; + if([format isEqualToString:@"csv"]) { [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, - [SWGApiClient escape:key], [values componentsJoinedByString:@","]]]; + [SWGApiClient escape:key], [NSString stringWithFormat:@"%@", [values componentsJoinedByString:@","]]]]; + } else if([format isEqualToString:@"tsv"]) { [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, - [SWGApiClient escape:key], [values componentsJoinedByString:@"\t"]]]; + [SWGApiClient escape:key], [NSString stringWithFormat:@"%@", [values componentsJoinedByString:@"\t"]]]]; + } - else if ([format isEqualToString:@"pipes"]) { + else if([format isEqualToString:@"pipes"]) { [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, - [SWGApiClient escape:key], [values componentsJoinedByString:@"|"]]]; + [SWGApiClient escape:key], [NSString stringWithFormat:@"%@", [values componentsJoinedByString:@"|"]]]]; + } else if([format isEqualToString:@"multi"]) { for(id obj in values) { - - [requestUrl appendString: [NSString stringWithFormat:@"%@%@=%@", separator, [SWGApiClient escape:key], obj]]; - if(counter > 0) separator = @"&"; + [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, + [SWGApiClient escape:key], [NSString stringWithFormat:@"%@", obj]]]; + counter += 1; } + } } else { - value = [NSString stringWithFormat:@"%@", [queryParams valueForKey:key]]; [requestUrl appendString:[NSString stringWithFormat:@"%@%@=%@", separator, - [SWGApiClient escape:key], value]]; + [SWGApiClient escape:key], [NSString stringWithFormat:@"%@", [queryParams valueForKey:key]]]]; } counter += 1; diff --git a/samples/client/petstore/objc/client/SWGPetApi.h b/samples/client/petstore/objc/client/SWGPetApi.h index b40a210ea4c..e444cea8f4b 100644 --- a/samples/client/petstore/objc/client/SWGPetApi.h +++ b/samples/client/petstore/objc/client/SWGPetApi.h @@ -15,10 +15,7 @@ Update an existing pet - - - @param body Pet object that needs to be added to the store @@ -34,10 +31,7 @@ Add a new pet to the store - - - @param body Pet object that needs to be added to the store @@ -52,11 +46,8 @@ /** Finds Pets by status - Multiple status values can be provided with comma seperated strings - - @param status Status values that need to be considered for filter @@ -71,11 +62,8 @@ /** Finds Pets by tags - Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing. - - @param tags Tags to filter by @@ -90,11 +78,8 @@ /** Find pet by ID - Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions - - @param petId ID of pet that needs to be fetched @@ -110,14 +95,9 @@ Updates a pet in the store with form data - - - @param petId ID of pet that needs to be updated - @param name Updated name of the pet - @param status Updated status of the pet @@ -135,12 +115,8 @@ Deletes a pet - - - @param api_key - @param petId Pet id to delete @@ -157,14 +133,9 @@ uploads an image - - - @param petId ID of pet to update - @param additionalMetadata Additional data to pass to server - @param file file to upload diff --git a/samples/client/petstore/objc/client/SWGStoreApi.h b/samples/client/petstore/objc/client/SWGStoreApi.h index 2daa1e8656f..b4b8e5af5dc 100644 --- a/samples/client/petstore/objc/client/SWGStoreApi.h +++ b/samples/client/petstore/objc/client/SWGStoreApi.h @@ -13,9 +13,7 @@ /** Returns pet inventories by status - Returns a map of status codes to quantities - @@ -30,10 +28,7 @@ Place an order for a pet - - - @param body order placed for purchasing the pet @@ -48,11 +43,8 @@ /** Find purchase order by ID - For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions - - @param orderId ID of pet that needs to be fetched @@ -67,11 +59,8 @@ /** Delete purchase order by ID - For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors - - @param orderId ID of the order that needs to be deleted diff --git a/samples/client/petstore/objc/client/SWGUserApi.h b/samples/client/petstore/objc/client/SWGUserApi.h index 14d08208f83..d110c02776e 100644 --- a/samples/client/petstore/objc/client/SWGUserApi.h +++ b/samples/client/petstore/objc/client/SWGUserApi.h @@ -13,11 +13,8 @@ /** Create user - This can only be done by the logged in user. - - @param body Created user object @@ -33,10 +30,7 @@ Creates list of users with given input array - - - @param body List of user object @@ -52,10 +46,7 @@ Creates list of users with given input array - - - @param body List of user object @@ -71,12 +62,8 @@ Logs user into the system - - - @param username The user name for login - @param password The password for login in clear text @@ -93,8 +80,6 @@ Logs out current logged in user session - - @@ -109,10 +94,7 @@ Get user by user name - - - @param username The name that needs to be fetched. Use user1 for testing. @@ -127,13 +109,9 @@ /** Updated user - This can only be done by the logged in user. - - @param username name that need to be deleted - @param body Updated user object @@ -149,11 +127,8 @@ /** Delete user - This can only be done by the logged in user. - - @param username The name that needs to be deleted