Bug 17663 - [System.Runtime.Serialization][patch] DataContractSerializer does not correctly serialize System.Guid or System.Char
Summary: [System.Runtime.Serialization][patch] DataContractSerializer does not correct...
Alias: None
Product: Class Libraries
Classification: Mono
Component: WCF assemblies ()
Version: master
Hardware: PC Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Atsushi Eno
Depends on:
Reported: 2014-02-07 20:24 UTC by Brendan Zagaeski (Xamarin Team, assistant)
Modified: 2014-02-24 21:16 UTC (History)
2 users (show)

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

Test case (2.25 KB, application/zip)
2014-02-07 20:24 UTC, Brendan Zagaeski (Xamarin Team, assistant)
Proposed patch (1.97 KB, patch)
2014-02-07 20:33 UTC, Brendan Zagaeski (Xamarin Team, assistant)
Better patch (1.01 KB, patch)
2014-02-08 23:34 UTC, Brendan Zagaeski (Xamarin Team, assistant)
Proposed tests (1.89 KB, patch)
2014-02-23 20:38 UTC, Brendan Zagaeski (Xamarin Team, assistant)

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Brendan Zagaeski (Xamarin Team, assistant) 2014-02-07 20:24:36 UTC
Created attachment 6002 [details]
Test case

## Steps to reproduce

1. Compile and run the attached test project using Mono.
2. Compile and run the attached test project using .NET.

## Results

### Excerpt from .NET output
<a:anyType i:type="b:guid" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/">00000000-0000-0000-0000-000000000000</a:anyType>

### Excerpt from Mono output
<d2p1:anyType xmlns:d3p1="http://www.w3.org/2001/XMLSchema" i:type="d3p1:guid">00000000-0000-0000-0000-000000000000</d2p1:anyType>

## Problem

`System.Guid` does _not_ map to an XML Schema (XSD) primitive type. See for example [1, 2]. Similarly, `System.Char` does not map to an XML Schema primitive type. Therefore, in both cases the appropriate namespace is actually:
> http://schemas.microsoft.com/2003/10/Serialization/

... rather than:
> http://www.w3.org/2001/XMLSchema

> [1] The "Type/primitive mapping" section on http://msdn.microsoft.com/en-us/library/ms733112.aspx
> [2] http://www.w3.org/TR/2012/REC-xmlschema11-2-20120405/datatypes.html#built-in-primitive-datatypes
Comment 1 Brendan Zagaeski (Xamarin Team, assistant) 2014-02-07 20:33:41 UTC
Created attachment 6003 [details]
Proposed patch

This patch changes the logic in XmlFormatterSerializer.Serialize(). It now checks the `qname.Name` against the list of XSD primitive type names, taken from [1].

> [1] http://msdn.microsoft.com/en-us/library/ms733112.aspx

This fixes the test case, and also the `System.Char` case. It also does not break the serialization of types that _do_ map to XSD primitive types, for example `System.Int32`.
Comment 2 Brendan Zagaeski (Xamarin Team, assistant) 2014-02-08 23:34:50 UTC
Created attachment 6010 [details]
Better patch

My original patch was wasteful. The list of XSD primitive types is already contained once in the private `KnownTypeCollection.xs_predefined_types` field, and also a couple of times in the System.Xml.Schema namespace.

This new patch uses the public, static `XmlSchemaType.GetBuiltInSimpleType()` method [1] to check if the `qname.Name` can be matched to a "simple type" within the `XmlSchema.Namespace` namespace.

[1] http://msdn.microsoft.com/en-us/library/1c4sky9s.aspx
Comment 3 Miguel de Icaza [MSFT] 2014-02-11 21:48:45 UTC
Atsushi, could you review this patch and approve if OK?
Comment 4 Atsushi Eno 2014-02-12 03:35:34 UTC
The change is looking good.

(Though since it's fairly doable we'd want a test case. Maybe Test/System.Runtime.Serialization/XmlObjectSerializerTest.cs has similar ones that helps writing one for this case.)
Comment 5 Brendan Zagaeski (Xamarin Team, assistant) 2014-02-23 20:38:25 UTC
Created attachment 6126 [details]
Proposed tests

Sounds good. Here are two tests, one each for Guid and Char. The tests fail before the patch for `XmlFormatterSerializer.Serialize()` has been applied, but succeed after.

The tests also pass on Windows.
Comment 6 Atsushi Eno 2014-02-23 23:34:28 UTC
Oh I thought you already committed your patch to mono. You can do it.
Comment 7 Miguel de Icaza [MSFT] 2014-02-24 21:16:45 UTC

Applied patch and tests

Thank you Brendan!