mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-03 22:20:56 +00:00
Merge pull request #1026 from geekerzp/objc-deserializtion
[Objc] Fix deserialize nested map error
This commit is contained in:
commit
fe83d18a09
@ -215,21 +215,22 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
Property inner = ap.getItems();
|
Property inner = ap.getItems();
|
||||||
String innerType = getSwaggerType(inner);
|
String innerType = getSwaggerType(inner);
|
||||||
|
|
||||||
// In this codition, type of property p is array of primitive,
|
|
||||||
// return container type with pointer, e.g. `NSArray*'
|
|
||||||
if (languageSpecificPrimitives.contains(innerType)) {
|
|
||||||
return getSwaggerType(p) + "*";
|
|
||||||
}
|
|
||||||
|
|
||||||
// In this codition, type of property p is array of model,
|
|
||||||
// return container type combine inner type with pointer, e.g. `NSArray<SWGTag>*'
|
|
||||||
String innerTypeDeclaration = getTypeDeclaration(inner);
|
String innerTypeDeclaration = getTypeDeclaration(inner);
|
||||||
|
|
||||||
if (innerTypeDeclaration.endsWith("*")) {
|
if (innerTypeDeclaration.endsWith("*")) {
|
||||||
innerTypeDeclaration = innerTypeDeclaration.substring(0, innerTypeDeclaration.length() - 1);
|
innerTypeDeclaration = innerTypeDeclaration.substring(0, innerTypeDeclaration.length() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// In this codition, type of property p is array of primitive,
|
||||||
|
// return container type with pointer, e.g. `NSArray* /* NSString */'
|
||||||
|
if (languageSpecificPrimitives.contains(innerType)) {
|
||||||
|
return getSwaggerType(p) + "*" + " /* " + innerTypeDeclaration + " */";
|
||||||
|
}
|
||||||
|
// In this codition, type of property p is array of model,
|
||||||
|
// return container type combine inner type with pointer, e.g. `NSArray<SWGTag>*'
|
||||||
|
else {
|
||||||
return getSwaggerType(p) + "<" + innerTypeDeclaration + ">*";
|
return getSwaggerType(p) + "<" + innerTypeDeclaration + ">*";
|
||||||
|
}
|
||||||
} else if (p instanceof MapProperty) {
|
} else if (p instanceof MapProperty) {
|
||||||
MapProperty mp = (MapProperty) p;
|
MapProperty mp = (MapProperty) p;
|
||||||
Property inner = mp.getAdditionalProperties();
|
Property inner = mp.getAdditionalProperties();
|
||||||
|
@ -356,13 +356,14 @@ static void (^reachabilityChangeBlock)(int);
|
|||||||
NSTextCheckingResult *match = nil;
|
NSTextCheckingResult *match = nil;
|
||||||
NSMutableArray *resultArray = nil;
|
NSMutableArray *resultArray = nil;
|
||||||
NSMutableDictionary *resultDict = nil;
|
NSMutableDictionary *resultDict = nil;
|
||||||
|
NSString *innerType = nil;
|
||||||
|
|
||||||
// return nil if data is nil or class is nil
|
// return nil if data is nil or class is nil
|
||||||
if (!data || !class) {
|
if (!data || !class) {
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove "*" from class, if ends with "*"
|
// remove "*" from class, if ends with "*"
|
||||||
if ([class hasSuffix:@"*"]) {
|
if ([class hasSuffix:@"*"]) {
|
||||||
class = [class substringToIndex:[class length] - 1];
|
class = [class substringToIndex:[class length] - 1];
|
||||||
}
|
}
|
||||||
@ -383,7 +384,7 @@ static void (^reachabilityChangeBlock)(int);
|
|||||||
range:NSMakeRange(0, [class length])];
|
range:NSMakeRange(0, [class length])];
|
||||||
|
|
||||||
if (match) {
|
if (match) {
|
||||||
NSString *innerType = [class substringWithRange:[match rangeAtIndex:1]];
|
innerType = [class substringWithRange:[match rangeAtIndex:1]];
|
||||||
|
|
||||||
resultArray = [NSMutableArray arrayWithCapacity:[data count]];
|
resultArray = [NSMutableArray arrayWithCapacity:[data count]];
|
||||||
[data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
[data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
||||||
@ -395,8 +396,8 @@ static void (^reachabilityChangeBlock)(int);
|
|||||||
}
|
}
|
||||||
|
|
||||||
// list of primitives
|
// list of primitives
|
||||||
NSString *arrayOfPrimitivesPet = @"NSArray";
|
NSString *arrayOfPrimitivesPat = @"NSArray\\* /\\* (.+) \\*/";
|
||||||
regexp = [NSRegularExpression regularExpressionWithPattern:arrayOfPrimitivesPet
|
regexp = [NSRegularExpression regularExpressionWithPattern:arrayOfPrimitivesPat
|
||||||
options:NSRegularExpressionCaseInsensitive
|
options:NSRegularExpressionCaseInsensitive
|
||||||
error:nil];
|
error:nil];
|
||||||
match = [regexp firstMatchInString:class
|
match = [regexp firstMatchInString:class
|
||||||
@ -404,16 +405,18 @@ static void (^reachabilityChangeBlock)(int);
|
|||||||
range:NSMakeRange(0, [class length])];
|
range:NSMakeRange(0, [class length])];
|
||||||
|
|
||||||
if (match) {
|
if (match) {
|
||||||
|
innerType = [class substringWithRange:[match rangeAtIndex:1]];
|
||||||
|
|
||||||
resultArray = [NSMutableArray arrayWithCapacity:[data count]];
|
resultArray = [NSMutableArray arrayWithCapacity:[data count]];
|
||||||
[data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
[data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
||||||
[resultArray addObject:[self deserialize:obj class:NSStringFromClass([obj class])]];
|
[resultArray addObject:[self deserialize:obj class:innerType]];
|
||||||
}];
|
}];
|
||||||
|
|
||||||
return resultArray;
|
return resultArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
// map
|
// map
|
||||||
NSString *dictPat = @"NSDictionary\\* /\\* (.+), (.+) \\*/";
|
NSString *dictPat = @"NSDictionary\\* /\\* (.+?), (.+) \\*/";
|
||||||
regexp = [NSRegularExpression regularExpressionWithPattern:dictPat
|
regexp = [NSRegularExpression regularExpressionWithPattern:dictPat
|
||||||
options:NSRegularExpressionCaseInsensitive
|
options:NSRegularExpressionCaseInsensitive
|
||||||
error:nil];
|
error:nil];
|
||||||
|
@ -90,7 +90,7 @@ class ObjcModelTest extends FlatSpec with Matchers {
|
|||||||
vars.get(0).isNotContainer should equal(true)
|
vars.get(0).isNotContainer should equal(true)
|
||||||
|
|
||||||
vars.get(1).baseName should be("urls")
|
vars.get(1).baseName should be("urls")
|
||||||
vars.get(1).datatype should be("NSArray*")
|
vars.get(1).datatype should be("NSArray* /* NSString */")
|
||||||
vars.get(1).name should be("urls")
|
vars.get(1).name should be("urls")
|
||||||
// vars.get(1).defaultValue should be ("null")
|
// vars.get(1).defaultValue should be ("null")
|
||||||
vars.get(1).baseType should be("NSArray")
|
vars.get(1).baseType should be("NSArray")
|
||||||
|
@ -356,13 +356,14 @@ static void (^reachabilityChangeBlock)(int);
|
|||||||
NSTextCheckingResult *match = nil;
|
NSTextCheckingResult *match = nil;
|
||||||
NSMutableArray *resultArray = nil;
|
NSMutableArray *resultArray = nil;
|
||||||
NSMutableDictionary *resultDict = nil;
|
NSMutableDictionary *resultDict = nil;
|
||||||
|
NSString *innerType = nil;
|
||||||
|
|
||||||
// return nil if data is nil or class is nil
|
// return nil if data is nil or class is nil
|
||||||
if (!data || !class) {
|
if (!data || !class) {
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove "*" from class, if ends with "*"
|
// remove "*" from class, if ends with "*"
|
||||||
if ([class hasSuffix:@"*"]) {
|
if ([class hasSuffix:@"*"]) {
|
||||||
class = [class substringToIndex:[class length] - 1];
|
class = [class substringToIndex:[class length] - 1];
|
||||||
}
|
}
|
||||||
@ -383,7 +384,7 @@ static void (^reachabilityChangeBlock)(int);
|
|||||||
range:NSMakeRange(0, [class length])];
|
range:NSMakeRange(0, [class length])];
|
||||||
|
|
||||||
if (match) {
|
if (match) {
|
||||||
NSString *innerType = [class substringWithRange:[match rangeAtIndex:1]];
|
innerType = [class substringWithRange:[match rangeAtIndex:1]];
|
||||||
|
|
||||||
resultArray = [NSMutableArray arrayWithCapacity:[data count]];
|
resultArray = [NSMutableArray arrayWithCapacity:[data count]];
|
||||||
[data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
[data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
||||||
@ -395,8 +396,8 @@ static void (^reachabilityChangeBlock)(int);
|
|||||||
}
|
}
|
||||||
|
|
||||||
// list of primitives
|
// list of primitives
|
||||||
NSString *arrayOfPrimitivesPet = @"NSArray";
|
NSString *arrayOfPrimitivesPat = @"NSArray\\* /\\* (.+) \\*/";
|
||||||
regexp = [NSRegularExpression regularExpressionWithPattern:arrayOfPrimitivesPet
|
regexp = [NSRegularExpression regularExpressionWithPattern:arrayOfPrimitivesPat
|
||||||
options:NSRegularExpressionCaseInsensitive
|
options:NSRegularExpressionCaseInsensitive
|
||||||
error:nil];
|
error:nil];
|
||||||
match = [regexp firstMatchInString:class
|
match = [regexp firstMatchInString:class
|
||||||
@ -404,16 +405,18 @@ static void (^reachabilityChangeBlock)(int);
|
|||||||
range:NSMakeRange(0, [class length])];
|
range:NSMakeRange(0, [class length])];
|
||||||
|
|
||||||
if (match) {
|
if (match) {
|
||||||
|
innerType = [class substringWithRange:[match rangeAtIndex:1]];
|
||||||
|
|
||||||
resultArray = [NSMutableArray arrayWithCapacity:[data count]];
|
resultArray = [NSMutableArray arrayWithCapacity:[data count]];
|
||||||
[data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
[data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
||||||
[resultArray addObject:[self deserialize:obj class:NSStringFromClass([obj class])]];
|
[resultArray addObject:[self deserialize:obj class:innerType]];
|
||||||
}];
|
}];
|
||||||
|
|
||||||
return resultArray;
|
return resultArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
// map
|
// map
|
||||||
NSString *dictPat = @"NSDictionary\\* /\\* (.+), (.+) \\*/";
|
NSString *dictPat = @"NSDictionary\\* /\\* (.+?), (.+) \\*/";
|
||||||
regexp = [NSRegularExpression regularExpressionWithPattern:dictPat
|
regexp = [NSRegularExpression regularExpressionWithPattern:dictPat
|
||||||
options:NSRegularExpressionCaseInsensitive
|
options:NSRegularExpressionCaseInsensitive
|
||||||
error:nil];
|
error:nil];
|
||||||
|
@ -23,7 +23,7 @@
|
|||||||
|
|
||||||
@property(nonatomic) NSString* name;
|
@property(nonatomic) NSString* name;
|
||||||
|
|
||||||
@property(nonatomic) NSArray* photoUrls;
|
@property(nonatomic) NSArray* /* NSString */ photoUrls;
|
||||||
|
|
||||||
@property(nonatomic) NSArray<SWGTag>* tags;
|
@property(nonatomic) NSArray<SWGTag>* tags;
|
||||||
/* pet status in the store [optional]
|
/* pet status in the store [optional]
|
||||||
|
@ -59,7 +59,7 @@
|
|||||||
///
|
///
|
||||||
///
|
///
|
||||||
/// @return NSArray<SWGPet>*
|
/// @return NSArray<SWGPet>*
|
||||||
-(NSNumber*) findPetsByStatusWithCompletionBlock :(NSArray*) status
|
-(NSNumber*) findPetsByStatusWithCompletionBlock :(NSArray* /* NSString */) status
|
||||||
|
|
||||||
completionHandler: (void (^)(NSArray<SWGPet>* output, NSError* error))completionBlock;
|
completionHandler: (void (^)(NSArray<SWGPet>* output, NSError* error))completionBlock;
|
||||||
|
|
||||||
@ -74,7 +74,7 @@
|
|||||||
///
|
///
|
||||||
///
|
///
|
||||||
/// @return NSArray<SWGPet>*
|
/// @return NSArray<SWGPet>*
|
||||||
-(NSNumber*) findPetsByTagsWithCompletionBlock :(NSArray*) tags
|
-(NSNumber*) findPetsByTagsWithCompletionBlock :(NSArray* /* NSString */) tags
|
||||||
|
|
||||||
completionHandler: (void (^)(NSArray<SWGPet>* output, NSError* error))completionBlock;
|
completionHandler: (void (^)(NSArray<SWGPet>* output, NSError* error))completionBlock;
|
||||||
|
|
||||||
|
@ -260,7 +260,7 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
|
|||||||
///
|
///
|
||||||
/// @returns NSArray<SWGPet>*
|
/// @returns NSArray<SWGPet>*
|
||||||
///
|
///
|
||||||
-(NSNumber*) findPetsByStatusWithCompletionBlock: (NSArray*) status
|
-(NSNumber*) findPetsByStatusWithCompletionBlock: (NSArray* /* NSString */) status
|
||||||
|
|
||||||
completionHandler: (void (^)(NSArray<SWGPet>* output, NSError* error))completionBlock {
|
completionHandler: (void (^)(NSArray<SWGPet>* output, NSError* error))completionBlock {
|
||||||
|
|
||||||
@ -341,7 +341,7 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
|
|||||||
///
|
///
|
||||||
/// @returns NSArray<SWGPet>*
|
/// @returns NSArray<SWGPet>*
|
||||||
///
|
///
|
||||||
-(NSNumber*) findPetsByTagsWithCompletionBlock: (NSArray*) tags
|
-(NSNumber*) findPetsByTagsWithCompletionBlock: (NSArray* /* NSString */) tags
|
||||||
|
|
||||||
completionHandler: (void (^)(NSArray<SWGPet>* output, NSError* error))completionBlock {
|
completionHandler: (void (^)(NSArray<SWGPet>* output, NSError* error))completionBlock {
|
||||||
|
|
||||||
|
@ -55,6 +55,14 @@
|
|||||||
XCTAssertTrue([result isEqualToString:data]);
|
XCTAssertTrue([result isEqualToString:data]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)testDeserializeListOfString {
|
||||||
|
NSArray *data = @[@"test string"];
|
||||||
|
NSArray *result = [apiClient deserialize:data class:@"NSArray* /* NSString */"];
|
||||||
|
|
||||||
|
XCTAssertTrue([result isKindOfClass:[NSArray class]]);
|
||||||
|
XCTAssertTrue([result[0] isKindOfClass:[NSString class]]);
|
||||||
|
}
|
||||||
|
|
||||||
- (void)testDeserializeListOfModels {
|
- (void)testDeserializeListOfModels {
|
||||||
NSArray *data =
|
NSArray *data =
|
||||||
@[
|
@[
|
||||||
@ -116,4 +124,29 @@
|
|||||||
XCTAssertEqualObjects([result[@"pet"] _id], @119);
|
XCTAssertEqualObjects([result[@"pet"] _id], @119);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)testDeserializeNestedMap {
|
||||||
|
NSDictionary *data =
|
||||||
|
@{
|
||||||
|
@"foo": @{
|
||||||
|
@"bar": @1
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
NSDictionary *result = [apiClient deserialize:data class:@"NSDictionary* /* NSString, NSDictionary* /* NSString, NSNumber */ */"];
|
||||||
|
|
||||||
|
XCTAssertTrue([result isKindOfClass:[NSDictionary class]]);
|
||||||
|
XCTAssertTrue([result[@"foo"] isKindOfClass:[NSDictionary class]]);
|
||||||
|
XCTAssertTrue([result[@"foo"][@"bar"] isKindOfClass:[NSNumber class]]);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)testDeserializeNestedList {
|
||||||
|
NSArray *data = @[@[@"foo"]];
|
||||||
|
|
||||||
|
NSArray *result = [apiClient deserialize:data class:@"NSArray* /* NSArray* /* NSString */ */"];
|
||||||
|
|
||||||
|
XCTAssertTrue([result isKindOfClass:[NSArray class]]);
|
||||||
|
XCTAssertTrue([result[0] isKindOfClass:[NSArray class]]);
|
||||||
|
XCTAssertTrue([result[0][0] isKindOfClass:[NSString class]]);
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user