Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Generation of C# serializer for a = nothing field with a default value fails with an obscure exception #413

Open

Description

If I have a C# class that has a nullable field and this field has a default value other than null, serialization fails with a very obscure error.

namespace NullableDefValueBug
{
    using Bond;
    using Bond.IO.Safe;
    using Bond.Protocols;

    [Schema]
    public class NullableWithDefault
    {
        [Id(0)] [Type(typeof(Bond.Tag.nullable<int>))] public int? ni = 100;
    }

    class Program
    {
        static void Main(string[] args)
        {
            var obj = new NullableWithDefault();

            var ser = new Serializer<CompactBinaryWriter<OutputBuffer>>(typeof(NullableWithDefault));
         }
    }
}

The error says "The binary operator NotEqual is not defined for the types 'System.Nullable`1[System.Int32]' and 'System.Int32'." An error more like "A nullable field cannot have a default value other than null" is more expected.

Unhandled Exception: 
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> 
System.InvalidOperationException: The binary operator NotEqual is not defined for the types 'System.Nullable`1[System.Int32]' and 
'System.Int32'.
   at System.Linq.Expressions.Expression.GetEqualityComparisonOperator(ExpressionType binaryType, String opName, Expression left, Expression 
right, Boolean liftToNull)
   at System.Linq.Expressions.Expression.NotEqual(Expression left, Expression right, Boolean liftToNull, MethodInfo method)
   at Bond.Expressions.ObjectParser.Field(ITransform transform, Expression structVar, UInt16 id, ISchemaField schemaField, IField field) in 
S:\bondlab\nuget\bond\cs\src\core\expressions\ObjectParser.cs:line 136
   at Bond.Expressions.ObjectParser.<>c__DisplayClass21_0.<Apply>b__4(<>f__AnonymousType0`2 <>h__TransparentIdentifier0, IField knownField) 
in S:\bondlab\nuget\bond\cs\src\core\expressions\ObjectParser.cs:line 88
   at System.Linq.Enumerable.<SelectManyIterator>d__22`3.MoveNext()
   at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
   at Bond.Expressions.ObjectParser.Apply(ITransform transform) in S:\bondlab\nuget\bond\cs\src\core\expressions\ObjectParser.cs:line 84
   at Bond.Expressions.SerializerTransform`2.Struct(IParser parser, RuntimeSchema schema, Boolean isBase) in 
S:\bondlab\nuget\bond\cs\src\core\expressions\SerializerTransform.cs:line 173
   at Bond.Expressions.SerializerTransform`2.Struct(IParser parser, RuntimeSchema schema) in 
S:\bondlab\nuget\bond\cs\src\core\expressions\SerializerTransform.cs:line 165
   at Bond.Expressions.SerializerTransform`2.<>c__DisplayClass12_0.<GenerateSerialize>b__0(IParser p) in 
S:\bondlab\nuget\bond\cs\src\core\expressions\SerializerTransform.cs:line 146
   at Bond.Expressions.SerializerGenerator`2.GenerateSerialize(Serialize serialize, IParser parser, ParameterExpression writer, Boolean 
inline) in S:\bondlab\nuget\bond\cs\src\core\expressions\SerializerTransform.cs:line 77
   at Bond.Expressions.SerializerTransform`2.GenerateSerialize(SerializeWithSchema serializeWithSchema, IParser parser, RuntimeSchema 
schema) in S:\bondlab\nuget\bond\cs\src\core\expressions\SerializerTransform.cs:line 155
   at Bond.Expressions.SerializerTransform`2.Generate(IParser parser) in 
S:\bondlab\nuget\bond\cs\src\core\expressions\SerializerTransform.cs:line 125
   at Bond.Serializer`1.SerializerHelper..ctor(ObjectParser parser, Type type, Boolean inlineNested) in 
S:\bondlab\nuget\bond\cs\src\core\Serializer.cs:line 140
   at Bond.Serializer`1.TwoPassSerializerHelper`1..ctor(ObjectParser parser, Type type, Boolean inlineNested) in 
S:\bondlab\nuget\bond\cs\src\core\Serializer.cs:line 0
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] 
activationAttributes, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] 
activationAttributes)
   at System.Activator.CreateInstance(Type type, Object[] args)
   at Bond.Serializer`1..ctor(Type type, IParser parser, Boolean inlineNested) in S:\bondlab\nuget\bond\cs\src\core\Serializer.cs:line 117
   at Bond.Serializer`1..ctor(Type type) in S:\bondlab\nuget\bond\cs\src\core\Serializer.cs:line 91
   at NullableDefValueBug.Program.Main(String[] args) in C:\Users\chwarr\documents\visual studio 
2015\Projects\Playground\CsPlayground\Program.cs:line 19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions