Skip to content

includeToJson: false is not working properly #1280

New issue

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

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

Already on GitHub? # to your account

Closed
mernen opened this issue Feb 1, 2023 · 2 comments · Fixed by #1281
Closed

includeToJson: false is not working properly #1280

mernen opened this issue Feb 1, 2023 · 2 comments · Fixed by #1281

Comments

@mernen
Copy link

mernen commented Feb 1, 2023

Given the following class:

@JsonSerializable(createFactory: false, createToJson: true)
class SerializationTest {
  SerializationTest(this.x, this.y);

  final int x;
  @JsonKey(includeToJson: false)
  final int y;

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

I would expect the generated serializer to only include x. However, this is the produced function:

Map<String, dynamic> _$SerializationTestToJson(SerializationTest instance) =>
    <String, dynamic>{
      'x': instance.x,
      'y': instance.y,
    };

Also serializing y.

y disappears if we change the annotation to includeFromJson: false.

I believe the culprit is this line:

bool get explicitNoToJson => includeFromJson == false;

It should test for includeToJson == false.


Environment:

dependencies:
  json_annotation: ^4.8.0

dev_dependencies:
  build_runner: ^2.3.3
  json_serializable: ^6.6.0
$ dart --version
Dart SDK version: 2.19.0-444.2.beta (beta) (Thu Dec 15 17:12:16 2022 +0000) on "macos_arm64"
@aytunch
Copy link

aytunch commented Feb 4, 2023

This is not an answer but in your pubspec.yaml, you put json_serializable under dev_dependencies instead of dependencies
However in the installation section it says json_serializable should be under dependencies
https://pub.dev/packages/json_serializable/install

Does it make any difference? What is the correct convention? Some clarification would be highly appreciated.
Thanks

@kevmoo
Copy link
Collaborator

kevmoo commented Feb 6, 2023

Hey @aytunch – that's a bug on pub.dev. I'll create an issue!

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants