Skip to content

Commit

Permalink
#282 fix NullReferenceException for null public arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
stilettk committed Aug 27, 2021
1 parent dd2313e commit 44280f5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
40 changes: 40 additions & 0 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2208,6 +2208,46 @@ public void TestArrayOfMsTypeGeneration()
//Assert.NotEmpty((System.Collections.IEnumerable)deserialized.D); //<== oops
}

[Fact]
public void TestArrayOfStringsWhenPublicAndNull()
{
// see https://github.com/mganss/XmlSchemaClassGenerator/issues/282

// arrange
var xsd =
@"<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:tns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"">
<xs:complexType name=""ArrayOfstring"">
<xs:sequence>
<xs:element name=""testString"" type=""xs:string"" minOccurs=""0"" maxOccurs=""unbounded""/>
</xs:sequence>
</xs:complexType>
</xs:schema>
";
var validXml =
@"<ArrayOfstring xmlns:tns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"">
</ArrayOfstring>
";
var generator = new Generator
{
IntegerDataType = typeof(int),
NamespacePrefix = "Test_NS1",
GenerateNullables = true,
CollectionType = typeof(System.Array),
CollectionSettersMode = CollectionSettersMode.Public
};
var contents = ConvertXml(nameof(TestArrayOfStringsWhenPublicAndNull), new[] { xsd }, generator).ToArray();
var assembly = Compiler.Compile(nameof(TestForceIsNullableGeneration), contents);
var testType = assembly.GetType("Test_NS1.ArrayOfstring");
Assert.NotNull(testType);
var serializer = new XmlSerializer(testType);

// act
dynamic deserialized = serializer.Deserialize(new StringReader(validXml));

// assert
var xml = Serialize(serializer, deserialized);
}

[Fact, TestPriority(1)]
public void AirspaceServicesTest1()
{
Expand Down
2 changes: 1 addition & 1 deletion XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
var countProperty = collectionType == typeof(System.Array) ? "Length" : "Count";
var countReference = new CodePropertyReferenceExpression(listReference, countProperty);
var notZeroExpression = new CodeBinaryOperatorExpression(countReference, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(0));
if (Configuration.CollectionSettersMode == CollectionSettersMode.PublicWithoutConstructorInitialization)
if (Configuration.CollectionSettersMode is CollectionSettersMode.PublicWithoutConstructorInitialization or CollectionSettersMode.Public)
{
var notNullExpression = new CodeBinaryOperatorExpression(listReference, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null));
notZeroExpression = new CodeBinaryOperatorExpression(notNullExpression, CodeBinaryOperatorType.BooleanAnd, notZeroExpression);
Expand Down

0 comments on commit 44280f5

Please sign in to comment.