Skip to content

Commit d911748

Browse files
committed
[objc] Add casts that avoid method resolution errors
Because the `data` that we're deserializing is of type `id` (essentially untyped), it's possible to have method resolution clashes without explicitly casting here once we've parsed a type. I had this issue with a pagination container model, for instance, which has a field named `count` that conflicts with the property of the same name on `NSArray` or `NSDictionary`.
1 parent ea1c280 commit d911748

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,10 @@ static void (^reachabilityChangeBlock)(int);
311311
range:NSMakeRange(0, [class length])];
312312

313313
if (match) {
314+
NSArray *dataArray = data;
314315
innerType = [class substringWithRange:[match rangeAtIndex:1]];
315316
316-
resultArray = [NSMutableArray arrayWithCapacity:[data count]];
317+
resultArray = [NSMutableArray arrayWithCapacity:[dataArray count]];
317318
[data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
318319
[resultArray addObject:[self deserialize:obj class:innerType]];
319320
}
@@ -332,9 +333,10 @@ static void (^reachabilityChangeBlock)(int);
332333
range:NSMakeRange(0, [class length])];
333334

334335
if (match) {
336+
NSArray *dataArray = data;
335337
innerType = [class substringWithRange:[match rangeAtIndex:1]];
336338
337-
resultArray = [NSMutableArray arrayWithCapacity:[data count]];
339+
resultArray = [NSMutableArray arrayWithCapacity:[dataArray count]];
338340
[data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
339341
[resultArray addObject:[self deserialize:obj class:innerType]];
340342
}];
@@ -352,9 +354,10 @@ static void (^reachabilityChangeBlock)(int);
352354
range:NSMakeRange(0, [class length])];
353355

354356
if (match) {
357+
NSDictionary *dataDict = data;
355358
NSString *valueType = [class substringWithRange:[match rangeAtIndex:2]];
356359
357-
resultDict = [NSMutableDictionary dictionaryWithCapacity:[data count]];
360+
resultDict = [NSMutableDictionary dictionaryWithCapacity:[dataDict count]];
358361
[data enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
359362
[resultDict setValue:[self deserialize:obj class:valueType] forKey:key];
360363
}];
@@ -728,7 +731,8 @@ static void (^reachabilityChangeBlock)(int);
728731
return [object ISO8601String];
729732
}
730733
else if ([object isKindOfClass:[NSArray class]]) {
731-
NSMutableArray *sanitizedObjs = [NSMutableArray arrayWithCapacity:[object count]];
734+
NSArray *objectArray = object;
735+
NSMutableArray *sanitizedObjs = [NSMutableArray arrayWithCapacity:[objectArray count]];
732736
[object enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
733737
if (obj) {
734738
[sanitizedObjs addObject:[self sanitizeForSerialization:obj]];
@@ -737,7 +741,8 @@ static void (^reachabilityChangeBlock)(int);
737741
return sanitizedObjs;
738742
}
739743
else if ([object isKindOfClass:[NSDictionary class]]) {
740-
NSMutableDictionary *sanitizedObjs = [NSMutableDictionary dictionaryWithCapacity:[object count]];
744+
NSDictionary *objectDict = object;
745+
NSMutableDictionary *sanitizedObjs = [NSMutableDictionary dictionaryWithCapacity:[objectDict count]];
741746
[object enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
742747
if (obj) {
743748
[sanitizedObjs setValue:[self sanitizeForSerialization:obj] forKey:key];

0 commit comments

Comments
 (0)