Skip to content

Commit

Permalink
Also take into account containing type name when choosing XmlTextAttr…
Browse files Browse the repository at this point in the history
…ibute member name (yes, this collision is really happening in the schemas I need to process).
  • Loading branch information
jokokko committed Oct 10, 2018
1 parent a9aef9a commit e1e5282
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,30 @@ public void MixedTypeMustNotCollideWithExistingMembers()
generatedType.First());
}

[Fact]
public void MixedTypeMustNotCollideWithContainingTypeName()
{
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" targetNamespace=""http://local.none"" xmlns:l=""http://local.none"">
<xs:element name=""document"" type=""l:Text"">
</xs:element>
<xs:complexType name=""Text"" mixed=""true"">
</xs:complexType>
</xs:schema>";

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)
Expand Down
2 changes: 1 addition & 1 deletion XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
}
Expand Down

0 comments on commit e1e5282

Please sign in to comment.