silentorbit / protobuf

C# code generator for reading and writing the protocol buffers format
https://silentorbit.com/protobuf/
MIT License
307 stars 105 forks source link

Non-unique XML names #11

Closed DaTeL237 closed 9 years ago

DaTeL237 commented 11 years ago

XmlSerializer cannot always be created with the generated System.Serializable attributes.

An InvalidOperationException is raised when creating an XmlSerializer with an inner exception with message:

{"Types 'Example.MessageTwo.SubType' and 'Example.MessageOne.SubType' both use the XML type name, 'SubType', from namespace ''. Use XML attributes to specify a unique XML name and/or namespace for the type."}

The following .proto definition and C# code reproduce the exception:

.proto definition:

message MessageOne {  
  enum SubType {
    typeOneA = 1;
    typeOneB = 2;
  }

  optional SubType subType = 1;
}

message MessageTwo {
  enum SubType {
    typeTwoA = 1;
    typeTwoB = 2;
  }

  optional SubType subType = 1;
}

C# code:

using System;
using System.Xml;
using Example; // This is where my generated code went

namespace SerializeTest
{
    public class SerializeTest
    {
        [STAThread]
        static void Main(string[] args)
        {
            Type[] types = new Type[] { typeof(MessageOne), typeof(MessageTwo) };
            var serializer = new System.Xml.Serialization.XmlSerializer(typeof(string), types);
        }
    }
}
hultqvist commented 11 years ago

Have you tried adding any xml attributes to get it working?

DaTeL237 commented 11 years ago

When I add the following XML attributes to the SubType definitions (not the field definitions) it works ok:

        [System.Xml.Serialization.XmlType(TypeName = "SubType", Namespace = "Example.MessageOne")]
        public enum SubType
(..)
        [System.Xml.Serialization.XmlType(TypeName = "SubType", Namespace = "Example.MessageTwo")]
        public enum SubType
hultqvist commented 11 years ago

Ok, one solution could be to add Namespace with the Matching the c# namespace including the class hierarchy

With this I will also make XmlSerialization an optional command line flag so it won't be included by default.

On 2013-06-10 16:31, DaTeL237 wrote:

When I add the following XML attributes to the SubType definitions (not the field definitions) it works ok:

[System.Xml.Serialization.XmlType(TypeName = "SubType", Namespace = "Example.MessageOne")] public enum SubType (..) [System.Xml.Serialization.XmlType(TypeName = "SubType", Namespace = "Example.MessageTwo")] public enum SubType

— Reply to this email directly or view it on GitHub https://github.com/hultqvist/ProtoBuf/issues/11#issuecomment-19202059.

hultqvist commented 9 years ago

I won't work on this myself but if anyone else starts working to fix this I will help to integrate it into working code.