Skip to content

Commit

Permalink
For mixed complexTypes, the XmlTextAttribute member should not collid…
Browse files Browse the repository at this point in the history
…e with existing members.

XmlTextAttribute member string -> string[] (so no data loss in serializing mixed elements).
  • Loading branch information
jokokko committed Oct 10, 2018
1 parent 5d55070 commit a9aef9a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
26 changes: 25 additions & 1 deletion XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,32 @@ 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());
}

[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)); 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 a9aef9a

Please sign in to comment.