google / json_serializable.dart

Generates utilities to aid in serializing to/from JSON.
https://pub.dev/packages/json_serializable
BSD 3-Clause "New" or "Revised" License
1.55k stars 395 forks source link

BUG - 'includeFromJson : false' also excludes from ToJson. #1331

Closed APPXOTICA closed 1 year ago

APPXOTICA commented 1 year ago

This is a test class

@JsonSerializable()
class TestClass {
  TestClass({
    this.a,
    this.b,
  });

  @JsonKey(includeFromJson: false)
  String? a;
  @JsonKey(includeToJson: false)
  String? b;

  factory TestClass.fromJson(Map<String, dynamic> json) => _$TestClassFromJson(json);
  Map<String, dynamic> toJson() => _$TestClassToJson(this);
}

Below is the generated code.

TestClass _$TestClassFromJson(Map<String, dynamic> json) => TestClass(
      b: json['b'] as String?,
    );

Map<String, dynamic> _$TestClassToJson(TestClass instance) =>
    <String, dynamic>{};

includeToJson: false works as expected but includeFromJson : false should not exclude String a from _$TestClassToJson.

Seems like a bug.

json_serializable: ^6.7.0 json_annotation: ^4.8.1

APPXOTICA commented 1 year ago

Post as a new issue