Skip to content

Commit 82436c5

Browse files
committed
Merge pull request #780 from geekerzp/develop_2.0_objc_contenttype
Minor improvement to Objc API Client 'accept' and 'content-type' header
2 parents 156afa7 + 2514f3a commit 82436c5

File tree

11 files changed

+552
-141
lines changed

11 files changed

+552
-141
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 *)accepts;
52+
+(NSString *) selectHeaderContentType:(NSArray *)contentTypes;
53+
5154
-(void)setHeaderValue:(NSString*) value
5255
forKey:(NSString*) forKey;
5356

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

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

82+
/*
83+
* Detect `Accept` from accepts
84+
*/
85+
+ (NSString *) selectHeaderAccept:(NSArray *)accepts
86+
{
87+
if (accepts == nil || [accepts count] == 0) {
88+
return @"";
89+
}
90+
91+
NSMutableArray *lowerAccepts = [[NSMutableArray alloc] initWithCapacity:[accepts count]];
92+
[accepts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
93+
[lowerAccepts addObject:[obj lowercaseString]];
94+
}];
95+
96+
97+
if ([lowerAccepts containsObject:@"application/json"]) {
98+
return @"application/json";
99+
}
100+
else {
101+
return [lowerAccepts componentsJoinedByString:@", "];
102+
}
103+
}
104+
105+
/*
106+
* Detect `Content-Type` from contentTypes
107+
*/
108+
+ (NSString *) selectHeaderContentType:(NSArray *)contentTypes
109+
{
110+
if (contentTypes == nil || [contentTypes count] == 0) {
111+
return @"application/json";
112+
}
113+
114+
NSMutableArray *lowerContentTypes = [[NSMutableArray alloc] initWithCapacity:[contentTypes count]];
115+
[contentTypes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
116+
[lowerContentTypes addObject:[obj lowercaseString]];
117+
}];
118+
119+
if ([lowerContentTypes containsObject:@"application/json"]) {
120+
return @"application/json";
121+
}
122+
else {
123+
return lowerContentTypes[0];
124+
}
125+
}
126+
82127
-(void)setHeaderValue:(NSString*) value
83128
forKey:(NSString*) forKey {
84129
[self.requestSerializer setValue:value forHTTPHeaderField:forKey];

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

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

86-
NSArray* requestContentTypes = @[{{#consumes}}@"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}}];
87-
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
88-
89-
NSArray* responseContentTypes = @[{{#produces}}@"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}}];
90-
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
91-
9286
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
9387
{{#queryParams}}if({{paramName}} != nil) {
9488
{{#collectionFormat}}
@@ -102,6 +96,24 @@ static NSString * basePath = @"{{basePath}}";
10296
{{#headerParams}}if({{paramName}} != nil)
10397
headerParams[@"{{baseName}}"] = {{paramName}};
10498
{{/headerParams}}
99+
100+
// HTTP header `Accept`
101+
headerParams[@"Accept"] = [SWGApiClient selectHeaderAccept:@[{{#produces}}@"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}}]];
102+
if ([headerParams[@"Accept"] length] == 0) {
103+
[headerParams removeObjectForKey:@"Accept"];
104+
}
105+
106+
// response content type
107+
NSString *responseContentType;
108+
if ([headerParams objectForKey:@"Accept"]) {
109+
responseContentType = [headerParams[@"Accept"] componentsSeparatedByString:@", "][0];
110+
}
111+
else {
112+
responseContentType = @"";
113+
}
114+
115+
// request content type
116+
NSString *requestContentType = [SWGApiClient selectHeaderContentType:@[{{#consumes}}@"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}}]];
105117

106118
id bodyDictionary = nil;
107119
{{#bodyParam}}

samples/client/petstore/objc/PetstoreClient/PetstoreClient.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
BA525648922D4C0E9F44D4F1 /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 73DA4F1067C343C3962F1542 /* libPods.a */; };
11+
CF31D0991B105E4B00509935 /* SWGApiClientTest.m in Sources */ = {isa = PBXBuildFile; fileRef = CF31D0981B105E4B00509935 /* SWGApiClientTest.m */; };
1112
CFD1B6701B05EC7D00DCCD51 /* JSONValueTransformer+ISO8601.m in Sources */ = {isa = PBXBuildFile; fileRef = CFD1B66F1B05EC7D00DCCD51 /* JSONValueTransformer+ISO8601.m */; };
1213
CFD1B6711B05EC7D00DCCD51 /* JSONValueTransformer+ISO8601.m in Sources */ = {isa = PBXBuildFile; fileRef = CFD1B66F1B05EC7D00DCCD51 /* JSONValueTransformer+ISO8601.m */; };
1314
EA66999A1811D2FA00A70D03 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EA6699991811D2FA00A70D03 /* Foundation.framework */; };
@@ -55,6 +56,7 @@
5556
/* Begin PBXFileReference section */
5657
73DA4F1067C343C3962F1542 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
5758
A425648B5C0A4849C7668069 /* Pods.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.release.xcconfig; path = "../Pods/Target Support Files/Pods/Pods.release.xcconfig"; sourceTree = "<group>"; };
59+
CF31D0981B105E4B00509935 /* SWGApiClientTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SWGApiClientTest.m; sourceTree = "<group>"; };
5860
CFD1B66E1B05EC7D00DCCD51 /* JSONValueTransformer+ISO8601.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "JSONValueTransformer+ISO8601.h"; sourceTree = "<group>"; };
5961
CFD1B66F1B05EC7D00DCCD51 /* JSONValueTransformer+ISO8601.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "JSONValueTransformer+ISO8601.m"; sourceTree = "<group>"; };
6062
E2B6DA00BE52336E23783686 /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "../Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = "<group>"; };
@@ -207,6 +209,7 @@
207209
isa = PBXGroup;
208210
children = (
209211
EA8CD3EB1AC274BE00C47D0B /* PetApiTest.h */,
212+
CF31D0981B105E4B00509935 /* SWGApiClientTest.m */,
210213
EA6699C71811D2FB00A70D03 /* PetApiTest.m */,
211214
EA6699C21811D2FB00A70D03 /* Supporting Files */,
212215
);
@@ -417,6 +420,7 @@
417420
EAB26B0C1AC8DF78002F5C7A /* PetApiTest.h in Sources */,
418421
CFD1B6711B05EC7D00DCCD51 /* JSONValueTransformer+ISO8601.m in Sources */,
419422
EAB26B0D1AC8DF78002F5C7A /* PetApiTest.m in Sources */,
423+
CF31D0991B105E4B00509935 /* SWGApiClientTest.m in Sources */,
420424
);
421425
runOnlyForDeploymentPostprocessing = 0;
422426
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#import <UIKit/UIKit.h>
2+
#import <XCTest/XCTest.h>
3+
#import "SWGApiClient.h"
4+
5+
@interface SWGApiClientTest : XCTestCase
6+
7+
@end
8+
9+
@implementation SWGApiClientTest
10+
11+
- (void)setUp {
12+
[super setUp];
13+
// Put setup code here. This method is called before the invocation of each test method in the class.
14+
}
15+
16+
- (void)tearDown {
17+
// Put teardown code here. This method is called after the invocation of each test method in the class.
18+
[super tearDown];
19+
}
20+
21+
- (void)testSelectHeaderAccept {
22+
NSArray *accepts = nil;
23+
24+
accepts = @[@"APPLICATION/JSON", @"APPLICATION/XML"];
25+
XCTAssertEqualObjects([SWGApiClient selectHeaderAccept:accepts], @"application/json");
26+
27+
accepts = @[@"application/json", @"application/xml"];
28+
XCTAssertEqualObjects([SWGApiClient selectHeaderAccept:accepts], @"application/json");
29+
30+
accepts = @[@"APPLICATION/xml", @"APPLICATION/json"];
31+
XCTAssertEqualObjects([SWGApiClient selectHeaderAccept:accepts], @"application/json");
32+
33+
accepts = @[@"text/plain", @"application/xml"];
34+
XCTAssertEqualObjects([SWGApiClient selectHeaderAccept:accepts], @"text/plain, application/xml");
35+
36+
accepts = @[];
37+
XCTAssertEqualObjects([SWGApiClient selectHeaderAccept:accepts], @"");
38+
}
39+
40+
- (void)testSelectHeaderContentType {
41+
NSArray *contentTypes = nil;
42+
43+
contentTypes = @[@"APPLICATION/JSON", @"APPLICATION/XML"];
44+
XCTAssertEqualObjects([SWGApiClient selectHeaderContentType:contentTypes], @"application/json");
45+
46+
contentTypes = @[@"application/json", @"application/xml"];
47+
XCTAssertEqualObjects([SWGApiClient selectHeaderContentType:contentTypes], @"application/json");
48+
49+
contentTypes = @[@"APPLICATION/xml", @"APPLICATION/json"];
50+
XCTAssertEqualObjects([SWGApiClient selectHeaderContentType:contentTypes], @"application/json");
51+
52+
contentTypes = @[@"text/plain", @"application/xml"];
53+
XCTAssertEqualObjects([SWGApiClient selectHeaderContentType:contentTypes], @"text/plain");
54+
55+
contentTypes = @[];
56+
XCTAssertEqualObjects([SWGApiClient selectHeaderContentType:contentTypes], @"application/json");
57+
}
58+
59+
@end
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
PODS:
2-
- AFNetworking (2.5.3):
3-
- AFNetworking/NSURLConnection (= 2.5.3)
4-
- AFNetworking/NSURLSession (= 2.5.3)
5-
- AFNetworking/Reachability (= 2.5.3)
6-
- AFNetworking/Security (= 2.5.3)
7-
- AFNetworking/Serialization (= 2.5.3)
8-
- AFNetworking/UIKit (= 2.5.3)
9-
- AFNetworking/NSURLConnection (2.5.3):
2+
- AFNetworking (2.5.4):
3+
- AFNetworking/NSURLConnection (= 2.5.4)
4+
- AFNetworking/NSURLSession (= 2.5.4)
5+
- AFNetworking/Reachability (= 2.5.4)
6+
- AFNetworking/Security (= 2.5.4)
7+
- AFNetworking/Serialization (= 2.5.4)
8+
- AFNetworking/UIKit (= 2.5.4)
9+
- AFNetworking/NSURLConnection (2.5.4):
1010
- AFNetworking/Reachability
1111
- AFNetworking/Security
1212
- AFNetworking/Serialization
13-
- AFNetworking/NSURLSession (2.5.3):
13+
- AFNetworking/NSURLSession (2.5.4):
1414
- AFNetworking/Reachability
1515
- AFNetworking/Security
1616
- AFNetworking/Serialization
17-
- AFNetworking/Reachability (2.5.3)
18-
- AFNetworking/Security (2.5.3)
19-
- AFNetworking/Serialization (2.5.3)
20-
- AFNetworking/UIKit (2.5.3):
17+
- AFNetworking/Reachability (2.5.4)
18+
- AFNetworking/Security (2.5.4)
19+
- AFNetworking/Serialization (2.5.4)
20+
- AFNetworking/UIKit (2.5.4):
2121
- AFNetworking/NSURLConnection
2222
- AFNetworking/NSURLSession
2323
- ISO8601 (0.2.0)
@@ -29,8 +29,8 @@ DEPENDENCIES:
2929
- JSONModel (~> 1.0)
3030

3131
SPEC CHECKSUMS:
32-
AFNetworking: e1d86c2a96bb5d2e7408da36149806706ee122fe
32+
AFNetworking: 05edc0ac4c4c8cf57bcf4b84be5b0744b6d8e71e
3333
ISO8601: 962282de75074c38bbfaa7b133b0e743ed6deb8d
3434
JSONModel: ec77e9865236a7a09d9cf7668df6b4b328d9ec1d
3535

36-
COCOAPODS: 0.36.0
36+
COCOAPODS: 0.37.1

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 *)accepts;
52+
+(NSString *) selectHeaderContentType:(NSArray *)contentTypes;
53+
5154
-(void)setHeaderValue:(NSString*) value
5255
forKey:(NSString*) forKey;
5356

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

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

82+
/*
83+
* Detect `Accept` from accepts
84+
*/
85+
+ (NSString *) selectHeaderAccept:(NSArray *)accepts
86+
{
87+
if (accepts == nil || [accepts count] == 0) {
88+
return @"";
89+
}
90+
91+
NSMutableArray *lowerAccepts = [[NSMutableArray alloc] initWithCapacity:[accepts count]];
92+
[accepts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
93+
[lowerAccepts addObject:[obj lowercaseString]];
94+
}];
95+
96+
97+
if ([lowerAccepts containsObject:@"application/json"]) {
98+
return @"application/json";
99+
}
100+
else {
101+
return [lowerAccepts componentsJoinedByString:@", "];
102+
}
103+
}
104+
105+
/*
106+
* Detect `Content-Type` from contentTypes
107+
*/
108+
+ (NSString *) selectHeaderContentType:(NSArray *)contentTypes
109+
{
110+
if (contentTypes == nil || [contentTypes count] == 0) {
111+
return @"application/json";
112+
}
113+
114+
NSMutableArray *lowerContentTypes = [[NSMutableArray alloc] initWithCapacity:[contentTypes count]];
115+
[contentTypes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
116+
[lowerContentTypes addObject:[obj lowercaseString]];
117+
}];
118+
119+
if ([lowerContentTypes containsObject:@"application/json"]) {
120+
return @"application/json";
121+
}
122+
else {
123+
return lowerContentTypes[0];
124+
}
125+
}
126+
82127
-(void)setHeaderValue:(NSString*) value
83128
forKey:(NSString*) forKey {
84129
[self.requestSerializer setValue:value forHTTPHeaderField:forKey];

0 commit comments

Comments
 (0)