Bug 12939 - DataContractSerializer writes invalid namespace for Guid value declared as object
Summary: DataContractSerializer writes invalid namespace for Guid value declared as ob...
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: WCF assemblies (show other bugs)
Version: master
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-06-28 09:55 UTC by foka
Modified: 2013-11-28 00:40 UTC (History)
3 users (show)

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


Attachments
Test app (672 bytes, text/plain)
2013-06-28 09:55 UTC, foka
Details

Description foka 2013-06-28 09:55:46 UTC
Created attachment 4223 [details]
Test app

DataContractSerializer writes invalid namespace for Guid value declared as object.
Mono writes: http://www.w3.org/2001/XMLSchema
Windows runtime writes: http://schemas.microsoft.com/2003/10/Serialization/

Attached app outputs on Mono:

<?xml version="1.0" encoding="utf-16"?>
<Foo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WcfClient">
        <GuidBar xmlns:d2p1="http://www.w3.org/2001/XMLSchema" i:type="d2p1:guid">46c41296-2896-4371-9b7d-aa8db40ae235</GuidBar>
        <StringBar xmlns:d2p1="http://www.w3.org/2001/XMLSchema" i:type="d2p1:string">Whatever</StringBar>
</Foo>

On Windows (expected):

<?xml version="1.0" encoding="utf-16"?>
<Foo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WcfServer.Contract">
        <GuidBar xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/" i:type="d2p1:guid">7954b06c-f3a0-42f4-bd01-e35044350750</GuidBar>
        <StringBar xmlns:d2p1="http://www.w3.org/2001/XMLSchema" i:type="d2p1:string">Whatever</StringBar>
</Foo>

Build command line: mcs Program.cs -r:System.Runtime.Serialization.dll

In my production code I use object[] array which contains objects of various types: string, int etc and guid. So object serialized on mono server brakes during deserialization on windows client.

Tested on trunk 3.0.11, SHA-1 a97e686783f0a39dd253a9071c2562a0bc14d207
Comment 1 foka 2013-07-01 04:10:05 UTC
I made a mistake in the description.
On Windows Foo's namespace is obviously the same as on Mono: xmlns="http://schemas.datacontract.org/2004/07/WcfClient".
Comment 2 foka 2013-07-01 05:55:35 UTC
I've debugged process of serialization.
There's a bug either in XmlFormatterSerializer or KnownTypeCollection.

In XmlFormatterSerializer.Serialize(Type, object) this fragment is hit for guid sent in Object property:

if (explicityType && (map == null || map.OutputXsiType)) {
	QName qname = resolvedQName ?? types.GetXmlName (actualType);
	string name = qname.Name;
	string ns = qname.Namespace;
	if (qname == QName.Empty) {
		name = XmlConvert.EncodeLocalName (actualType.Name);
		ns = KnownTypeCollection.DefaultClrNamespaceBase + actualType.Namespace;
	} else if (qname.Namespace == KnownTypeCollection.MSSimpleNamespace)
		ns = XmlSchema.Namespace;
	if (writer.LookupPrefix (ns) == null) // it goes first (extraneous, but it makes att order compatible)
		writer.WriteXmlnsAttribute (null, ns);
	writer.WriteStartAttribute ("i", "type", XmlSchema.InstanceNamespace);
	writer.WriteQualifiedName (name, ns);
	writer.WriteEndAttribute ();
}

At first, KnownTypeCollection returns QName right namespace: "http://schemas.microsoft.com/2003/10/Serialization/" (statement: types.GetXmlName (actualType))
But then it's replaced with wrong namespace "http://www.w3.org/2001/XMLSchema" in fragment:
	} else if (qname.Namespace == KnownTypeCollection.MSSimpleNamespace)
		ns = XmlSchema.Namespace;
Comment 3 foka 2013-07-01 10:27:20 UTC
This fixes the bug:
https://github.com/foka/mono/commit/1d1cdde5bfd75943990632a2722ecf8d438ff411

As this is just a shitty quick fix, I strongly believe it can be done much better.

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