Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not using the provided @JsonKey annotation on the generated code #58

Closed
apiep opened this issue Feb 21, 2020 · 3 comments
Closed

Not using the provided @JsonKey annotation on the generated code #58

apiep opened this issue Feb 21, 2020 · 3 comments
Labels
question Further information is requested

Comments

@apiep
Copy link

apiep commented Feb 21, 2020

For example I have the following code:

@freezed
@immutable
abstract class QAccount1 with _$QAccount1 {
  factory QAccount1({
    @JsonKey(name: 'email') @required String id,
    @JsonKey(name: 'username') String name,
    @JsonKey(name: 'avatar_url') String avatarUrl,
    @JsonKey(name: 'extras', nullable: true) Map<String, dynamic> extras,
    @JsonKey(name: 'last_comment_id') int lastMessageId,
    @JsonKey(name: 'last_sync_event_id') int lastEventId,
  }) = _QAccount1;

  factory QAccount1.fromJson(Map<String, dynamic> json) =>
      _$QAccount1FromJson(json);
}

Freezed should generate:

// This is taken from json_serializable
QAccount1 _$QAccountFromJson(Map<String, dynamic> json) {
  return QAccount1(
    id: json['email'] as String,
    name: json['username'] as String,
    avatarUrl: json['avatar_url'] as String,
    extras: json['extras'] as Map<String, dynamic>,
    lastMessageId: json['last_comment_id'] as int,
    lastEventId: json['last_sync_event_id'] as int,
  );
}

but the actual generated code are:

_$_QAccount1 _$_$_QAccount1FromJson(Map<String, dynamic> json) {
  return _$_QAccount1(
    id: json['id'] as String,
    name: json['name'] as String,
    avatarUrl: json['avatarUrl'] as String,
    extras: json['extras'] as Map<String, dynamic>,
    lastMessageId: json['lastMessageId'] as String,
    lastEventId: json['lastEventId'] as String,
  );
}

As you can see, generated code still using id instead of email

@rrousselGit
Copy link
Owner

Are you sure that your generation is up to date? This works on my side

@rrousselGit rrousselGit added the question Further information is requested label Feb 21, 2020
@rrousselGit
Copy link
Owner

Here's the test I made:

  test('QAccount1', () {
    expect(
      QAccount1(
        id: '42',
        avatarUrl: 'url',
        extras: <String, dynamic>{'a': 42},
        name: 'name',
        lastMessageId: 42,
        lastEventId: 21,
      ).toJson(),
      {
        'email': '42',
        'avatar_url': 'url',
        'extras': {'a': 42},
        'username': 'name',
        'last_comment_id': 42,
        'last_sync_event_id': 21
      },
    );
  });

@apiep
Copy link
Author

apiep commented Feb 22, 2020

Ah yeah, seems like I got interested on freezed after reading a tutorial about it, and just copy paste from the tutorial without checking the latest version.
Thanks.

@apiep apiep closed this as completed Feb 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants