Skip to content

Commit

Permalink
Merge pull request #283 from stilettk/282-fix-null-public-arrays
Browse files Browse the repository at this point in the history
#282 fix NullReferenceException for null public arrays
  • Loading branch information
mganss authored Aug 29, 2021
2 parents dd2313e + 9f1da01 commit c7f5dcd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
44 changes: 44 additions & 0 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ private static IEnumerable<string> ConvertXml(string name, IEnumerable<string> x
NetCoreSpecificCode = generatorPrototype.NetCoreSpecificCode,
GenerateCommandLineArgumentsComment = generatorPrototype.GenerateCommandLineArgumentsComment,
CommandLineArgumentsProvider = generatorPrototype.CommandLineArgumentsProvider,
CollectionType = generatorPrototype.CollectionType,
CollectionImplementationType = generatorPrototype.CollectionImplementationType,
CollectionSettersMode = generatorPrototype.CollectionSettersMode,
};

gen.CommentLanguages.Clear();
Expand Down Expand Up @@ -2208,6 +2211,47 @@ 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));
var xml = Serialize(serializer, deserialized);

// assert
Assert.NotNull(xml);
}

[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 c7f5dcd

Please sign in to comment.