Jaguar-dart / jaguar_serializer

Format (JSON, XML, protobuf, mongodb, etc) and platform (server, client) agnostic serialization framework
172 stars 34 forks source link

fix for `bool cannot be null`exception #183

Closed escamoteur closed 4 years ago

escamoteur commented 5 years ago

I got an bool cannot be null Exception the moment I did a Field() annotation. In my case I wanted to add a processor.

The source of the exception was the code in instantiater.dart from line 195:

      String encodeTo = name;
      String decodeFrom = name;
      bool nullable = globalNullable;
      FieldProcessorInfo processor;
      if (annot != null) {
        dontEncode =
            annot.getField('dontEncode').toBoolValue() ? true : dontEncode;   <- here came the exception
        dontDecode =
            annot.getField('dontDecode').toBoolValue() ? true : dontDecode;

        encodeTo = annot.getField('encodeTo')?.toStringValue() ?? encodeTo;
        decodeFrom =
            annot.getField('decodeFrom')?.toStringValue() ?? decodeFrom;

        nullable = annot.getField('isNullable').toBoolValue() ?? nullable;
        processor = _parseFieldProcessor(annot.getField('processor'));
      }

I wonder how this could have worked in the last time at all because if anno did not contain dontEncode and dontEncode you always would get this exception because null.toBoolValue == null my fix now is this here:

      if (annot != null) {
        dontEncode =
            annot.getField('dontEncode').toBoolValue() ?? false ? true : dontEncode;
        dontDecode =
            annot.getField('dontDecode').toBoolValue() ?? false ? true : dontDecode;

        encodeTo = annot.getField('encodeTo')?.toStringValue() ?? encodeTo;
        decodeFrom =
            annot.getField('decodeFrom')?.toStringValue() ?? decodeFrom;

I guess this could made more readable if we give up some of the concise format.

Cheers Thomas