Skip to content

Commit

Permalink
Merge pull request #304 from fabsenet/master
Browse files Browse the repository at this point in the history
Avoid XmlElementAttribute creation for Properties created via a ref=""
  • Loading branch information
mganss authored Feb 2, 2022
2 parents b351a92 + c52f248 commit 5cb6f30
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
51 changes: 51 additions & 0 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1765,6 +1765,57 @@ public void RenameInterfacePropertyInDerivedClassTest()
Assert.Equal("ClassItemBaseProperty", level3Interface.GetProperties().First().Name);
}

[Fact]
public void RefTypesGetNoXmlElementAttributeTest()
{
const string xsd = @"<?xml version=""1.0"" encoding=""utf-16""?>
<xs:schema xmlns=""SampleNamespace"" targetNamespace=""SampleNamespace"" version=""1.0"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
<xs:element name=""SampleRoot"">
<xs:complexType>
<xs:sequence>
<xs:element name=""Direct"">
<xs:complexType>
<xs:sequence>
<xs:element name=""Direct1"" type=""xs:string"" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element ref=""ViaRef"" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name=""ViaRef"">
<xs:complexType>
<xs:sequence>
<xs:element name=""ViaRef1"" type=""xs:string"" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>";

var generator = new Generator
{
NamespaceProvider = new NamespaceProvider
{
GenerateNamespace = key => "Test"
},
GenerateInterfaces = true,
AssemblyVisible = true
};
var contents = ConvertXml(nameof(RefTypesGetNoXmlElementAttributeTest), xsd, generator);
var content = Assert.Single(contents);

var assembly = Compiler.Compile(nameof(RefTypesGetNoXmlElementAttributeTest), content);
var classType = assembly.GetType("Test.SampleRoot");
Assert.NotNull(classType);

var directProperty = Assert.Single(classType.GetProperties().Where(p => p.Name == "Direct"));
Assert.Equal(XmlSchemaForm.Unqualified, directProperty.GetCustomAttributes<XmlElementAttribute>().FirstOrDefault()?.Form);

var viaRefProperty = Assert.Single(classType.GetProperties().Where(p => p.Name == "ViaRef"));
Assert.Equal(XmlSchemaForm.None, viaRefProperty.GetCustomAttributes<XmlElementAttribute>().FirstOrDefault()?.Form);
}

[Fact]
public void DoNotGenerateSamePropertiesInDerivedInterfacesClassTest()
{
Expand Down
11 changes: 10 additions & 1 deletion XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -912,14 +912,23 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
IsCollection = item.MaxOccurs > 1.0m || particle.MaxOccurs > 1.0m, // http://msdn.microsoft.com/en-us/library/vstudio/d3hx2s7e(v=vs.100).aspx
DefaultValue = element.DefaultValue ?? ((item.MinOccurs >= 1.0m && item.XmlParent is not XmlSchemaChoice) ? element.FixedValue : null),
FixedValue = element.FixedValue,
Form = element.Form == XmlSchemaForm.None ? element.GetSchema().ElementFormDefault : element.Form,
XmlNamespace = !string.IsNullOrEmpty(effectiveElement.QualifiedName.Namespace) && effectiveElement.QualifiedName.Namespace != typeModel.XmlSchemaName.Namespace
? effectiveElement.QualifiedName.Namespace : null,
XmlParticle = item.XmlParticle,
XmlParent = item.XmlParent,
Particle = item
};

if (element.Form == XmlSchemaForm.None)
{
if (element.RefName != null && !element.RefName.IsEmpty)
property.Form = XmlSchemaForm.Qualified;
else
property.Form = element.GetSchema().ElementFormDefault;
}
else
property.Form = element.Form;

if (property.IsArray && !_configuration.GenerateComplexTypesForCollections)
{
property.Type.Namespace.Types.Remove(property.Type.Name);
Expand Down

0 comments on commit 5cb6f30

Please sign in to comment.