schultek / dart_mappable

Improved json serialization and data classes with full support for generics, inheritance, customization and more.
https://pub.dev/packages/dart_mappable
MIT License
135 stars 20 forks source link

__type being applied to non-null nullable generics #181

Closed FufferKS closed 3 months ago

FufferKS commented 3 months ago

Hi!

I have the following core class:

@MappableClass()
class Snapshot<T> with SnapshotMappable<T> {
  const Snapshot({
    required this.data,
    required this.source,
  });

  final T data;
  final String source;
}

and this business one:

@MappableClass()
class CreditCard with CreditCardMappable {
  const CreditCard({
    required this.apr,
    });
    final Snapshot<Decimal?> apr;
}

In runtime, at one point apr is being assigned a Snapshot<Decimal> - not null. This causes my json request to include "__type": "Snapshot<Decimal>" which seems redundant.

Is there an option to ignore it?

schultek commented 3 months ago

I assume you do creditCard.toMap() or .toJson().

To fix this right now you can do CreditCardMapper.ensureInitialized().encodeMap(creditCard, EncodingOptions(includeTypeId: false))); or .encodeJson(...)

The __type is added in certain cases where it is needed so that x == X.fromMap(x.toMap()). However this should disregard type mismatches that only differ in nullability. So I consider this a bug and work on a fix.

FufferKS commented 3 months ago

Thanks for the reply. I am encoding to json, but I'm using a mix of freezed and mappable (slowly migrating to your solution), plus the credit card is one of many types of products, so I don't see an easy way to pass those exclusions. It doesn't hurt that bad, so I will wait for the fix. Thanks!

schultek commented 3 months ago

Already landed in 4.2.2 😁✌️

FufferKS commented 3 months ago

Amazing!

FufferKS commented 3 months ago

image 🙌