Skip to content

Commit

Permalink
Merge pull request #86 from jokokko/namecollisions
Browse files Browse the repository at this point in the history
If member names collide (e.g. attributes with the same names but from…
  • Loading branch information
mganss authored Oct 9, 2018
2 parents 4ee90be + df3be5b commit 5d55070
Show file tree
Hide file tree
Showing 5 changed files with 57 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>
19 changes: 15 additions & 4 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,21 @@ 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))
{
var propertyIndex = 0;
foreach (var p in property)
{
if (propertyIndex > 0)
{
p.Name += $"_{propertyIndex}";
}
p.AddMembersTo(classDeclaration, Configuration.EnableDataBinding);
propertyIndex++;
}
}

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 5d55070

Please sign in to comment.