google / google/json_serializable.dart

Bug when using `@JsonSerializable(createFactory: false)` on an `Equatable` class.

Open
#1,348 11 comments 13 reactions 0 assignees View on GitHub
Type: enhancement
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.