Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
chwarr opened this issue Apr 12, 2017 · 0 comments
Labels

Comments

@chwarr
Copy link
Member

chwarr commented Apr 12, 2017

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
Labels
Projects
None yet
Development

No branches or pull requests

1 participant