diff --git a/XmlSchemaClassGenerator.Tests/DefaultInSequenceTests.cs b/XmlSchemaClassGenerator.Tests/DefaultInSequenceTests.cs new file mode 100644 index 00000000..270fe93e --- /dev/null +++ b/XmlSchemaClassGenerator.Tests/DefaultInSequenceTests.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace XmlSchemaClassGenerator.Tests +{ + public class DefaultInSequenceTests + { + /// + /// In case a element which is defined as a sequence, has a default value, + /// the default value shall be ignored (it was wrong to initialize a collection with int value, for example). + /// bakcing field was like: private Collection _someElementWithDefaultValue = 0; + /// In such case, the syntax breaks compliation. + /// We would like to verify the default is ignored. + /// + [Fact] + public void SequenceWithDefaultValue_CompilationPass() + { + var assembly = Compiler.Generate(nameof(SequenceWithDefaultValue_CompilationPass), + "xsd/SequenceWithDefault/*.xsd", new Generator + { + GenerateNullables = true, + UseShouldSerializePattern = true, + NamespaceProvider = new NamespaceProvider + { + GenerateNamespace = key => "Test" + } + }); + + var type = assembly.GetType("Test.SomeComplexType"); + + var collectionProperties = type + .GetProperties() + .Select(p => (p.Name, p.PropertyType)) + .OrderBy(p => p.Name) + .ToArray(); + + Assert.Equal(new[] + { + ("SomeElementWithDefault", typeof(Collection)), + }, collectionProperties); + } + } +} diff --git a/XmlSchemaClassGenerator.Tests/xsd/SequenceWithDefault/SomeComplexType.xsd b/XmlSchemaClassGenerator.Tests/xsd/SequenceWithDefault/SomeComplexType.xsd new file mode 100644 index 00000000..9f070837 --- /dev/null +++ b/XmlSchemaClassGenerator.Tests/xsd/SequenceWithDefault/SomeComplexType.xsd @@ -0,0 +1,15 @@ + + + + + + + + + \ No newline at end of file diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index d1a4b6e4..75dde456 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -716,7 +716,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi typeDeclaration.Members.Add(backingField); } - if (DefaultValue == null || (isEnumerable && !IsRequired)) + if (DefaultValue == null || isEnumerable) { if (isNullableValueType && Configuration.GenerateNullables && !(Configuration.UseShouldSerializePattern && !IsAttribute)) member.Name += Value;