__type being applied to non-null nullable generics
Closed this issue · 5 comments
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?
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.
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!
Already landed in 4.2.2 😁✌️
Amazing!