Skip to content

Commit 3a8b149

Browse files
committed
minor improvement to objc api client accept and content-type header
1 parent e068ad4 commit 3a8b149

File tree

8 files changed

+300
-126
lines changed

8 files changed

+300
-126
lines changed

modules/swagger-codegen/src/main/resources/objc/SWGApiClient.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ extern NSString *const SWGResponseObjectErrorKey;
4848

4949
+(void) configureCacheReachibilityForHost:(NSString*)host;
5050

51+
+(NSString *) selectHeaderAccept:(NSArray *)headerAcceptArray;
52+
+(NSString *) selectHeaderContentType:(NSArray *)headerContentTypeArray;
53+
5154
-(void)setHeaderValue:(NSString*) value
5255
forKey:(NSString*) forKey;
5356

modules/swagger-codegen/src/main/resources/objc/SWGApiClient.m

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,44 @@ +(SWGApiClient *)sharedClientFromPool:(NSString *)baseUrl {
7979
}
8080
}
8181

82+
/*
83+
* Detect `Accept` from acceptArray
84+
*/
85+
+ (NSString *) selectHeaderAccept:(NSArray *)acceptArray
86+
{
87+
// if acceptArray is nil or empty, return `application/json`
88+
if (acceptArray == nil || [acceptArray count] == 0) {
89+
return @"application/json";
90+
}
91+
92+
// if `application/json` in acceptArray, return it
93+
if ([acceptArray containsObject:[@"application/json" lowercaseString]]) {
94+
return @"application/json";
95+
}
96+
else {
97+
return [acceptArray componentsJoinedByString:@", "];
98+
}
99+
}
100+
101+
/*
102+
* Detect `Content-Type` from contentTypeArray
103+
*/
104+
+ (NSString *) selectHeaderContentType:(NSArray *)contentTypeArray
105+
{
106+
// if contentTypeArray is nil or empty, return `application/json`
107+
if (contentTypeArray == nil || [contentTypeArray count] == 0) {
108+
return @"application/json";
109+
}
110+
111+
// if `application/json` in contentTypeArray, return it
112+
if ([contentTypeArray containsObject:[@"application/json" lowercaseString]]) {
113+
return @"applications/json";
114+
}
115+
else {
116+
return contentTypeArray[0];
117+
}
118+
}
119+
82120
-(void)setHeaderValue:(NSString*) value
83121
forKey:(NSString*) forKey {
84122
[self.requestSerializer setValue:value forHTTPHeaderField:forKey];

modules/swagger-codegen/src/main/resources/objc/api-body.mustache

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,6 @@ static NSString * basePath = @"{{basePath}}";
7878
{{#pathParams}}[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"{{baseName}}", @"}"]] withString: [SWGApiClient escape:{{paramName}}]];
7979
{{/pathParams}}
8080

81-
NSArray* requestContentTypes = @[{{#consumes}}@"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}}];
82-
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
83-
84-
NSArray* responseContentTypes = @[{{#produces}}@"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}}];
85-
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
86-
8781
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
8882
{{#queryParams}}if({{paramName}} != nil) {
8983
{{#collectionFormat}}
@@ -97,6 +91,16 @@ static NSString * basePath = @"{{basePath}}";
9791
{{#headerParams}}if({{paramName}} != nil)
9892
headerParams[@"{{baseName}}"] = {{paramName}};
9993
{{/headerParams}}
94+
95+
// HTTP header `Accept`
96+
NSArray *headerAccept = @[{{#produces}}@"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}}];
97+
headerParams[@"Accept"] = [SWGApiClient selectHeaderAccept:headerAccept];
98+
NSString *responseContentType = [headerParams[@"Accept"] componentsSeparatedByString:@", "][0];
99+
100+
// HTTP header `Content-Type`
101+
NSArray *headerContentType = @[{{#consumes}}@"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}}];
102+
headerParams[@"Content-Type"] = [SWGApiClient selectHeaderContentType:headerContentType];
103+
NSString *requestContentType = headerParams[@"Content-Type"];
100104

101105
id bodyDictionary = nil;
102106
{{#bodyParam}}

samples/client/petstore/objc/client/SWGApiClient.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ extern NSString *const SWGResponseObjectErrorKey;
4848

4949
+(void) configureCacheReachibilityForHost:(NSString*)host;
5050

51+
+(NSString *) selectHeaderAccept:(NSArray *)headerAcceptArray;
52+
+(NSString *) selectHeaderContentType:(NSArray *)headerContentTypeArray;
53+
5154
-(void)setHeaderValue:(NSString*) value
5255
forKey:(NSString*) forKey;
5356

samples/client/petstore/objc/client/SWGApiClient.m

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,44 @@ +(SWGApiClient *)sharedClientFromPool:(NSString *)baseUrl {
7979
}
8080
}
8181

82+
/*
83+
* Detect `Accept` from acceptArray
84+
*/
85+
+ (NSString *) selectHeaderAccept:(NSArray *)acceptArray
86+
{
87+
// if acceptArray is nil or empty, return `application/json`
88+
if (acceptArray == nil || [acceptArray count] == 0) {
89+
return @"application/json";
90+
}
91+
92+
// if `application/json` in acceptArray, return it
93+
if ([acceptArray containsObject:[@"application/json" lowercaseString]]) {
94+
return @"application/json";
95+
}
96+
else {
97+
return [acceptArray componentsJoinedByString:@", "];
98+
}
99+
}
100+
101+
/*
102+
* Detect `Content-Type` from contentTypeArray
103+
*/
104+
+ (NSString *) selectHeaderContentType:(NSArray *)contentTypeArray
105+
{
106+
// if contentTypeArray is nil or empty, return `application/json`
107+
if (contentTypeArray == nil || [contentTypeArray count] == 0) {
108+
return @"application/json";
109+
}
110+
111+
// if `application/json` in contentTypeArray, return it
112+
if ([contentTypeArray containsObject:[@"application/json" lowercaseString]]) {
113+
return @"applications/json";
114+
}
115+
else {
116+
return contentTypeArray[0];
117+
}
118+
}
119+
82120
-(void)setHeaderValue:(NSString*) value
83121
forKey:(NSString*) forKey {
84122
[self.requestSerializer setValue:value forHTTPHeaderField:forKey];

samples/client/petstore/objc/client/SWGPetApi.m

Lines changed: 88 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,21 @@ -(NSNumber*) updatePetWithCompletionBlock: (SWGPet*) body
7575

7676

7777

78-
NSArray* requestContentTypes = @[@"application/json", @"application/xml", ];
79-
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
80-
81-
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
82-
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
83-
8478
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
8579

8680
NSMutableDictionary* headerParams = [NSMutableDictionary dictionaryWithDictionary:self.defaultHeaders];
8781

8882

83+
84+
// HTTP header `Accept`
85+
NSArray *headerAccept = @[@"application/json", @"application/xml"];
86+
headerParams[@"Accept"] = [SWGApiClient selectHeaderAccept:headerAccept];
87+
NSString *responseContentType = [headerParams[@"Accept"] componentsSeparatedByString:@", "][0];
88+
89+
// HTTP header `Content-Type`
90+
NSArray *headerContentType = @[@"application/json", @"application/xml", ];
91+
headerParams[@"Content-Type"] = [SWGApiClient selectHeaderContentType:headerContentType];
92+
NSString *requestContentType = headerParams[@"Content-Type"];
8993

9094
id bodyDictionary = nil;
9195

@@ -166,17 +170,21 @@ -(NSNumber*) addPetWithCompletionBlock: (SWGPet*) body
166170

167171

168172

169-
NSArray* requestContentTypes = @[@"application/json", @"application/xml", ];
170-
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
171-
172-
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
173-
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
174-
175173
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
176174

177175
NSMutableDictionary* headerParams = [NSMutableDictionary dictionaryWithDictionary:self.defaultHeaders];
178176

179177

178+
179+
// HTTP header `Accept`
180+
NSArray *headerAccept = @[@"application/json", @"application/xml"];
181+
headerParams[@"Accept"] = [SWGApiClient selectHeaderAccept:headerAccept];
182+
NSString *responseContentType = [headerParams[@"Accept"] componentsSeparatedByString:@", "][0];
183+
184+
// HTTP header `Content-Type`
185+
NSArray *headerContentType = @[@"application/json", @"application/xml", ];
186+
headerParams[@"Content-Type"] = [SWGApiClient selectHeaderContentType:headerContentType];
187+
NSString *requestContentType = headerParams[@"Content-Type"];
180188

181189
id bodyDictionary = nil;
182190

@@ -257,12 +265,6 @@ -(NSNumber*) findPetsByStatusWithCompletionBlock: (NSArray*) status
257265

258266

259267

260-
NSArray* requestContentTypes = @[];
261-
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
262-
263-
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
264-
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
265-
266268
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
267269
if(status != nil) {
268270

@@ -274,6 +276,16 @@ -(NSNumber*) findPetsByStatusWithCompletionBlock: (NSArray*) status
274276
NSMutableDictionary* headerParams = [NSMutableDictionary dictionaryWithDictionary:self.defaultHeaders];
275277

276278

279+
280+
// HTTP header `Accept`
281+
NSArray *headerAccept = @[@"application/json", @"application/xml"];
282+
headerParams[@"Accept"] = [SWGApiClient selectHeaderAccept:headerAccept];
283+
NSString *responseContentType = [headerParams[@"Accept"] componentsSeparatedByString:@", "][0];
284+
285+
// HTTP header `Content-Type`
286+
NSArray *headerContentType = @[];
287+
headerParams[@"Content-Type"] = [SWGApiClient selectHeaderContentType:headerContentType];
288+
NSString *requestContentType = headerParams[@"Content-Type"];
277289

278290
id bodyDictionary = nil;
279291

@@ -344,12 +356,6 @@ -(NSNumber*) findPetsByTagsWithCompletionBlock: (NSArray*) tags
344356

345357

346358

347-
NSArray* requestContentTypes = @[];
348-
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
349-
350-
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
351-
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
352-
353359
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
354360
if(tags != nil) {
355361

@@ -361,6 +367,16 @@ -(NSNumber*) findPetsByTagsWithCompletionBlock: (NSArray*) tags
361367
NSMutableDictionary* headerParams = [NSMutableDictionary dictionaryWithDictionary:self.defaultHeaders];
362368

363369

370+
371+
// HTTP header `Accept`
372+
NSArray *headerAccept = @[@"application/json", @"application/xml"];
373+
headerParams[@"Accept"] = [SWGApiClient selectHeaderAccept:headerAccept];
374+
NSString *responseContentType = [headerParams[@"Accept"] componentsSeparatedByString:@", "][0];
375+
376+
// HTTP header `Content-Type`
377+
NSArray *headerContentType = @[];
378+
headerParams[@"Content-Type"] = [SWGApiClient selectHeaderContentType:headerContentType];
379+
NSString *requestContentType = headerParams[@"Content-Type"];
364380

365381
id bodyDictionary = nil;
366382

@@ -432,17 +448,29 @@ -(NSNumber*) getPetByIdWithCompletionBlock: (NSNumber*) petId
432448
[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]];
433449

434450

435-
NSArray* requestContentTypes = @[];
436-
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
437-
438-
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
439-
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
440-
441451
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
442452

443453
NSMutableDictionary* headerParams = [NSMutableDictionary dictionaryWithDictionary:self.defaultHeaders];
444454

445455

456+
457+
// HTTP header `Accept`
458+
NSArray *headerAccept = @[];
459+
headerParams[@"Accept"] = [SWGApiClient selectHeaderAccept:headerAccept];
460+
NSString *responseContentType = [headerParams[@"Accept"] componentsSeparatedByString:@", "][0];
461+
462+
// HTTP header `Content-Type`
463+
NSArray *headerContentType = @[];
464+
headerParams[@"Content-Type"] = [SWGApiClient selectHeaderContentType:headerContentType];
465+
NSString *requestContentType = headerParams[@"Content-Type"];
466+
467+
NSLog(@"--------------------------------");
468+
NSLog(@"request type: %@", requestContentType);
469+
NSLog(@"response type: %@", responseContentType);
470+
NSLog(@"headers: %@", headerParams);
471+
NSLog(@"--------------------------------");
472+
473+
446474

447475
id bodyDictionary = nil;
448476

@@ -519,17 +547,21 @@ -(NSNumber*) updatePetWithFormWithCompletionBlock: (NSString*) petId
519547
[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]];
520548

521549

522-
NSArray* requestContentTypes = @[@"application/x-www-form-urlencoded", ];
523-
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
524-
525-
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
526-
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
527-
528550
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
529551

530552
NSMutableDictionary* headerParams = [NSMutableDictionary dictionaryWithDictionary:self.defaultHeaders];
531553

532554

555+
556+
// HTTP header `Accept`
557+
NSArray *headerAccept = @[@"application/json", @"application/xml"];
558+
headerParams[@"Accept"] = [SWGApiClient selectHeaderAccept:headerAccept];
559+
NSString *responseContentType = [headerParams[@"Accept"] componentsSeparatedByString:@", "][0];
560+
561+
// HTTP header `Content-Type`
562+
NSArray *headerContentType = @[@"application/x-www-form-urlencoded", ];
563+
headerParams[@"Content-Type"] = [SWGApiClient selectHeaderContentType:headerContentType];
564+
NSString *requestContentType = headerParams[@"Content-Type"];
533565

534566
id bodyDictionary = nil;
535567

@@ -606,19 +638,23 @@ -(NSNumber*) deletePetWithCompletionBlock: (NSString*) apiKey
606638
[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]];
607639

608640

609-
NSArray* requestContentTypes = @[];
610-
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
611-
612-
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
613-
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
614-
615641
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
616642

617643
NSMutableDictionary* headerParams = [NSMutableDictionary dictionaryWithDictionary:self.defaultHeaders];
618644

619645
if(apiKey != nil)
620646
headerParams[@"api_key"] = apiKey;
621647

648+
649+
// HTTP header `Accept`
650+
NSArray *headerAccept = @[@"application/json", @"application/xml"];
651+
headerParams[@"Accept"] = [SWGApiClient selectHeaderAccept:headerAccept];
652+
NSString *responseContentType = [headerParams[@"Accept"] componentsSeparatedByString:@", "][0];
653+
654+
// HTTP header `Content-Type`
655+
NSArray *headerContentType = @[];
656+
headerParams[@"Content-Type"] = [SWGApiClient selectHeaderContentType:headerContentType];
657+
NSString *requestContentType = headerParams[@"Content-Type"];
622658

623659
id bodyDictionary = nil;
624660

@@ -681,17 +717,21 @@ -(NSNumber*) uploadFileWithCompletionBlock: (NSNumber*) petId
681717
[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]];
682718

683719

684-
NSArray* requestContentTypes = @[@"multipart/form-data", ];
685-
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
686-
687-
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
688-
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
689-
690720
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
691721

692722
NSMutableDictionary* headerParams = [NSMutableDictionary dictionaryWithDictionary:self.defaultHeaders];
693723

694724

725+
726+
// HTTP header `Accept`
727+
NSArray *headerAccept = @[@"application/json", @"application/xml"];
728+
headerParams[@"Accept"] = [SWGApiClient selectHeaderAccept:headerAccept];
729+
NSString *responseContentType = [headerParams[@"Accept"] componentsSeparatedByString:@", "][0];
730+
731+
// HTTP header `Content-Type`
732+
NSArray *headerContentType = @[@"multipart/form-data", ];
733+
headerParams[@"Content-Type"] = [SWGApiClient selectHeaderContentType:headerContentType];
734+
NSString *requestContentType = headerParams[@"Content-Type"];
695735

696736
id bodyDictionary = nil;
697737

0 commit comments

Comments
 (0)