From e1e52821cf6cbe5cecace5bb4d992985dc01f155 Mon Sep 17 00:00:00 2001 From: jokokko Date: Wed, 10 Oct 2018 15:01:13 +0300 Subject: [PATCH] Also take into account containing type name when choosing XmlTextAttribute member name (yes, this collision is really happening in the schemas I need to process). --- XmlSchemaClassGenerator.Tests/XmlTests.cs | 24 +++++++++++++++++++++++ XmlSchemaClassGenerator/TypeModel.cs | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/XmlSchemaClassGenerator.Tests/XmlTests.cs b/XmlSchemaClassGenerator.Tests/XmlTests.cs index 7843952c..c67b332d 100644 --- a/XmlSchemaClassGenerator.Tests/XmlTests.cs +++ b/XmlSchemaClassGenerator.Tests/XmlTests.cs @@ -402,6 +402,30 @@ public void MixedTypeMustNotCollideWithExistingMembers() generatedType.First()); } + [Fact] + public void MixedTypeMustNotCollideWithContainingTypeName() + { + const string xsd = @" + + + + + +"; + + var generatedType = ConvertXml(nameof(MixedTypeMustNotCollideWithExistingMembers), xsd, new Generator + { + NamespaceProvider = new NamespaceProvider + { + GenerateNamespace = key => "Test" + } + }); + + Assert.Contains( + @"public string[] Text_1 { get; set; }", + generatedType.First()); + } + [Theory] [InlineData(@"xml/sameattributenames.xsd", @"xml/sameattributenames_import.xsd")] public void CollidingAttributeAndPropertyNamesCanBeResolved(params string[] files) diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index bcb3ec14..e45ce3f1 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -402,7 +402,7 @@ public override CodeTypeDeclaration Generate() var propName = "Text"; // To not collide with any existing members - for (var propertyIndex = 1; Properties.Any(x => x.Name.Equals(propName, StringComparison.Ordinal)); propertyIndex++) + for (var propertyIndex = 1; Properties.Any(x => x.Name.Equals(propName, StringComparison.Ordinal)) || propName.Equals(classDeclaration.Name, StringComparison.Ordinal); propertyIndex++) { propName = $"Text_{propertyIndex}"; }