Skip to content

Commit 8288fa3

Browse files
authored
Skip warning about duplicate annotations if the values match. (#1532)
1 parent 3c09ab4 commit 8288fa3

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

json_serializable/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

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

68
## 6.11.1
79

json_serializable/lib/src/json_key_utils.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ KeyConfig _from(FieldElement2 element, ClassConfig classAnnotation) {
3636
if (ctorObj != null && !ctorObj.isNull) {
3737
final ctorReadResult = ctorObj.read(field);
3838
if (!ctorReadResult.isNull) {
39-
if (!obj.isNull && !obj.read(field).isNull) {
39+
final fieldReadResult = obj.isNull ? null : obj.read(field);
40+
if (fieldReadResult != null &&
41+
fieldReadResult.objectValue != ctorReadResult.objectValue) {
4042
log.warning(
4143
'Field `${element.name3}` has conflicting `JsonKey.$field` '
4244
'annotations: both constructor parameter and class field have '

json_serializable/test/src/extends_jsonkey_override.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ class CtorParamJsonKey {
3232

3333
@JsonKey(name: 'fake_first')
3434
final String attributeOne;
35+
36+
// Duplicate annotation with exact same value does not give a warning.
37+
@JsonKey(name: 'second')
3538
final String attributeTwo;
3639
}
3740

38-
@ShouldGenerate(r'''
41+
@ShouldGenerate(
42+
r'''
3943
CtorParamJsonKeyWithExtends _$CtorParamJsonKeyWithExtendsFromJson(
4044
Map<String, dynamic> json,
4145
) => CtorParamJsonKeyWithExtends(
@@ -49,7 +53,13 @@ Map<String, dynamic> _$CtorParamJsonKeyWithExtendsToJson(
4953
'fake_first': instance.attributeOne,
5054
'two': instance.attributeTwo,
5155
};
52-
''')
56+
''',
57+
expectedLogItems: [
58+
'Field `attributeTwo` has conflicting `JsonKey.name` annotations: both '
59+
'constructor parameter and class field have this annotation. Using '
60+
'constructor parameter value.',
61+
],
62+
)
5363
@JsonSerializable()
5464
class CtorParamJsonKeyWithExtends extends CtorParamJsonKey {
5565
CtorParamJsonKeyWithExtends({

0 commit comments

Comments
 (0)