Bug 42843 - XmlSerializer does not deserialize UTC Time values on Xamarin.Android but works well on windows.
Summary: XmlSerializer does not deserialize UTC Time values on Xamarin.Android but wor...
Status: VERIFIED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries (show other bugs)
Version: 6.1.0 (C7)
Hardware: PC Windows
: High critical
Target Milestone: 7.1 (C9)
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2016-07-27 16:38 UTC by Rupa Paramasivan
Modified: 2016-12-27 08:19 UTC (History)
4 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
repro project (30.13 KB, application/zip)
2016-10-06 20:10 UTC, Peter Collins
Details
bxc-42843.zip (1.62 KB, application/zip)
2016-10-13 16:53 UTC, Jonathan Pryor
Details

Description Rupa Paramasivan 2016-07-27 16:38:08 UTC
On calling XmlSerializer.Deserialize on an utc string value it throws and exception in Android if the corresponding DateTime field in the c# class is decorated with the XMLElement attribute of Datatype "time".

Here is a sample piece of unit test code. The second test "DeserializeDateTime_WithUtc()" fails on Android.

using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using NUnit.Framework;


namespace DateTimeDeserialization.Tests
{
    [XmlType(Namespace = "http://somecompany.com/schemas/")]
    [XmlRoot(Namespace = "http://somecompany.com/schemas/", IsNullable = false)]
    public class DateTimeTest
    {
        [XmlType(Namespace = "http://somecompany.com/schemas/")]
        [XmlRoot(Namespace = "http://somecompany.com/schemas/", IsNullable = false)]
        public class SomeTime
        {
            [XmlElement(DataType = "time")]
            public DateTime DateTimeValue { get; set; }
        }

        [XmlElement("SomeTime", typeof(SomeTime), Namespace = "http://somecompany.com/schemas/")]
        public object Item { get; set; }
    }

    [TestFixture]
    public class LedmBaseTypesTest
    {
        private const string TestStringWithoutUtc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dd:DateTimeTest  xmlns:xsi= \"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dd= \"http://somecompany.com/schemas/\" ><dd:SomeTime><dd:DateTimeValue>12:34:56</dd:DateTimeValue></dd:SomeTime></dd:DateTimeTest>";
        private const string TestStringWithUtc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dd:DateTimeTest xmlns:xsi= \"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dd= \"http://somecompany.com/schemas/\" ><dd:SomeTime><dd:DateTimeValue>12:34:56.0Z</dd:DateTimeValue></dd:SomeTime></dd:DateTimeTest>";

        private DateTimeTest DeserializeString(string dataString)
        {
            DateTimeTest retObj = null;
            try
            {
                var data = Encoding.UTF8.GetBytes(dataString);

                using (var strm = new MemoryStream(data))
                {
                    var serializer = new XmlSerializer(typeof(DateTimeTest));
                    retObj = (DateTimeTest)serializer.Deserialize(strm);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return retObj;
        }

        [Test]
        public void DeserializeDateTime_WithoutUtc()
        {
            var obj = DeserializeString(TestStringWithoutUtc);
            Assert.IsNotNull(obj);
            Assert.IsNotNull(obj.Item);
        }

        [Test]
        public void DeserializeDateTime_WithUtc()
        {
            var obj = DeserializeString(TestStringWithUtc);
            Assert.IsNotNull(obj);
            Assert.IsNotNull(obj.Item);
        }

    }
}

The Serializer.Deserialize call throws a FormatException with the message "DeserializeDateTime_WithUtc String was not recognized as a valid DateTime".
This call works without issues on Windows.
The error occurs both on an android emulator running on a windows PC and an android device (Lenovo tablet with Android Version 4.4.4).
Comment 1 Jonathan Pryor 2016-07-27 18:51:31 UTC
@Eno: Was XmlSerializer part of the System.Xml migration work you did?
Comment 2 Atsushi Eno 2016-07-28 02:11:24 UTC
XmlSerializer is NOT part of migration. I'm not the author of that either.
Comment 3 Jonathan Pryor 2016-09-08 16:30:47 UTC
Peter: Could
Comment 4 Marek Safar 2016-09-23 13:37:53 UTC
This looks like some Android specific issue (linker?).

Attached code works with Mono 4.6 with no error.
Comment 5 Rupa Paramasivan 2016-09-29 20:45:01 UTC
We reference Xamarin.Android and don’t directly reference the mono libraries. The mono libraries are not referenced in Visual Studio project templates. Xamarin.Android version 7 internally uses mono 4.6. I can’t confirm that the bug doesn't occur in mono. What I found  though was that this problem occurs also on the latest Xamarin.iOS version 10.0. Not sure if this helps!
Comment 6 Peter Collins 2016-10-06 20:10:50 UTC
Created attachment 17915 [details]
repro project

I'm able to reproduce this using monodroid/master. I've attached a small unit test project which uses the tests outlined in the description.

Logcat output:
https://gist.github.com/pjcollins/aacdf371f619f5cc31711d8a49ac8202

Environment:
MacOS 10.12
monodroid/master/d46688705d
Comment 7 Jonathan Pryor 2016-10-13 16:53:10 UTC
Created attachment 18027 [details]
bxc-42843.zip
Comment 8 Jonathan Pryor 2016-10-13 17:01:26 UTC
@Mareks: This isn't a linker issue. This appears to be a MOBILE issue. See also Comment #5:

> What I found  though was that this problem occurs also on the latest Xamarin.iOS version 10.0

Please see Attachment #18027 [details]:

  $ curl -o bxc-42843.zip https://bugzilla.xamarin.com/attachment.cgi?id=18027
  $ unzip bxc-42843.zip
  $ cd bxc-42843

Build it, overriding the $(MONO_CHECKOUT) make variable appropriately for your mono checkout:

  $ make MONO_CHECKOUT=$HOME/Development/xamarin/mono

The build will fail:

> referencesource/System.Xml/System/Xml/Serialization/CodeIdentifier.cs(26,25): error CS0104: `CodeDomProvider' is an ambiguous reference between `System.CodeDom.Compiler.CodeDomProvider' and `Microsoft.CSharp.CodeDomProvider'

Fix that by editing `mcs/class/referencesource/System.Xml/System/Xml/Serialization/CodeIdentifier.cs` and removing line 17:

  // Remove this line:
  using System.CodeDom.Compiler;

Then build again:

  $ make MONO_CHECKOUT=$HOME/Development/xamarin/mono

The result of the build will be bxc-42843.exe and mobile-bxc-42843.exe. The former is "normal desktop use":

  $ mono bxc-42843.exe
  DateTimeTest
  DateTimeTest+SomeTime
  DateTimeTest
  DateTimeTest+SomeTime

The latter skips the "desktop" System.Xml.dll and instead includes a MOBILE-profile System.Xml within mobile-bxc-42843.exe. Executing it fails:

  $ mono mobile-bxc-42843.exe
> Unhandled Exception:
> System.FormatException: String was not recognized as a valid DateTime.
>   at System.DateTimeParse.ParseExact (System.String s, System.String format, System.Globalization.DateTimeFormatInfo dtfi, System.Globalization.DateTimeStyles style) [0x0002e] in <dca3b561b8ad4f9fb10141d81b39ff45>:0 
> ...

This shows that it's a problem with our MOBILE profile, and not with MOBILE-related features such as the linker.
Comment 9 Marek Safar 2016-10-14 07:13:13 UTC
Fixed in master and Mono 4.8 branch
Comment 10 Shruti 2016-10-14 10:59:15 UTC
************************
Reproduce Status:
************************

I have checked this issue on both MAC and Window environment following by the comment0, comment5 and comment6 with latest Master Builds and observed that this issue still persist. The Serializer.Deserialize call throws a FormatException with the message "DeserializeDateTime_WithUtc String was not recognized as a valid DateTime".

Supplement Info:
Screencast: 
Debug Output:
https://gist.github.com/Shruti360/d5454758a65b1190eb8120741a891a69
Build Output:
https://gist.github.com/Shruti360/ad870ef20f689690507cc0837a4d355a
Device Log:
https://gist.github.com/Shruti360/354670451ada9f7e913f22b584a9abb6
IDE Log:
https://gist.github.com/Shruti360/052cc3a1702d2778f0e55517f4a4c924

Window Environment Info:
https://gist.github.com/Shruti360/5e6388d972f51af7ffa3498130523b20

MAC Environment Info:
https://gist.github.com/sachins360/b19a50254a49621d9da445c26d055a50

Hence, changing the bug status to reopened.

Thanks!
Comment 11 Shruti 2016-10-14 11:00:50 UTC
Screencast in support of comment10
http://www.screencast.com/t/FmnDPqaqG
Comment 12 Marek Safar 2016-10-14 11:01:53 UTC
Please test the issue with builds which contain the fix
Comment 13 Shruti 2016-10-14 12:21:19 UTC
I have rechecked this issue with latest 
MonoFramework-MDK-4.8.0.228.macos10.xamarin.universal_f15d5eb557a5b2e126936ca23919cd71706f7008 of Mono 4.8 branch using the test project attached in comment6 and observed that this issue still persist.

 The Serializer.Deserialize call throws a FormatException with the message "DeserializeDateTime_WithUtc String was not recognized as a valid DateTime".

Supplement Info:
Screencast: http://www.screencast.com/t/kaRoXdPx

Environment Info and IDE Log:
https://gist.github.com/shrutis360/7cdbbf5e01f907504cfd49bcc2e71ff7

@Marek, I am observing the issue on both MAC as well as Windows environment. It would be great if you specify the build version in which the fix of this issue is merged.


Thanks!
Comment 14 Marek Safar 2016-10-14 12:25:15 UTC
As this is Android issue you need XA which includes the fix and as XA hasn't branched for C9 yet I don't think there is any.
Comment 15 Shruti 2016-12-27 08:19:15 UTC
************************
Verification Status:
************************

I have checked this issue on both MAC and Window environment following by the comment0, comment5, comment6 and comment14 with latest Cycle 9 and Master Builds and observed that this issue is fixed. The Serializer.Deserialize call doesn't throws a FormatException anymore and DeserializeDateTime_WithUtc and WithoutUTC tests result are "passed".

=====Supplement Info=====
Screencast: https://www.screencast.com/t/FEoffQfUP
Build Output: https://gist.github.com/Shruti360/72f8d0f7833a4fb901e02a1cdfcec9fd
Debug Output: https://gist.github.com/Shruti360/d95aac0bb2f029a8ac249cc60e3ba30c
IDE Log:     https://gist.github.com/Shruti360/9d3907ea82b13ae8625a0bcb61b244ca

====Cycle 9 Build Info=====
*Window Envt Info: https://gist.github.com/Shruti360/3147ae9dfccef456d2d919fd16954f46

*MAC Envt Info and IDE Logs:
https://gist.github.com/shrutis360/1e9424dedb63a39741433c8f44a1957f

=====Master Build Info=====
*Window Envt Info: https://gist.github.com/Shruti360/000c34709e55d8b84c99827814aaa642

*MAC Envt Info and IDE Logs: 
https://gist.github.com/shrutis360/4707b7713ffcadc1c52cc7759f20669c

Hence, marking this issue as verified fixed.

Thanks!

Note You need to log in before you can comment on or make changes to this bug.