Skip to content

Commit

Permalink
Remove dupplicate interface members in derived interfaces
Browse files Browse the repository at this point in the history
This commit ensures that all interface properties with the same name and type as in base interfaces are removed as duplicates.
  • Loading branch information
AVTit committed May 1, 2020
1 parent b219658 commit 9d32425
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
80 changes: 80 additions & 0 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1502,5 +1502,85 @@ public void RenameInterfacePropertyInDerivedClassTest()
Assert.Single(level3Interface.GetProperties());
Assert.Equal("ClassItemBaseProperty", level3Interface.GetProperties().First().Name);
}

[Fact]
public void DoNotGenerateSamePropertiesInDerivedInterfacesClassTest()
{
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema""
elementFormDefault=""qualified"" attributeFormDefault=""unqualified"">
<xs:element name=""ParentClass"">
<xs:complexType>
<xs:group ref=""Level1""/>
</xs:complexType>
</xs:element>
<xs:group name=""Level1"">
<xs:choice>
<xs:sequence>
<xs:element name=""InterfaceProperty"" type=""xs:string""/>
<xs:group ref=""Level2""/>
</xs:sequence>
</xs:choice>
</xs:group>
<xs:group name=""Level2"">
<xs:sequence>
<xs:element name=""InterfaceProperty"" type=""xs:string""/>
<xs:group ref=""Level3""/>
</xs:sequence>
</xs:group>
<xs:group name=""Level22"">
<xs:sequence>
<xs:element name=""InterfaceProperty"" type=""xs:string""/>
<xs:element name=""Level22OwnProperty"" type=""xs:string""/>
<xs:group ref=""Level3""/>
</xs:sequence>
</xs:group>
<xs:group name=""Level3"">
<xs:choice>
<xs:element name=""InterfaceProperty"" type=""xs:string""/>
</xs:choice>
</xs:group>
</xs:schema>";

var generator = new Generator
{
NamespaceProvider = new NamespaceProvider
{
GenerateNamespace = key => "Test"
},
GenerateInterfaces = true,
AssemblyVisible = true
};
var contents = ConvertXml(nameof(DoNotGenerateSamePropertiesInDerivedInterfacesClassTest), xsd, generator);
var content = Assert.Single(contents);

var assembly = Compiler.Compile(nameof(DoNotGenerateSamePropertiesInDerivedInterfacesClassTest), content);

var listType = assembly.GetType("Test.ParentClass");
Assert.NotNull(listType);

var listTypePropertyInfo = listType.GetProperties().FirstOrDefault(p => p.Name == "InterfaceProperty");
Assert.NotNull(listTypePropertyInfo);

var level1Interface = assembly.GetType("Test.ILevel1");
Assert.NotNull(level1Interface);
Assert.Empty(level1Interface.GetProperties());

var level2Interface = assembly.GetType("Test.ILevel2");
Assert.NotNull(level2Interface);
Assert.Empty(level2Interface.GetProperties());

var level3Interface = assembly.GetType("Test.ILevel3");
Assert.NotNull(level3Interface);
var level3InterfacePropertyInfo = level3Interface.GetProperties().FirstOrDefault(p => p.Name == "InterfaceProperty");
Assert.NotNull(level3InterfacePropertyInfo);

}
}
}
20 changes: 20 additions & 0 deletions XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ public ModelBuilder(GeneratorConfiguration configuration, XmlSchemaSet set)
if (configuration.GenerateInterfaces)
{
RenameInterfacePropertiesIfRenamedInDerivedClasses();
RemoveDuplicateInterfaceProperties();
}
}

private void RemoveDuplicateInterfaceProperties()
{
foreach (var interfaceModel in Types.Values.OfType<InterfaceModel>())
{
var parentProperties = interfaceModel.Properties.ToList();
foreach (var baseInterfaceType in interfaceModel.AllDerivedReferenceTypes().OfType<InterfaceModel>())
{
foreach (var parentProperty in parentProperties)
{
var baseProperties = baseInterfaceType.Properties.ToList();
foreach (var baseProperty in baseProperties.Where(baseProperty => parentProperty.Name == baseProperty.Name && parentProperty.Type.Name == baseProperty.Type.Name))
{
baseInterfaceType.Properties.Remove(baseProperty);
}
}
}
}
}

Expand Down

0 comments on commit 9d32425

Please sign in to comment.