Skip to content

Commit

Permalink
Merge pull request #198 from AVTit/master
Browse files Browse the repository at this point in the history
Do not generate XmlElementAttribute IsNillable=true for collection pr…
  • Loading branch information
mganss authored May 13, 2020
2 parents d10f039 + 835f080 commit aee0512
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
40 changes: 39 additions & 1 deletion XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1797,7 +1797,7 @@ public void AmbiguousAnonymousTypesTest()
{
GenerateNamespace = key =>key.XmlSchemaNamespace
}
, };
};
var contents = ConvertXml(nameof(GenerateXmlRootAttributeForEnumTest), new[]{xsd1, xsd2}, generator).ToArray();
Assert.Equal(2, contents.Length);

Expand Down Expand Up @@ -1842,5 +1842,43 @@ public void AmbiguousAnonymousTypesTest()
Assert.Equal("TestType2Property", xmlRootAttribute.ElementName);
Assert.Equal("Test_NS2", xmlRootAttribute.Namespace);
}

[Fact]
public void TestShouldPatternForCollections()
{
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"" targetNamespace=""Test_NS1""
elementFormDefault=""qualified"" attributeFormDefault=""unqualified"">
<xs:element name=""TestType"">
<xs:complexType>
<xs:choice maxOccurs=""unbounded"">
<xs:element name=""DateValue"" type=""xs:dateTime"" nillable=""true""/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name=""TestType2"">
<xs:complexType>
<xs:choice maxOccurs=""unbounded"">
<xs:element name=""StringValue"" type=""xs:string"" nillable=""true""/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>";

var generator = new Generator
{
NamespaceProvider = new NamespaceProvider
{
GenerateNamespace = key => key.XmlSchemaNamespace
}
};

var contents = ConvertXml(nameof(TestShouldPatternForCollections), xsd, generator).ToArray();
Assert.Equal(1, contents.Length);
var assembly = Compiler.Compile(nameof(GenerateXmlRootAttributeForEnumTest), contents);
var testType = assembly.GetType("Test_NS1.TestType");
var serializer = new XmlSerializer(testType);
Assert.NotNull(serializer);
}
}
}
2 changes: 1 addition & 1 deletion XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,7 @@ private IEnumerable<CodeAttributeDeclaration> GetAttributes(bool isArray)
}
}

if (IsNillable)
if (IsNillable && !(IsCollection && Type is SimpleModel m && m.ValueType.IsValueType))
{
attribute.Arguments.Add(new CodeAttributeArgument("IsNullable", new CodePrimitiveExpression(true)));
}
Expand Down

0 comments on commit aee0512

Please sign in to comment.