Skip to content

Commit f9ad16e

Browse files
committed
Update objc client.
* Add test cases for deserialization. * Pretty print model infomation.
1 parent 37dbd6e commit f9ad16e

File tree

11 files changed

+222
-81
lines changed

11 files changed

+222
-81
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#import "{{classname}}.h"
44

55
@implementation {{classname}}
6-
6+
77
+ (JSONKeyMapper *)keyMapper
88
{
99
return [[JSONKeyMapper alloc] initWithDictionary:@{ {{#vars}}@"{{baseName}}": @"{{name}}"{{#hasMore}}, {{/hasMore}}{{/vars}} }];
@@ -21,6 +21,10 @@
2121
}
2222
}
2323

24+
- (NSString *)description {
25+
return [[self toDictionary] description];
26+
}
27+
2428
{{/model}}
2529
@end
2630
{{/models}}

samples/client/petstore/objc/SwaggerClient/SWGCategory.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#import "SWGCategory.h"
22

33
@implementation SWGCategory
4-
4+
55
+ (JSONKeyMapper *)keyMapper
66
{
77
return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id": @"_id", @"name": @"name" }];
@@ -19,4 +19,8 @@ + (BOOL)propertyIsOptional:(NSString *)propertyName
1919
}
2020
}
2121

22+
- (NSString *)description {
23+
return [[self toDictionary] description];
24+
}
25+
2226
@end

samples/client/petstore/objc/SwaggerClient/SWGOrder.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#import "SWGOrder.h"
22

33
@implementation SWGOrder
4-
4+
55
+ (JSONKeyMapper *)keyMapper
66
{
77
return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id": @"_id", @"petId": @"petId", @"quantity": @"quantity", @"shipDate": @"shipDate", @"status": @"status", @"complete": @"complete" }];
@@ -19,4 +19,8 @@ + (BOOL)propertyIsOptional:(NSString *)propertyName
1919
}
2020
}
2121

22+
- (NSString *)description {
23+
return [[self toDictionary] description];
24+
}
25+
2226
@end

samples/client/petstore/objc/SwaggerClient/SWGPet.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#import "SWGPet.h"
22

33
@implementation SWGPet
4-
4+
55
+ (JSONKeyMapper *)keyMapper
66
{
77
return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id": @"_id", @"category": @"category", @"name": @"name", @"photoUrls": @"photoUrls", @"tags": @"tags", @"status": @"status" }];
@@ -19,4 +19,8 @@ + (BOOL)propertyIsOptional:(NSString *)propertyName
1919
}
2020
}
2121

22+
- (NSString *)description {
23+
return [[self toDictionary] description];
24+
}
25+
2226
@end

samples/client/petstore/objc/SwaggerClient/SWGTag.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#import "SWGTag.h"
22

33
@implementation SWGTag
4-
4+
55
+ (JSONKeyMapper *)keyMapper
66
{
77
return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id": @"_id", @"name": @"name" }];
@@ -19,4 +19,8 @@ + (BOOL)propertyIsOptional:(NSString *)propertyName
1919
}
2020
}
2121

22+
- (NSString *)description {
23+
return [[self toDictionary] description];
24+
}
25+
2226
@end

samples/client/petstore/objc/SwaggerClient/SWGUser.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#import "SWGUser.h"
22

33
@implementation SWGUser
4-
4+
55
+ (JSONKeyMapper *)keyMapper
66
{
77
return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id": @"_id", @"username": @"username", @"firstName": @"firstName", @"lastName": @"lastName", @"email": @"email", @"password": @"password", @"phone": @"phone", @"userStatus": @"userStatus" }];
@@ -19,4 +19,8 @@ + (BOOL)propertyIsOptional:(NSString *)propertyName
1919
}
2020
}
2121

22+
- (NSString *)description {
23+
return [[self toDictionary] description];
24+
}
25+
2226
@end

samples/client/petstore/objc/SwaggerClientTests/SwaggerClient.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
6003F5BC195388D20070C39A /* Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6003F5BB195388D20070C39A /* Tests.m */; };
2424
873B8AEB1B1F5CCA007FD442 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 873B8AEA1B1F5CCA007FD442 /* Main.storyboard */; };
2525
94BE6BE84795B5034A811E61 /* libPods-SwaggerClient_Example.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8D46325ECAD48245C07F6733 /* libPods-SwaggerClient_Example.a */; };
26+
CF0ADB481B5F95D6008A2729 /* PetTest.m in Sources */ = {isa = PBXBuildFile; fileRef = CF0ADB471B5F95D6008A2729 /* PetTest.m */; };
27+
CF8F71391B5F73AC00162980 /* DeserializationTest.m in Sources */ = {isa = PBXBuildFile; fileRef = CF8F71381B5F73AC00162980 /* DeserializationTest.m */; };
2628
CFDFB4121B3CFFA8009739C5 /* UserApiTest.m in Sources */ = {isa = PBXBuildFile; fileRef = CFDFB40D1B3CFEC3009739C5 /* UserApiTest.m */; };
2729
CFDFB4131B3CFFDD009739C5 /* PetApiTest.m in Sources */ = {isa = PBXBuildFile; fileRef = CFDFB40A1B3CFEC3009739C5 /* PetApiTest.m */; };
2830
CFDFB4141B3CFFF6009739C5 /* StoreApiTest.m in Sources */ = {isa = PBXBuildFile; fileRef = CFDFB40B1B3CFEC3009739C5 /* StoreApiTest.m */; };
@@ -65,6 +67,8 @@
6567
873B8AEA1B1F5CCA007FD442 /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = "<group>"; };
6668
8D46325ECAD48245C07F6733 /* libPods-SwaggerClient_Example.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-SwaggerClient_Example.a"; sourceTree = BUILT_PRODUCTS_DIR; };
6769
BFB4BE760737508B3CFC23B2 /* Pods-SwaggerClient_Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SwaggerClient_Example.release.xcconfig"; path = "Pods/Target Support Files/Pods-SwaggerClient_Example/Pods-SwaggerClient_Example.release.xcconfig"; sourceTree = "<group>"; };
70+
CF0ADB471B5F95D6008A2729 /* PetTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PetTest.m; sourceTree = "<group>"; };
71+
CF8F71381B5F73AC00162980 /* DeserializationTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DeserializationTest.m; sourceTree = "<group>"; };
6872
CFDFB40A1B3CFEC3009739C5 /* PetApiTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PetApiTest.m; sourceTree = "<group>"; };
6973
CFDFB40B1B3CFEC3009739C5 /* StoreApiTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = StoreApiTest.m; sourceTree = "<group>"; };
7074
CFDFB40C1B3CFEC3009739C5 /* SWGApiClientTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SWGApiClientTest.m; sourceTree = "<group>"; };
@@ -166,6 +170,8 @@
166170
CFDFB40A1B3CFEC3009739C5 /* PetApiTest.m */,
167171
CFDFB40B1B3CFEC3009739C5 /* StoreApiTest.m */,
168172
CFDFB40C1B3CFEC3009739C5 /* SWGApiClientTest.m */,
173+
CF0ADB471B5F95D6008A2729 /* PetTest.m */,
174+
CF8F71381B5F73AC00162980 /* DeserializationTest.m */,
169175
CFDFB40D1B3CFEC3009739C5 /* UserApiTest.m */,
170176
6003F5BB195388D20070C39A /* Tests.m */,
171177
6003F5B6195388D20070C39A /* Supporting Files */,
@@ -374,10 +380,12 @@
374380
buildActionMask = 2147483647;
375381
files = (
376382
CFDFB4141B3CFFF6009739C5 /* StoreApiTest.m in Sources */,
383+
CF0ADB481B5F95D6008A2729 /* PetTest.m in Sources */,
377384
CFDFB4131B3CFFDD009739C5 /* PetApiTest.m in Sources */,
378385
6003F5BC195388D20070C39A /* Tests.m in Sources */,
379386
CFDFB4151B3D000B009739C5 /* SWGApiClientTest.m in Sources */,
380387
CFDFB4121B3CFFA8009739C5 /* UserApiTest.m in Sources */,
388+
CF8F71391B5F73AC00162980 /* DeserializationTest.m in Sources */,
381389
);
382390
runOnlyForDeploymentPostprocessing = 0;
383391
};

samples/client/petstore/objc/SwaggerClientTests/SwaggerClient/SWGViewController.m

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import "SWGViewController.h"
10+
#import <SwaggerClient/SWGPet.h>
1011

1112
@interface SWGViewController ()
1213

@@ -17,13 +18,38 @@ @implementation SWGViewController
1718
- (void)viewDidLoad
1819
{
1920
[super viewDidLoad];
20-
// Do any additional setup after loading the view, typically from a nib.
21+
SWGPet *pet = [self createPet];
22+
NSLog(@"%@", pet);
2123
}
2224

2325
- (void)didReceiveMemoryWarning
2426
{
2527
[super didReceiveMemoryWarning];
26-
// Dispose of any resources that can be recreated.
28+
}
29+
30+
- (SWGPet*) createPet {
31+
SWGPet * pet = [[SWGPet alloc] init];
32+
pet._id = [[NSNumber alloc] initWithLong:[[NSDate date] timeIntervalSince1970]];
33+
pet.name = @"monkey";
34+
35+
SWGCategory * category = [[SWGCategory alloc] init];
36+
category._id = [[NSNumber alloc] initWithInteger:arc4random_uniform(100000)];
37+
category.name = @"super-happy";
38+
pet.category = category;
39+
40+
SWGTag *tag1 = [[SWGTag alloc] init];
41+
tag1._id = [[NSNumber alloc] initWithInteger:arc4random_uniform(100000)];
42+
tag1.name = @"test tag 1";
43+
SWGTag *tag2 = [[SWGTag alloc] init];
44+
tag2._id = [[NSNumber alloc] initWithInteger:arc4random_uniform(100000)];
45+
tag2.name = @"test tag 2";
46+
pet.tags = (NSArray<SWGTag> *)[[NSArray alloc] initWithObjects:tag1, tag2, nil];
47+
48+
pet.status = @"available";
49+
50+
NSArray * photos = [[NSArray alloc] initWithObjects:@"http://foo.bar.com/3", @"http://foo.bar.com/4", nil];
51+
pet.photoUrls = photos;
52+
return pet;
2753
}
2854

2955
@end
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#import <UIKit/UIKit.h>
2+
#import <XCTest/XCTest.h>
3+
#import <SwaggerClient/SWGApiClient.h>
4+
#import <SwaggerClient/SWGPet.h>
5+
6+
@interface DeserializationTest : XCTestCase {
7+
8+
@private SWGApiClient *apiClient;
9+
10+
}
11+
12+
@end
13+
14+
@implementation DeserializationTest
15+
16+
- (void)setUp {
17+
[super setUp];
18+
apiClient = [[SWGApiClient alloc] init];
19+
}
20+
21+
- (void)testDeserializeDate {
22+
NSString *dateStr = @"2012-09-27";
23+
24+
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
25+
NSTimeZone *timezone = [NSTimeZone timeZoneWithName:@"UTC"];
26+
[formatter setTimeZone:timezone];
27+
[formatter setDateFormat:@"yyyy-MM-dd"];
28+
NSDate *date = [formatter dateFromString:dateStr];
29+
30+
NSDate *deserializedDate = [apiClient deserialize:dateStr class:@"NSDate*"];
31+
32+
XCTAssertEqualWithAccuracy([date timeIntervalSinceReferenceDate], [deserializedDate timeIntervalSinceReferenceDate], 0.001);
33+
}
34+
35+
- (void)testDeserializeDateTime {
36+
NSString *dateTimeStr = @"1997-07-16T19:20:30+00:00";
37+
38+
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
39+
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
40+
NSDate *dateTime = [formatter dateFromString:dateTimeStr];
41+
42+
NSDate *deserializedDateTime = [apiClient deserialize:dateTimeStr class:@"NSDate*"];
43+
44+
XCTAssertEqualWithAccuracy([dateTime timeIntervalSinceReferenceDate], [deserializedDateTime timeIntervalSinceReferenceDate], 0.001);
45+
}
46+
47+
- (void)testDeserializeObject {
48+
XCTAssertTrue([[apiClient deserialize:@"" class:@"NSObject*"] isKindOfClass:[NSObject class]]);
49+
}
50+
51+
- (void)testDeserializeString {
52+
NSString *data = @"test string";
53+
NSString *result = [apiClient deserialize:data class:@"NSString*"];
54+
55+
XCTAssertTrue([result isEqualToString:data]);
56+
}
57+
58+
- (void)testDeserializeListOfModels {
59+
NSArray *data =
60+
@[
61+
@{
62+
@"id": @119,
63+
@"category": @{
64+
@"id": @0,
65+
@"name": @"string"
66+
},
67+
@"name": @"doggie",
68+
@"photoUrls": @[
69+
@"string"
70+
],
71+
@"tags": @[
72+
@{
73+
@"id": @0,
74+
@"name": @"string"
75+
}
76+
],
77+
@"status": @"available"
78+
79+
}];
80+
81+
NSArray *result = [apiClient deserialize:data class:@"NSArray<SWGPet>*"];
82+
83+
XCTAssertTrue([result isKindOfClass:[NSArray class]]);
84+
XCTAssertTrue([[result firstObject] isKindOfClass:[SWGPet class]]);
85+
XCTAssertEqualObjects([[result firstObject] _id], @119);
86+
}
87+
88+
- (void)testDeserializeMapOfModels {
89+
NSDictionary *data =
90+
@{
91+
@"pet": @{
92+
@"id": @119,
93+
@"category": @{
94+
@"id": @0,
95+
@"name": @"string"
96+
},
97+
@"name": @"doggie",
98+
@"photoUrls": @[
99+
@"string"
100+
],
101+
@"tags": @[
102+
@{
103+
@"id": @0,
104+
@"name": @"string"
105+
}
106+
],
107+
@"status": @"available"
108+
109+
}
110+
};
111+
112+
NSDictionary *result = [apiClient deserialize:data class:@"NSDictionary* /* NSString, SWGPet */"];
113+
114+
XCTAssertTrue([result isKindOfClass:[NSDictionary class]]);
115+
XCTAssertTrue([result[@"pet"] isKindOfClass:[SWGPet class]]);
116+
XCTAssertEqualObjects([result[@"pet"] _id], @119);
117+
}
118+
119+
@end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#import <UIKit/UIKit.h>
2+
#import <XCTest/XCTest.h>
3+
#import <SwaggerClient/SWGPet.h>
4+
5+
@interface PetTest : XCTestCase {
6+
7+
@private SWGPet *pet;
8+
9+
}
10+
11+
@end
12+
13+
@implementation PetTest
14+
15+
- (void)setUp {
16+
[super setUp];
17+
18+
NSDictionary *petDict = @{ @"id": @1, @"name": @"test pet",
19+
@"status": @"sold",
20+
@"photoUrls": @[@"string"],
21+
@"category": @{ @"id": @1, @"name": @"test category" },
22+
@"tags": @[ @{ @"id": @1, @"name": @"test tag" } ]};
23+
pet = [[SWGPet alloc] initWithDictionary:petDict error:nil];
24+
}
25+
26+
- (void)testDescription {
27+
NSDictionary *petDict = @{ @"id": @1, @"name": @"test pet",
28+
@"status": @"sold",
29+
@"photoUrls": @[@"string"],
30+
@"category": @{ @"id": @1, @"name": @"test category" },
31+
@"tags": @[ @{ @"id": @1, @"name": @"test tag" } ]};
32+
NSString *petStr = [petDict description];
33+
34+
XCTAssertTrue([[pet description] isEqualToString:petStr]);
35+
}
36+
37+
@end

0 commit comments

Comments
 (0)