runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Improve code coverage for System.Xml.XmlSerializer

Open stephentoub opened this issue 11 years ago • 10 comments

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.

stephentoub avatar Mar 21 '15 00:03 stephentoub

I want to pick up this issue. Can you assign it to me

taherchhabra avatar Mar 23 '15 09:03 taherchhabra

@taherchhabra, great! I've marked it as grabbed by community.

stephentoub avatar Mar 23 '15 15:03 stephentoub

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

taherchhabra avatar Mar 24 '15 21:03 taherchhabra

@davkean?

stephentoub avatar Mar 24 '15 22:03 stephentoub

Yeah, this should work. Owners aren't on git, pinging them offline.

davkean avatar Mar 25 '15 02:03 davkean

@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.

khdang avatar Mar 25 '15 17:03 khdang

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 :-)

Jacco avatar Jun 22 '16 19:06 Jacco

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.

shmao avatar Jun 23 '16 17:06 shmao

This is now part of System.Private.Xml contract so the number is not broken out by itself.I could certainly be improved though.

danmoseley avatar Feb 21 '17 05:02 danmoseley

Closing the issue as we've ported many tests for XmlSerializer from full framework to corefx.

shmao avatar Nov 02 '17 16:11 shmao