diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java index 4df150f81a8..c034e61f1e8 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java @@ -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 vars = new ArrayList(); public List allowableValues; diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index 0ad30ed8459..90207dfbe82 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -864,6 +864,10 @@ public CodegenModel fromModel(String name, Model model, Map 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; diff --git a/modules/swagger-codegen/src/main/resources/objc/model-body.mustache b/modules/swagger-codegen/src/main/resources/objc/model-body.mustache index 3d70cebfae2..ec5ab3b9f80 100644 --- a/modules/swagger-codegen/src/main/resources/objc/model-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/model-body.mustache @@ -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`. diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/objc/ObjcModelTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/objc/ObjcModelTest.java index 7d3eeaa4108..fed94aa04f6 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/objc/ObjcModelTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/objc/ObjcModelTest.java @@ -35,7 +35,8 @@ 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); @@ -43,6 +44,7 @@ public void simpleModelTest() { 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"); diff --git a/modules/swagger-codegen/src/test/resources/2_0/discriminatorTest.json b/modules/swagger-codegen/src/test/resources/2_0/discriminatorTest.json new file mode 100644 index 00000000000..09182fba661 --- /dev/null +++ b/modules/swagger-codegen/src/test/resources/2_0/discriminatorTest.json @@ -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" + } + } + } + + } +} \ No newline at end of file