diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/TypeExtensions.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/TypeExtensions.cs index de747454ba..e4e2afdc98 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/TypeExtensions.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/TypeExtensions.cs @@ -45,6 +45,8 @@ public static object GetDefaultValue(this Type type) public static Type[] GetInheritanceChain(this Type type) { + if (type.IsInterface) { return type.GetInterfaces(); } + var inheritanceChain = new List(); var current = type; diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs index 135005011a..ae6b45f426 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs @@ -236,6 +236,17 @@ public void GenerateSchema_IncludesInheritedProperties_IfTypeIsAnInterfaceHierar Assert.Equal(expectedPropertyNames.OrderBy(n => n), schema.Properties.Keys.OrderBy(k => k)); } + [Fact] + public void GenerateSchema_KeepMostDerivedType_IfTypeIsAnInterface() + { + var schemaRepository = new SchemaRepository(); + + var referenceSchema = Subject().GenerateSchema(typeof(INewBaseInterface), schemaRepository); + + var schema = schemaRepository.Schemas[referenceSchema.Reference.Id]; + Assert.Equal("integer", schema.Properties["BaseProperty"].Type); + } + [Fact] public void GenerateSchema_ExcludesIndexerProperties_IfComplexTypeIsIndexed() { diff --git a/test/Swashbuckle.AspNetCore.TestSupport/Fixtures/BaseInterface.cs b/test/Swashbuckle.AspNetCore.TestSupport/Fixtures/BaseInterface.cs index 616f6b855c..f857e4662a 100644 --- a/test/Swashbuckle.AspNetCore.TestSupport/Fixtures/BaseInterface.cs +++ b/test/Swashbuckle.AspNetCore.TestSupport/Fixtures/BaseInterface.cs @@ -19,4 +19,9 @@ public interface IMultiSubInterface : ISubInterface1, ISubInterface2 { public int Property3 { get; set; } } + + public interface INewBaseInterface : IBaseInterface + { + public new int BaseProperty { get; set; } + } }