Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions json_serializable/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Allow `run_only_if_triggered` to be specified in `build.yaml` to turn on the
`build_runner` triggers heuristic.
- Skip warning about annotations on both constructor and field if the values
match.

## 6.11.1

Expand Down
4 changes: 3 additions & 1 deletion json_serializable/lib/src/json_key_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ KeyConfig _from(FieldElement2 element, ClassConfig classAnnotation) {
if (ctorObj != null && !ctorObj.isNull) {
final ctorReadResult = ctorObj.read(field);
if (!ctorReadResult.isNull) {
if (!obj.isNull && !obj.read(field).isNull) {
final fieldReadResult = obj.isNull ? null : obj.read(field);
if (fieldReadResult != null &&
fieldReadResult.objectValue != ctorReadResult.objectValue) {
log.warning(
'Field `${element.name3}` has conflicting `JsonKey.$field` '
'annotations: both constructor parameter and class field have '
Expand Down
14 changes: 12 additions & 2 deletions json_serializable/test/src/extends_jsonkey_override.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ class CtorParamJsonKey {

@JsonKey(name: 'fake_first')
final String attributeOne;

// Duplicate annotation with exact same value does not give a warning.
@JsonKey(name: 'second')
final String attributeTwo;
}

@ShouldGenerate(r'''
@ShouldGenerate(
r'''
CtorParamJsonKeyWithExtends _$CtorParamJsonKeyWithExtendsFromJson(
Map<String, dynamic> json,
) => CtorParamJsonKeyWithExtends(
Expand All @@ -49,7 +53,13 @@ Map<String, dynamic> _$CtorParamJsonKeyWithExtendsToJson(
'fake_first': instance.attributeOne,
'two': instance.attributeTwo,
};
''')
''',
expectedLogItems: [
'Field `attributeTwo` has conflicting `JsonKey.name` annotations: both '
'constructor parameter and class field have this annotation. Using '
'constructor parameter value.',
],
)
@JsonSerializable()
class CtorParamJsonKeyWithExtends extends CtorParamJsonKey {
CtorParamJsonKeyWithExtends({
Expand Down