Merge remote-tracking branch 'main/master' into jmaster

This commit is contained in:
Tommy Alander 2016-03-03 09:23:50 +01:00
commit e23626b05c
5 changed files with 109 additions and 1 deletions

View File

@ -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;

View File

@ -881,6 +881,10 @@ public class DefaultCodegen {
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;

View File

@ -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`.

View File

@ -35,7 +35,8 @@ public class ObjcModelTest {
.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);
@ -43,6 +44,7 @@ public class ObjcModelTest {
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");

View File

@ -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"
}
}
}
}
}