forked from loafle/openapi-generator-original
add jsonmodel in objective-c client
This commit is contained in:
parent
20add5b58e
commit
fbe069cf5c
@ -48,6 +48,7 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
"NSObject",
|
||||
"NSArray",
|
||||
"NSNumber",
|
||||
"NSDate",
|
||||
"NSDictionary",
|
||||
"NSMutableArray",
|
||||
"NSMutableDictionary")
|
||||
@ -57,6 +58,7 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
"NSNumber",
|
||||
"NSString",
|
||||
"NSObject",
|
||||
"NSDate",
|
||||
"bool")
|
||||
);
|
||||
|
||||
@ -70,9 +72,8 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
|
||||
typeMapping = new HashMap<String, String>();
|
||||
typeMapping.put("enum", "NSString");
|
||||
typeMapping.put("Date", "SWGDate");
|
||||
typeMapping.put("DateTime", "SWGDate");
|
||||
// typeMapping.put("Date", "SWGDate");
|
||||
typeMapping.put("Date", "NSDate");
|
||||
typeMapping.put("DateTime", "NSDate");
|
||||
typeMapping.put("boolean", "NSNumber");
|
||||
typeMapping.put("string", "NSString");
|
||||
typeMapping.put("integer", "NSNumber");
|
||||
@ -87,13 +88,13 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
typeMapping.put("object", "NSObject");
|
||||
|
||||
importMapping = new HashMap<String, String> ();
|
||||
importMapping.put("Date", "SWGDate");
|
||||
|
||||
foundationClasses = new HashSet<String> (
|
||||
Arrays.asList(
|
||||
"NSNumber",
|
||||
"NSObject",
|
||||
"NSString",
|
||||
"NSDate",
|
||||
"NSDictionary")
|
||||
);
|
||||
|
||||
@ -151,12 +152,46 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
|
||||
@Override
|
||||
public String getTypeDeclaration(Property p) {
|
||||
if (p instanceof ArrayProperty) {
|
||||
ArrayProperty ap = (ArrayProperty) p;
|
||||
Property inner = ap.getItems();
|
||||
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);
|
||||
|
||||
if (innerTypeDeclaration.endsWith("*"))
|
||||
innerTypeDeclaration = innerTypeDeclaration.substring(0, innerTypeDeclaration.length() - 1);
|
||||
|
||||
return getSwaggerType(p) + "<" + innerTypeDeclaration + ">*";
|
||||
}
|
||||
else {
|
||||
String swaggerType = getSwaggerType(p);
|
||||
if(languageSpecificPrimitives.contains(swaggerType) && !foundationClasses.contains(swaggerType))
|
||||
return toModelName(swaggerType);
|
||||
else
|
||||
|
||||
// In this codition, type of p is objective-c primitive type, e.g. `NSSNumber',
|
||||
// return type of p with pointer, e.g. `NSNumber*'
|
||||
if (languageSpecificPrimitives.contains(swaggerType) &&
|
||||
foundationClasses.contains(swaggerType)) {
|
||||
return swaggerType + "*";
|
||||
}
|
||||
// In this codition, type of p is c primitive type, e.g. `bool',
|
||||
// return type of p, e.g. `bool'
|
||||
else if (languageSpecificPrimitives.contains(swaggerType)) {
|
||||
return swaggerType;
|
||||
}
|
||||
// In this codition, type of p is objective-c object type, e.g. `SWGPet',
|
||||
// return type of p with pointer, e.g. `'
|
||||
else {
|
||||
return swaggerType + "*";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toModelName(String type) {
|
||||
|
@ -1,3 +1,4 @@
|
||||
platform :ios, '6.0'
|
||||
xcodeproj '{{projectName}}/{{projectName}}.xcodeproj'
|
||||
pod 'AFNetworking', '~> 2.1'
|
||||
pod 'JSONModel', '~> 1.0'
|
||||
|
@ -1,6 +1,5 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "JSONModel.h"
|
||||
|
||||
@interface SWGObject : NSObject
|
||||
- (id) initWithValues:(NSDictionary*)dict;
|
||||
- (NSDictionary*) asDictionary;
|
||||
@interface SWGObject : JSONModel
|
||||
@end
|
||||
|
@ -1,17 +1,4 @@
|
||||
#import "SWGObject.h"
|
||||
|
||||
@implementation SWGObject
|
||||
|
||||
- (id) initWithValues:(NSDictionary*)dict {
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSDictionary*) asDictionary{
|
||||
return [NSDictionary init];
|
||||
}
|
||||
|
||||
- (NSString*)description {
|
||||
return [NSString stringWithFormat:@"%@ %@", [super description], [self asDictionary]];
|
||||
}
|
||||
|
||||
@end
|
||||
|
@ -1,81 +1,12 @@
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
#import "SWGDate.h"
|
||||
#import "{{classname}}.h"
|
||||
|
||||
@implementation {{classname}}
|
||||
|
||||
-(id){{#vars}}{{name}}: ({{datatype}}) {{name}}
|
||||
{{/vars}}
|
||||
{{newline}}{
|
||||
{{#vars}}_{{name}} = {{name}};
|
||||
{{/vars}}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
-(id) initWithValues:(NSDictionary*)dict
|
||||
+ (JSONKeyMapper *)keyMapper
|
||||
{
|
||||
self = [super init];
|
||||
if(self) {
|
||||
{{#vars}}{{#isPrimitiveType}}_{{name}} = dict[@"{{baseName}}"];{{/isPrimitiveType}}
|
||||
{{#complexType}}
|
||||
id {{name}}_dict = dict[@"{{baseName}}"];
|
||||
{{#isContainer}}
|
||||
if([{{name}}_dict isKindOfClass:[NSArray class]]) {
|
||||
NSMutableArray * objs = [[NSMutableArray alloc] initWithCapacity:[(NSArray*){{name}}_dict count]];
|
||||
if([(NSArray*){{name}}_dict count] > 0) {
|
||||
for (NSDictionary* dict in (NSArray*){{name}}_dict) {
|
||||
{{{complexType}}}* d = [[{{#instantiationType}}NSClassFromString(@"{{{instantiationType}}}") {{/instantiationType}}{{^instantiationType}}{{{complexType}}}{{/instantiationType}} alloc] initWithValues:dict];
|
||||
[objs addObject:d];
|
||||
}
|
||||
_{{{name}}} = [[NSArray alloc] initWithArray:objs];
|
||||
}
|
||||
else
|
||||
_{{name}} = [[NSArray alloc] init];
|
||||
}
|
||||
else {
|
||||
_{{name}} = [[NSArray alloc] init];
|
||||
}
|
||||
{{/isContainer}}{{#isNotContainer}}
|
||||
if({{name}}_dict != nil)
|
||||
_{{name}} = [[{{#instantiationType}}NSClassFromString(@"{{{instantiationType}}}") {{/instantiationType}}{{^instantiationType}}{{{complexType}}} {{/instantiationType}} alloc]{{setter}}:{{name}}_dict];
|
||||
{{/isNotContainer}}
|
||||
{{/complexType}}
|
||||
{{/vars}}{{newline}}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
-(NSDictionary*) asDictionary {
|
||||
NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
|
||||
{{#vars}}
|
||||
{{#complexType}}
|
||||
if(_{{name}} != nil){
|
||||
if([_{{name}} isKindOfClass:[NSArray class]]){
|
||||
NSMutableArray * array = [[NSMutableArray alloc] init];
|
||||
for( {{complexType}} *{{name}} in (NSArray*)_{{name}}) {
|
||||
[array addObject:[(SWGObject*){{name}} asDictionary]];
|
||||
}
|
||||
dict[@"{{name}}"] = array;
|
||||
}
|
||||
else if(_{{name}} && [_{{name}} isKindOfClass:[SWGDate class]]) {
|
||||
NSString * dateString = [(SWGDate*)_{{name}} toString];
|
||||
if(dateString){
|
||||
dict[@"{{name}}"] = dateString;
|
||||
}
|
||||
}
|
||||
else {
|
||||
{{/complexType}}
|
||||
if(_{{name}} != nil) dict[@"{{baseName}}"] = {{#complexType}}[(SWGObject*){{/complexType}}_{{name}} {{#complexType}}asDictionary]{{/complexType}};
|
||||
{{#complexType}}
|
||||
}
|
||||
}
|
||||
{{/complexType}}
|
||||
{{/vars}}
|
||||
|
||||
NSDictionary* output = [dict copy];
|
||||
return output;
|
||||
return [[JSONKeyMapper alloc] initWithDictionary:@{ {{#vars}}@"{{baseName}}": @"{{name}}"{{#hasMore}}, {{/hasMore}}{{/vars}} }];
|
||||
}
|
||||
|
||||
{{/model}}
|
||||
|
@ -6,17 +6,16 @@
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
|
||||
@protocol {{classname}}
|
||||
@end
|
||||
|
||||
@interface {{classname}} : SWGObject
|
||||
|
||||
{{#vars}}
|
||||
@property(nonatomic) {{datatype}} {{name}}; {{#description}}/* {{{description}}} {{#isNotRequired}}[optional]{{/isNotRequired}} */{{/description}}{{newline}}
|
||||
{{#description}}/* {{{description}}} {{#isNotRequired}}[optional]{{/isNotRequired}} */{{/description}}
|
||||
@property(nonatomic) {{{ datatype }}}{{#isNotRequired}}<Optional>{{/isNotRequired}} {{name}};
|
||||
{{/vars}}
|
||||
- (id) {{#vars}}{{name}}: ({{datatype}}) {{name}}{{#hasMore}}{{newline}} {{/hasMore}}{{^hasMore}};{{/hasMore}}
|
||||
{{/vars}}
|
||||
{{newline}}
|
||||
- (id) initWithValues: (NSDictionary*)dict;
|
||||
- (NSDictionary*) asDictionary;
|
||||
{{newline}}
|
||||
|
||||
@end
|
||||
{{/model}}
|
||||
{{/models}}
|
@ -56,17 +56,14 @@ class ObjcModelTest extends FlatSpec with Matchers {
|
||||
vars.get(1).isNotContainer should equal (true)
|
||||
|
||||
vars.get(2).baseName should be ("createdAt")
|
||||
vars.get(2).complexType should be ("SWGDate")
|
||||
vars.get(2).datatype should be ("SWGDate*")
|
||||
vars.get(2).datatype should be ("NSDate*")
|
||||
vars.get(2).name should be ("createdAt")
|
||||
vars.get(2).defaultValue should be (null)
|
||||
vars.get(2).baseType should be ("SWGDate")
|
||||
vars.get(2).baseType should be ("NSDate")
|
||||
vars.get(2).hasMore should equal (null)
|
||||
vars.get(2).required should equal (false)
|
||||
vars.get(2).isNotContainer should equal (true)
|
||||
|
||||
(cm.imports.asScala.toSet &
|
||||
Set("SWGDate")).size should be (1)
|
||||
}
|
||||
|
||||
it should "convert a model with list property" in {
|
||||
@ -173,7 +170,7 @@ class ObjcModelTest extends FlatSpec with Matchers {
|
||||
val vars = cm.vars
|
||||
vars.get(0).baseName should be ("children")
|
||||
vars.get(0).complexType should be ("SWGChildren")
|
||||
vars.get(0).datatype should be ("NSArray*")
|
||||
vars.get(0).datatype should be ("NSArray<SWGChildren>*")
|
||||
vars.get(0).name should be ("children")
|
||||
vars.get(0).baseType should be ("NSArray")
|
||||
vars.get(0).containerType should be ("array")
|
||||
|
Loading…
x
Reference in New Issue
Block a user