json_serializable.dart icon indicating copy to clipboard operation
json_serializable.dart copied to clipboard

JsonKey annotation in superclass is not being used for overriden getters

Open hacker1024 opened this issue 3 years ago • 3 comments

Consider a structure like the following:

abstract class MyBaseClass {
  @JsonKey(name: 'myCustomGetterName');
  String get myGetter;

  @JsonKey(name: 'myCustomFieldName');
  final String myField;
}

@JsonSerializable(createFactory: false)
class MySuperclass implements MyBaseClass {
  @override
  String get myGetter => 'myGetterValue';

  @override
  final String myField = 'myFieldValue';

  Map<String, dynamic> toJson() => _$MySuperclassToJson(this);
}

I'd expect to see a JSON result like this:

{
  "myCustomGetterName": "myGetterValue",
  "myCustomFieldName": "myFieldValue"
}

Instead, the getter name isn't inherited, and I get this:

{
  "myGetter": "myGetterValue",
  "myCustomFieldName": "myFieldValue"
}

hacker1024 avatar May 11 '21 12:05 hacker1024

@kevmoo any updates on this?

emme1444 avatar Aug 27 '21 19:08 emme1444

Sadly, @emme1444 – there's no good way to fix this without potentially breaking folks due to a change in behavior.

We'll have to wait until the next breaking release.

kevmoo avatar Oct 06 '21 02:10 kevmoo

This is maybe related with #1102

mdeandrea-mrmilu avatar Apr 07 '22 06:04 mdeandrea-mrmilu