google / google/json_serializable.dart
Bug when using `@JsonSerializable(createFactory: false)` on an `Equatable` class.
- Dominant language
- Dart
- Stars
- 1.6k
- Forks
- 461
- Avg merge
- 45m
- Merged PRs (30d)
- 1
Description
Using `json_serializable: ^6.7.1` on a model class which extends `Equatable` leads to unwanted fields being included in generated code.
Given:
```dart
@JsonSerializable(createFactory: false) // <- notice createFactory being false
@immutable
class User extends Equatable {
const User({
required this.id,
required this.name,
required this.username,
required this.password,
});
final String id;
final String name;
final String username;
final String password;
@override
List get props => [id, name, username, password];
}
```
the output is
```dart
Map _$UserToJson(User instance) {
final val = {};
void writeNotNull(String key, dynamic value) {
if (value != null) {
val[key] = value;
}
}
writeNotNull('stringify', instance.stringify); // <- should not be included
val['hashCode'] = instance.hashCode; // <- should not be included
val['id'] = instance.id;
val['name'] = instance.name;
val['username'] = instance.username;
val['password'] = instance.password;
val['props'] = instance.props; // <- should not be included
return val;
}
```
with multiple unwanted fields being included in the json output.
Hint: When using `@JsonSerializable()` or `@JsonSerializable(createToJson: false)` all works as expected.
Contributor guide
Assessment
This issue has not been assessed yet.