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

Inheritable JsonKey with Freezed

Open torbenkeller opened this issue 3 years ago • 6 comments

This issue is related to https://github.com/rrousselGit/freezed/issues/423

Is your feature request related to a problem? Please describe. I'm building an API wrapper, and I'm dealing with two JSON object that have numerous shared keys and only a few specific ones. I have an interface like this to implement this:

abstract class MyBaseType {
  @JsonKey(name: 'property_key')
  final String property;
}

And a couple of freezed classes like this:

@freezed
class MySpecializedType extends MyBaseType with _$MySpecializedType {
  const MySpecializedType._();

  const factory MySpecializedType({
    required String property,
    @JsonKey(name: 'another_propery_key') required String anotherProperty,
  }) = _MySpecializedType
}

Normally, when using json_serializable, the JsonKey annotation in the base class will be used in subclasses. This is not happening when using freezed.

Describe the solution you'd like I'd like JsonKey annotations used in superclasses to be used in freezed subclasses as well.

Describe alternatives you've considered The only alternative is writing the same annotation everywhere it's needed.

torbenkeller avatar May 05 '21 15:05 torbenkeller

This gets complicated, sadly. It's hard to pick a consistent ordering of fields to search first.

kevmoo avatar Jun 07 '21 18:06 kevmoo

related... With latest v5.0.0 (or perhaps I'm just noticing), I'm getting an annotation warning on 'JsonKey' when annotating in a freezed class:

"The annotation 'JsonKey' can only be used on fields or gettersdart(invalid_annotation_target)"

@freezed
class UserProfileDto with _$UserProfileDto {
  factory UserProfileDto({
    @JsonKey(ignore: true) @Default('') String id,
    required String name,
    required String userBio,
    required List<PhotoDto> profilePhotosList,
    required int numPosts,
    required int numMatches,
    @TimestampConverterAuto() required Option<Timestamp> modifyDate,
  }) = _UserProfileDto;

mpiparo avatar Jul 20 '21 19:07 mpiparo

Does freezed use JsonKey?

json_serializabel DOES NOT support annotating constructor params.

kevmoo avatar Jul 20 '21 19:07 kevmoo

No, looks like it just passes the annotation thru to its generated code (below), which is working ok. But what you're saying makes sense, since json_serializable doesn't support annotating on constructors, the warning is valid then. I just hadn't seen that warning before.

Also looks like there's a thread on freezed issues on this topic.

class _$UserProfileDtoTearOff {
  const _$UserProfileDtoTearOff();

  _UserProfileDto call(
      {@JsonKey(ignore: true) String id = '',
      required String name,
      required String userBio,
      required List<PhotoDto> profilePhotosList,
      required int numPosts,
...

mpiparo avatar Jul 20 '21 21:07 mpiparo

The warning IS new as of the latest release of pkg:json_annotation

On Tue, Jul 20, 2021 at 2:54 PM mpiparo @.***> wrote:

No, looks like it just passes the annotation thru to its generated code (below), which is working ok. But what you're saying makes sense, since json_serializable doesn't support annotating on constructors, the warning is valid then. I just hadn't seen that warning before.

Also looks like there's a thread on freezed issues https://github.com/rrousselGit/freezed/issues/476 on this topic.

class _$UserProfileDtoTearOff { const _$UserProfileDtoTearOff();

_UserProfileDto call( {@JsonKey(ignore: true) String id = '', required String name, required String userBio, required List<PhotoDto> profilePhotosList, required int numPosts, ...

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/google/json_serializable.dart/issues/885#issuecomment-883732592, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAEFCRJDLA2BQ2XKDCWQITTYXWDFANCNFSM44FIQDOA .

kevmoo avatar Jul 20 '21 23:07 kevmoo

same issue, Have you found a solution for the warning The annotation 'JsonKey' can only be used on fields or getters

Ngochiendev avatar Apr 29 '24 07:04 Ngochiendev