From 73239a0be3b2d6602723a8dc26f4cce78da8b78b Mon Sep 17 00:00:00 2001 From: jokokko Date: Mon, 8 Oct 2018 17:55:19 +0300 Subject: [PATCH 1/2] If member names collide (e.g. attributes with the same names but from different namespaces), override names to non-colliding. --- .../XmlSchemaClassGenerator.Tests.csproj | 6 +++++ XmlSchemaClassGenerator.Tests/XmlTests.cs | 10 +++++++++ .../xml/sameattributenames.xsd | 16 ++++++++++++++ .../xml/sameattributenames_import.xsd | 10 +++++++++ XmlSchemaClassGenerator/TypeModel.cs | 22 +++++++++++++++---- 5 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 XmlSchemaClassGenerator.Tests/xml/sameattributenames.xsd create mode 100644 XmlSchemaClassGenerator.Tests/xml/sameattributenames_import.xsd diff --git a/XmlSchemaClassGenerator.Tests/XmlSchemaClassGenerator.Tests.csproj b/XmlSchemaClassGenerator.Tests/XmlSchemaClassGenerator.Tests.csproj index 28c87204..73ecadbc 100644 --- a/XmlSchemaClassGenerator.Tests/XmlSchemaClassGenerator.Tests.csproj +++ b/XmlSchemaClassGenerator.Tests/XmlSchemaClassGenerator.Tests.csproj @@ -128,6 +128,12 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + PreserveNewest diff --git a/XmlSchemaClassGenerator.Tests/XmlTests.cs b/XmlSchemaClassGenerator.Tests/XmlTests.cs index 216ff11d..b81f706a 100644 --- a/XmlSchemaClassGenerator.Tests/XmlTests.cs +++ b/XmlSchemaClassGenerator.Tests/XmlTests.cs @@ -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() { diff --git a/XmlSchemaClassGenerator.Tests/xml/sameattributenames.xsd b/XmlSchemaClassGenerator.Tests/xml/sameattributenames.xsd new file mode 100644 index 00000000..dda80a3c --- /dev/null +++ b/XmlSchemaClassGenerator.Tests/xml/sameattributenames.xsd @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/XmlSchemaClassGenerator.Tests/xml/sameattributenames_import.xsd b/XmlSchemaClassGenerator.Tests/xml/sameattributenames_import.xsd new file mode 100644 index 00000000..0ff0aa76 --- /dev/null +++ b/XmlSchemaClassGenerator.Tests/xml/sameattributenames_import.xsd @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index a71fa388..2f9f9074 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -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 From df3be5b7a25bbbb586e3c259fddfcce4ff12d512 Mon Sep 17 00:00:00 2001 From: jokokko Date: Tue, 9 Oct 2018 14:19:34 +0300 Subject: [PATCH 2/2] Always iterate over groups, no matter the count (saves one or two GetEnumerator calls). --- XmlSchemaClassGenerator/TypeModel.cs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index 2f9f9074..a65aab14 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -385,19 +385,16 @@ public override CodeTypeDeclaration Generate() 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)) + var propertyIndex = 0; + foreach (var p in property) + { + if (propertyIndex > 0) { - n.Name += $"_{propertyIndex}"; - propertyIndex++; - n.AddMembersTo(classDeclaration, Configuration.EnableDataBinding); + p.Name += $"_{propertyIndex}"; } - } - + p.AddMembersTo(classDeclaration, Configuration.EnableDataBinding); + propertyIndex++; + } } if (IsMixed && (BaseClass == null || (BaseClass is ClassModel && !AllBaseClasses.Any(b => b.IsMixed))))