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

Added discriminator support to Objective-C #2202

Merged
merged 1 commit into from
Mar 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class CodegenModel {
public String parent, parentSchema;
public String name, classname, description, classVarName, modelJson, dataType;
public String unescapedDescription;
public String discriminator;
public String defaultValue;
public List<CodegenProperty> vars = new ArrayList<CodegenProperty>();
public List<String> allowableValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,10 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
m.externalDocs = model.getExternalDocs();
m.vendorExtensions = model.getVendorExtensions();

if (model instanceof ModelImpl) {
m.discriminator = ((ModelImpl) model).getDiscriminator();
}


if (model instanceof ArrayModel) {
ArrayModel am = (ArrayModel) model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@
return self;
}

{{#discriminator}}
/**
Maps "discriminator" value to the sub-class name, so that inheritance is supported.
*/
- (id)initWithDictionary:(NSDictionary *)dict error:(NSError *__autoreleasing *)err {


NSString * discriminatedClassName = [dict valueForKey:@"{{discriminator}}"];

if(discriminatedClassName == nil ){
return [super initWithDictionary:dict error:err];
}

Class class = NSClassFromString([@"{{classPrefix}}" stringByAppendingString:discriminatedClassName]);

if([self class ] == class) {
return [super initWithDictionary:dict error:err];
}


return [[class alloc] initWithDictionary:dict error: err];

}
{{/discriminator}}

/**
* Maps json key to property name.
* This method is used by `JSONModel`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ public void simpleModelTest() {
.property("name", new StringProperty())
.property("createdAt", new DateTimeProperty())
.required("id")
.required("name");
.required("name")
.discriminator("test");
final DefaultCodegen codegen = new ObjcClientCodegen();
final CodegenModel cm = codegen.fromModel("sample", model);

Assert.assertEquals(cm.name, "sample");
Assert.assertEquals(cm.classname, "SWGSample");
Assert.assertEquals(cm.description, "a sample model");
Assert.assertEquals(cm.vars.size(), 3);
Assert.assertEquals(cm.discriminator,"test");

final CodegenProperty property1 = cm.vars.get(0);
Assert.assertEquals(property1.baseName, "id");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"swagger" : "2.0",
"info" : {},
"basePath" : "/v1",
"tags" : [ {
"name" : "pets",
"description" : "some pets"
}],
"paths" : {
"/pets" : {
"get" : {
"tags" : [ "pets" ],
"summary" : "Get your pets",
"description" : "Returns pets of different types",
"operationId" : "getPets",
"consumes" : [ "application/x-www-form-urlencoded" ],
"produces" : [ "application/json" ],
"parameters" : [ ],
"responses" : {
"200" : {
"description" : "successful operation",
"schema" : {
"$ref" : "#/definitions/Animal"
}
},
"409" : {
"description" : "User already has an account or an account request."
},
"500" : {
"description" : "Error creating the account request"
}
}
}
}
},

"definitions" : {
"Dog" : {
"allOf" : [ {
"$ref" : "#/definitions/Animal"
}, {
"type" : "object",
"properties" : {
"breed" : {
"type" : "string"
}
}
} ]
},
"Cat" : {
"allOf" : [ {
"$ref" : "#/definitions/Animal"
}, {
"type" : "object",
"properties" : {
"declawed" : {
"type" : "boolean"
}
}
} ]
},
"Animal" : {
"type" : "object",
"discriminator": "className",
"required": [
"className"
],
"properties" : {
"className" : {
"type" : "string"
}
}
}

}
}