added queryParamCollection

This commit is contained in:
Tony Tam 2015-03-27 22:17:50 -07:00
parent 236a1147b7
commit c9a2a14da5
13 changed files with 194 additions and 99 deletions

View File

@ -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

View File

@ -102,6 +102,8 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
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"));

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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 = "<group>"; };
EA6699C51811D2FB00A70D03 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
EA6699C71811D2FB00A70D03 /* PetApiTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PetApiTest.m; sourceTree = "<group>"; };
EA8B8AA21AC6683700638FBB /* SWGQueryParamCollection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SWGQueryParamCollection.h; sourceTree = "<group>"; };
EA8B8AA31AC6683700638FBB /* SWGQueryParamCollection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SWGQueryParamCollection.m; sourceTree = "<group>"; };
EA8CD3EB1AC274BE00C47D0B /* PetApiTest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PetApiTest.h; sourceTree = "<group>"; };
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 = "<group>"; };
@ -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;

View File

@ -6,96 +6,45 @@
- (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* pet = [self createPet];
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"];
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)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;
}
if(!hasTag)
XCTFail(@"failed to find tag in pet");
NSArray* tags = [[NSArray alloc] initWithObjects:@"tony", nil];
[api findPetsByTagsWithCompletionBlock:tags completionHandler:^(NSArray *output, NSError *error) {
if(error){
XCTFail(@"got error %@", error);
}
}
[expectation fulfill];
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");
}
}
[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

View File

@ -1,5 +1,6 @@
#import "SWGApiClient.h"
#import "SWGFile.h"
#import "SWGQueryParamCollection.h"
@implementation SWGApiClient
@ -208,14 +209,42 @@ static bool loggingEnabled = true;
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;
}
}

View File

@ -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 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
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 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
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];

View File

@ -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

View File

@ -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

View File

@ -1,5 +1,6 @@
#import "SWGStoreApi.h"
#import "SWGFile.h"
#import "SWGQueryParamCollection.h"
#import "SWGApiClient.h"
#import "SWGOrder.h"

View File

@ -1,5 +1,6 @@
#import "SWGUserApi.h"
#import "SWGFile.h"
#import "SWGQueryParamCollection.h"
#import "SWGApiClient.h"
#import "SWGUser.h"
@ -311,10 +312,14 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
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];