Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parse issue when genericClass of array is NSURL #281

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions YYModel/NSObject+YYModel.m
Expand Up @@ -902,6 +902,18 @@ static void ModelSetValueForProperty(__unsafe_unretained id model,
for (id one in valueArr) {
if ([one isKindOfClass:meta->_genericCls]) {
[objectArr addObject:one];
} else if (meta->_genericCls == [NSString class] && [one isKindOfClass:[NSNumber class]]) {
[objectArr addObject:((NSNumber *)one).stringValue];
} else if (meta->_genericCls == [NSMutableString class] && [one isKindOfClass:[NSNumber class]]) {
[objectArr addObject:((NSNumber *)one).stringValue.mutableCopy];
} else if (meta->_genericCls == [NSNumber class]) {
[objectArr addObject:YYNSNumberCreateFromID(one)];
} else if (meta->_genericCls == [NSURL class] && [one isKindOfClass:[NSString class]]) {
NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *str = [one stringByTrimmingCharactersInSet:set];
if (str.length > 0) {
[objectArr addObject:[[NSURL alloc] initWithString:str]];
}
} else if ([one isKindOfClass:[NSDictionary class]]) {
Class cls = meta->_genericCls;
if (meta->_hasCustomClassFromDictionary) {
Expand Down
29 changes: 28 additions & 1 deletion YYModelTests/YYTestCustomClass.m
Expand Up @@ -47,14 +47,24 @@ @interface YYTestCustomClassModel : NSObject
@property (nonatomic, strong) NSDictionary *userDict;
@property (nonatomic, strong) NSSet *userSet;
@property (nonatomic, strong) YYBaseUser *user;
@property (nonatomic, strong) NSArray<NSURL *> *imageURLs;
@property (nonatomic, strong) NSArray<NSString *> *phones;
@property (nonatomic, strong) NSArray<NSMutableString *> *editPhones;
@property (nonatomic, strong) NSArray<NSNumber *> *scores;

@end

@implementation YYTestCustomClassModel

+ (NSDictionary *)modelContainerPropertyGenericClass {
return @{@"users" : YYBaseUser.class,
@"userDict" : YYBaseUser.class,
@"userSet" : YYBaseUser.class};
@"userSet" : YYBaseUser.class,
@"imageURLs" : NSURL.class,
@"phones" : NSString.class,
@"editPhones" : NSMutableString.class,
@"scores" : NSNumber.class,
};
}
+ (Class)modelCustomClassForDictionary:(NSDictionary*)dictionary {
if (dictionary[@"localName"]) {
Expand All @@ -80,6 +90,9 @@ - (void)test {
NSDictionary *jsonUserBase = @{@"uid" : @123, @"name" : @"Harry"};
NSDictionary *jsonUserLocal = @{@"uid" : @123, @"name" : @"Harry", @"localName" : @"HarryLocal"};
NSDictionary *jsonUserRemote = @{@"uid" : @123, @"name" : @"Harry", @"remoteName" : @"HarryRemote"};
NSArray *jsonImageURLs = @[@"http://aaa.com", @"http://bbb.com", @""];
NSArray *jsonPhones = @[@13000000001, @13000000002, @"13000000003"];
NSArray *jsonScores = @[@"90", @"80", @"70"];

user = [YYBaseUser yy_modelWithDictionary:jsonUserBase];
XCTAssert([user isMemberOfClass:[YYBaseUser class]]);
Expand All @@ -90,6 +103,20 @@ - (void)test {
user = [YYBaseUser yy_modelWithDictionary:jsonUserRemote];
XCTAssert([user isMemberOfClass:[YYRemoteUser class]]);

model = [YYTestCustomClassModel yy_modelWithJSON:@{@"imageURLs" : jsonImageURLs}];
XCTAssert([model.imageURLs[0] isMemberOfClass:[NSURL class]]);

model = [YYTestCustomClassModel yy_modelWithJSON:@{@"phones" : jsonPhones}];
XCTAssert([model.phones[0] isKindOfClass:[NSString class]]);

model = [YYTestCustomClassModel yy_modelWithJSON:@{@"editPhones" : jsonPhones}];
//__NSCFConstantString->__NSCFString->NSMutableString->NSString
//model.editPhones[2]不是 NSMutableString,使用 `appendString:` 会导致 crash
XCTAssert([model.editPhones[2] isKindOfClass:[NSMutableString class]]);

model = [YYTestCustomClassModel yy_modelWithJSON:@{@"scores" : jsonScores}];
XCTAssert([model.scores[0] isKindOfClass:[NSNumber class]]);


model = [YYTestCustomClassModel yy_modelWithJSON:@{@"user" : jsonUserLocal}];
XCTAssert([model.user isMemberOfClass:[YYLocalUser class]]);
Expand Down