Skip to content

Commit

Permalink
Handle nullable primitives before passing them to WritePrimitive whic…
Browse files Browse the repository at this point in the history
…h expects non-null values. (#54800)
  • Loading branch information
StephenMolloy authored Jul 13, 2021
1 parent 92ed400 commit 88112bc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,17 @@ private void WriteElement(object? o, ElementAccessor element, bool writeAccessor
{
WriteQualifiedNameElement(name, ns!, element.Default, (XmlQualifiedName)o!, element.IsNullable, mapping.IsSoap, mapping);
}
else if (o == null && element.IsNullable)
{
if (mapping.IsSoap)
{
WriteNullTagEncoded(element.Name, ns);
}
else
{
WriteNullTagLiteral(element.Name, ns);
}
}
else
{
WritePrimitiveMethodRequirement suffixNullable = mapping.IsSoap ? WritePrimitiveMethodRequirement.Encoded : WritePrimitiveMethodRequirement.None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ public static void Xml_ByteArrayAsRoot()
Assert.Equal(x, y);
}

[Fact]
public static void Xml_ByteArrayNull()
{
Assert.Null(SerializeAndDeserialize<byte[]>(null,
@"<?xml version=""1.0""?>
<base64Binary d1p1:nil=""true"" xmlns:d1p1=""http://www.w3.org/2001/XMLSchema-instance"" />"));
byte[] x = new byte[] { 1, 2 };
byte[] y = SerializeAndDeserialize<byte[]>(x,
@"<?xml version=""1.0""?>
<base64Binary>AQI=</base64Binary>");
Assert.Equal(x, y);
}

[Fact]
public static void Xml_CharAsRoot()
{
Expand Down Expand Up @@ -1279,6 +1292,19 @@ public static void XML_TypeWithByteArrayAsXmlAttribute()
Assert.True(Enumerable.SequenceEqual(value.XmlAttributeForms, actual.XmlAttributeForms));
}

[Fact]
public static void XML_TypeWithNullableByteArray()
{
var value = new TypeWithNullableByteArray(); // XmlAttributeForms == null

var actual = SerializeAndDeserialize(value,
"<?xml version=\"1.0\"?>\r\n<MyXmlType xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n <XmlAttributeForms xsi:nil=\"true\" />\r\n</MyXmlType>");

Assert.NotNull(actual);
Assert.Null(value.XmlAttributeForms);
Assert.Null(actual.XmlAttributeForms);
}

[Fact]
public static void XML_TypeWithByteArrayArrayAsXmlAttribute()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,13 @@ public class TypeWithByteArrayAsXmlAttribute
public byte[] XmlAttributeForms;
}

[XmlType(TypeName = "MyXmlType")]
public class TypeWithNullableByteArray
{
[XmlElement(DataType = "base64Binary", IsNullable = true)]
public byte[] XmlAttributeForms { get; set; }
}

[XmlType(TypeName = "MyXmlType")]
public class TypeWithByteArrayArrayAsXmlAttribute
{
Expand Down

0 comments on commit 88112bc

Please sign in to comment.