Skip to content

Commit

Permalink
If member names collide (e.g. attributes with the same names but from…
Browse files Browse the repository at this point in the history
… different namespaces), override names to non-colliding.
  • Loading branch information
jokokko committed Oct 8, 2018
1 parent 4ee90be commit 73239a0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@
<None Update="xml\office_min.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="xml\sameattributenames.xsd">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="xml\sameattributenames_import.xsd">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="xml\seniorCare_max.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
10 changes: 10 additions & 0 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,16 @@ public void EditorBrowsableAttributeRespectsCodeTypeReferenceOptions(CodeTypeRef
}


[Theory]
[InlineData(@"xml/sameattributenames.xsd", @"xml/sameattributenames_import.xsd")]
public void CollidingAttributeAndPropertyNamesCanBeResolved(params string[] files)
{
// Compilation would previously throw due to duplicate type name within type
var assembly = Compiler.GenerateFiles("AttributesWithSameName", files);

Assert.NotNull(assembly);
}

[Fact]
public void ComplexTypeWithAttributeGroupExtension()
{
Expand Down
16 changes: 16 additions & 0 deletions XmlSchemaClassGenerator.Tests/xml/sameattributenames.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:a="http://none.local/a" xmlns:b="http://none.local/b" elementFormDefault="qualified" targetNamespace="http://none.local/a">
<import namespace="http://none.local/b" schemaLocation="sameattributenames_import.xsd" />
<element name="document" type="a:elem" />
<element name="document2" type="a:elem2" />
<complexType name="elem">
<sequence>
<element name="Type" type="a:elem2" />
</sequence>
<attribute ref="b:type" />
<attribute name="type" type="string" />
</complexType>
<complexType name="elem2">
<attribute name="type" type="string" />
</complexType>
</schema>
10 changes: 10 additions & 0 deletions XmlSchemaClassGenerator.Tests/xml/sameattributenames_import.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://none.local/b" xmlns:b="http://none.local/b">
<xs:attribute name="type" type="b:typeType"/>
<xs:simpleType name="typeType">
<xs:restriction base="xs:token">
<xs:enumeration value="a"/>
<xs:enumeration value="b"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
22 changes: 18 additions & 4 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,24 @@ public override CodeTypeDeclaration Generate()
keyProperty.IsKey = true;
}

foreach (var property in Properties)
property.AddMembersTo(classDeclaration, Configuration.EnableDataBinding);

if (IsMixed && (BaseClass == null || (BaseClass is ClassModel && !AllBaseClasses.Any(b => b.IsMixed))))
foreach (var property in Properties.GroupBy(x => x.Name))
{
property.First().AddMembersTo(classDeclaration, Configuration.EnableDataBinding);

if (property.Count() > 1)
{
var propertyIndex = 1;
foreach (var n in property.Skip(1))
{
n.Name += $"_{propertyIndex}";
propertyIndex++;
n.AddMembersTo(classDeclaration, Configuration.EnableDataBinding);
}
}

}

if (IsMixed && (BaseClass == null || (BaseClass is ClassModel && !AllBaseClasses.Any(b => b.IsMixed))))
{
var text = new CodeMemberField(typeof(string), "Text");
// hack to generate automatic property
Expand Down

0 comments on commit 73239a0

Please sign in to comment.