Skip to content

Commit

Permalink
Merge pull request #88 from jokokko/master
Browse files Browse the repository at this point in the history
For mixed complexTypes, the XmlTextAttribute member should not collid…
  • Loading branch information
mganss authored Oct 10, 2018
2 parents 5d55070 + e1e5282 commit 72abaaf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
50 changes: 49 additions & 1 deletion XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,56 @@ public void EditorBrowsableAttributeRespectsCodeTypeReferenceOptions(CodeTypeRef
generatedType.First());
}

[Fact]
public void MixedTypeMustNotCollideWithExistingMembers()
{
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:elem"">
</xs:element>
<xs:complexType name=""elem"" mixed=""true"">
<xs:attribute name=""Text"" type=""xs:string""/>
</xs:complexType>
</xs:schema>";

[Theory]
var generatedType = ConvertXml(nameof(MixedTypeMustNotCollideWithExistingMembers), xsd, new Generator
{
NamespaceProvider = new NamespaceProvider
{
GenerateNamespace = key => "Test"
}
});

Assert.Contains(
@"public string[] Text_1 { get; set; }",
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
3 changes: 2 additions & 1 deletion XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public ModelBuilder(GeneratorConfiguration configuration, XmlSchemaSet set)

foreach (var rootElement in set.GlobalElements.Values.Cast<XmlSchemaElement>())
{
var source = new Uri(rootElement.GetSchema().SourceUri);
var rootSchema = rootElement.GetSchema();
var source = !string.IsNullOrEmpty(rootSchema.SourceUri) ? new Uri(rootElement.GetSchema().SourceUri) : default(Uri);
var qualifiedName = rootElement.ElementSchemaType.QualifiedName;
if (qualifiedName.IsEmpty) { qualifiedName = rootElement.QualifiedName; }
var type = CreateTypeModel(source, rootElement.ElementSchemaType, qualifiedName);
Expand Down
11 changes: 9 additions & 2 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,15 @@ public override CodeTypeDeclaration Generate()
}

if (IsMixed && (BaseClass == null || (BaseClass is ClassModel && !AllBaseClasses.Any(b => b.IsMixed))))
{
var text = new CodeMemberField(typeof(string), "Text");
{
var propName = "Text";

// To not collide with any existing members
for (var propertyIndex = 1; Properties.Any(x => x.Name.Equals(propName, StringComparison.Ordinal)) || propName.Equals(classDeclaration.Name, StringComparison.Ordinal); propertyIndex++)
{
propName = $"Text_{propertyIndex}";
}
var text = new CodeMemberField(typeof(string[]), propName);
// hack to generate automatic property
text.Name += " { get; set; }";
text.Attributes = MemberAttributes.Public;
Expand Down

0 comments on commit 72abaaf

Please sign in to comment.