Improve code coverage for System.Xml.XmlSerializer
According to the current code coverage report, this library is only at ~42%. Our general goal is closer to 90%, though this will depend on the library. We should investigate to see whether the current coverage level is appropriate, and if not, improve our test suites to provide better coverage.
I want to pick up this issue. Can you assign it to me
@taherchhabra, great! I've marked it as grabbed by community.
Was writing test case for XmlAnyElement where I encountered the situation below
public class Program
{
public void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(typeof(Product));
string text = @"<?xml version=""1.0""?>
<Product xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<Name>Iphone</Name>
<Category>smartphone</Category>
</Product>";
StringReader reader = new StringReader(text);
Product product = (Product)serializer.Deserialize(reader);
Console.ReadLine();
}
}
public class Product
{
[XmlAnyElement]
public XmlElement[] AllElements;
}
The above code runs on .net 4.5
but does not run on corefx
gives exception {"To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. System.Xml.XmlElement does not implement Add(System.Object)."} System.Exception {System.InvalidOperationException}
while initializing the XmlSerializer
@davkean?
Yeah, this should work. Owners aren't on git, pinging them offline.
@taherchhabra Thanks for the information. There are some changes in XmlSerializer on corefx that it behaves differently in this case. I'm looking into details.
Am working on this issue (just for learning, am a newbie on corefx). After having setup tests nearing 100% on BinHexDecoder.cs (thought that would be an easy start, I used InternalsVisibleTo) I noticed that BinHexDecoder.cs and is NOT used by the entire project. In the end there is no reference (apart from ExtensionMethods.FromBinHexString, XmlCustomFormatter.ToByteArrayHex and XmlSerializationReader.ToByteArrayHex which I think can be removed) and it is also not called by the test cases even though a class with binhex field is involved (thought maybe only generated code calls it). It appears XmlReader.ReadElementContentAsBinHex is used instead. Or are we missing a test case that triggers it...
Strange though: did the same research for BinHexEncoder.cs. It is also only used in XmlSerializationWriter.FromByteArrayHex (also via XmlCustomFormatter and ExtensionMethods) which is also never directly referenced. Although when I delete it the test case with the binhex fails...
Hm, maybe this was not the easiest place to start a first contribution :-)
Thanks for looking at and working on the issue, @Jacco . The code we have in NetCore were migrated from our Desktop code base. As some APIs are not in NetCore's API contract, I believe some code probably is not being used anywhere. Please feel free to experiment removing the classes/methods that you think are 'dead'.
On the other hand, your hunch was correct. XmlSerializationWriter.FromByteArrayHex was used by generated code, see XmlSerializationWriterILGen.cs#L184 for details. We have other classes/methods are being used in this way.
This is now part of System.Private.Xml contract so the number is not broken out by itself.I could certainly be improved though.
Closing the issue as we've ported many tests for XmlSerializer from full framework to corefx.