-
Notifications
You must be signed in to change notification settings - Fork 402
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
Bug report - @JsonKey(includeFromJson: false) malfunctions #1334
Comments
Note the docstring: json_serializable.dart/json_annotation/lib/src/json_key.dart Lines 91 to 93 in 2185e8b
The current behaviour can be summarised by these test cases: ToJsonNullFromJsonFalsePublic and ToJsonFalseFromJsonNullPublic Summary:
I take json_serializable.dart/json_annotation/lib/src/json_key.dart Lines 41 to 44 in 2185e8b
json_serializable.dart/json_annotation/lib/src/json_key.dart Lines 137 to 140 in 2185e8b
To me it sounds like we only care about the to-from round-trip and that line 42 is a mistake (it should be replaced by the contents of line 138). Maybe this is an incorrect assumption, given that these docstrings have survived this long without modification. In either case, it seems that the current behaviour can break if we attempt a to-from round trip, as described above: @JsonSerializable()
class TestClass {
TestClass({
required this.a,
});
@JsonKey(includeToJson: false)
String a;
factory TestClass.fromJson(Map<String, dynamic> json) =>
_$TestClassFromJson(json);
Map<String, dynamic> toJson() => _$TestClassToJson(this);
}
void main() {
final json = TestClass(a: 'aaa').toJson();
final test = TestClass.fromJson(json);
} Generated code: TestClass _$TestClassFromJson(Map<String, dynamic> json) => TestClass(
a: json['a'] as String,
);
Map<String, dynamic> _$TestClassToJson(TestClass instance) =>
<String, dynamic>{};
Dependency versions:
|
If my assumption is correct, I propose we correct the docs to only refer to a to-from round-trip, and fix the current behaviour when attempting this round trip. Also, I see no reason to add to the docstrings for References:
|
You may have to specify explicitly
|
I encountered the same (for me strange) behavior. Why is field excluded from "toJson" method when I set "includeFromJson: false"? I don't see any reason for this.
So "includeToJson: true" must be explicitly set when using "includeFromJson: false" |
Here's a test class
Generated code.
TestClassToJson
should includeString a
but it doesn't. It excludesString a
fromTestClassFromJson
andTestClassToJson
both.json_serializable: ^6.7.0
json_annotation: ^4.8.1
The text was updated successfully, but these errors were encountered: