Skip to content

Commit 5d55070

Browse files
authored
Merge pull request #86 from jokokko/namecollisions
If member names collide (e.g. attributes with the same names but from…
2 parents 4ee90be + df3be5b commit 5d55070

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

XmlSchemaClassGenerator.Tests/XmlSchemaClassGenerator.Tests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@
128128
<None Update="xml\office_min.xml">
129129
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
130130
</None>
131+
<None Update="xml\sameattributenames.xsd">
132+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
133+
</None>
134+
<None Update="xml\sameattributenames_import.xsd">
135+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
136+
</None>
131137
<None Update="xml\seniorCare_max.xml">
132138
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
133139
</None>

XmlSchemaClassGenerator.Tests/XmlTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,16 @@ public void EditorBrowsableAttributeRespectsCodeTypeReferenceOptions(CodeTypeRef
378378
}
379379

380380

381+
[Theory]
382+
[InlineData(@"xml/sameattributenames.xsd", @"xml/sameattributenames_import.xsd")]
383+
public void CollidingAttributeAndPropertyNamesCanBeResolved(params string[] files)
384+
{
385+
// Compilation would previously throw due to duplicate type name within type
386+
var assembly = Compiler.GenerateFiles("AttributesWithSameName", files);
387+
388+
Assert.NotNull(assembly);
389+
}
390+
381391
[Fact]
382392
public void ComplexTypeWithAttributeGroupExtension()
383393
{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<import namespace="http://none.local/b" schemaLocation="sameattributenames_import.xsd" />
4+
<element name="document" type="a:elem" />
5+
<element name="document2" type="a:elem2" />
6+
<complexType name="elem">
7+
<sequence>
8+
<element name="Type" type="a:elem2" />
9+
</sequence>
10+
<attribute ref="b:type" />
11+
<attribute name="type" type="string" />
12+
</complexType>
13+
<complexType name="elem2">
14+
<attribute name="type" type="string" />
15+
</complexType>
16+
</schema>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://none.local/b" xmlns:b="http://none.local/b">
3+
<xs:attribute name="type" type="b:typeType"/>
4+
<xs:simpleType name="typeType">
5+
<xs:restriction base="xs:token">
6+
<xs:enumeration value="a"/>
7+
<xs:enumeration value="b"/>
8+
</xs:restriction>
9+
</xs:simpleType>
10+
</xs:schema>

XmlSchemaClassGenerator/TypeModel.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,21 @@ public override CodeTypeDeclaration Generate()
383383
keyProperty.IsKey = true;
384384
}
385385

386-
foreach (var property in Properties)
387-
property.AddMembersTo(classDeclaration, Configuration.EnableDataBinding);
388-
389-
if (IsMixed && (BaseClass == null || (BaseClass is ClassModel && !AllBaseClasses.Any(b => b.IsMixed))))
386+
foreach (var property in Properties.GroupBy(x => x.Name))
387+
{
388+
var propertyIndex = 0;
389+
foreach (var p in property)
390+
{
391+
if (propertyIndex > 0)
392+
{
393+
p.Name += $"_{propertyIndex}";
394+
}
395+
p.AddMembersTo(classDeclaration, Configuration.EnableDataBinding);
396+
propertyIndex++;
397+
}
398+
}
399+
400+
if (IsMixed && (BaseClass == null || (BaseClass is ClassModel && !AllBaseClasses.Any(b => b.IsMixed))))
390401
{
391402
var text = new CodeMemberField(typeof(string), "Text");
392403
// hack to generate automatic property

0 commit comments

Comments
 (0)