From 99ce790b3d2cd0e77fab14e813eaf1770c40f4d7 Mon Sep 17 00:00:00 2001 From: Fichtelcoder Date: Mon, 4 Aug 2025 18:05:54 +0200 Subject: [PATCH 1/2] Fix code generation for classes inheriting ListBase --- json_serializable/lib/src/field_helpers.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/json_serializable/lib/src/field_helpers.dart b/json_serializable/lib/src/field_helpers.dart index 4539c7e5..c2de4ca5 100644 --- a/json_serializable/lib/src/field_helpers.dart +++ b/json_serializable/lib/src/field_helpers.dart @@ -58,9 +58,9 @@ class _FieldSet implements Comparable<_FieldSet> { /// preference for the getter if it's defined. int offsetFor(FieldElement2 e) { if (e.isSynthetic) { - return (e.getter2 ?? e.setter2)!.firstFragment.nameOffset2!; + return (e.getter2 ?? e.setter2)!.firstFragment.nameOffset2 ?? 0; } - return e.firstFragment.nameOffset2!; + return e.firstFragment.nameOffset2 ?? 0; } return offsetFor(a).compareTo(offsetFor(b)); From 2e2ddd0a4c29071661cf44452b57c5a31e43ee50 Mon Sep 17 00:00:00 2001 From: Fichtelcoder Date: Mon, 4 Aug 2025 18:19:23 +0200 Subject: [PATCH 2/2] Add regression test for #1512 --- .../test/integration/json_test_example.dart | 31 +++++++++++++++++++ .../test/integration/json_test_example.g.dart | 12 +++++++ .../json_test_example.g_any_map.dart | 31 +++++++++++++++++++ .../json_test_example.g_any_map.g.dart | 12 +++++++ 4 files changed, 86 insertions(+) diff --git a/json_serializable/test/integration/json_test_example.dart b/json_serializable/test/integration/json_test_example.dart index 963cdc27..5b6a5581 100644 --- a/json_serializable/test/integration/json_test_example.dart +++ b/json_serializable/test/integration/json_test_example.dart @@ -266,3 +266,34 @@ class RegressionTestIssue1210 with RegressionTestIssue1210Mixin { Map toJson() => _$RegressionTestIssue1210ToJson(this); } + +@JsonSerializable() +class CustomList extends ListBase { + // Regression test for issue: + // https://github.com/google/json_serializable.dart/issues/1512 + + final List _innerList = []; + + CustomList(); + + @override + int get length => _innerList.length; + + @override + set length(int newLength) { + _innerList.length = newLength; + } + + @override + String operator [](int index) => _innerList[index]; + + @override + void operator []=(int index, String value) { + _innerList[index] = value; + } + + factory CustomList.fromJson(Map json) => + _$CustomListFromJson(json); + + Map toJson() => _$CustomListToJson(this); +} diff --git a/json_serializable/test/integration/json_test_example.g.dart b/json_serializable/test/integration/json_test_example.g.dart index 1f1b456e..66b12fa7 100644 --- a/json_serializable/test/integration/json_test_example.g.dart +++ b/json_serializable/test/integration/json_test_example.g.dart @@ -229,3 +229,15 @@ RegressionTestIssue1210 _$RegressionTestIssue1210FromJson( Map _$RegressionTestIssue1210ToJson( RegressionTestIssue1210 instance, ) => {'field': instance.field}; + +CustomList _$CustomListFromJson(Map json) => CustomList() + ..first = json['first'] as String + ..last = json['last'] as String + ..length = (json['length'] as num).toInt(); + +Map _$CustomListToJson(CustomList instance) => + { + 'first': instance.first, + 'last': instance.last, + 'length': instance.length, + }; diff --git a/json_serializable/test/integration/json_test_example.g_any_map.dart b/json_serializable/test/integration/json_test_example.g_any_map.dart index f499eb13..d4458416 100644 --- a/json_serializable/test/integration/json_test_example.g_any_map.dart +++ b/json_serializable/test/integration/json_test_example.g_any_map.dart @@ -266,3 +266,34 @@ class RegressionTestIssue1210 with RegressionTestIssue1210Mixin { Map toJson() => _$RegressionTestIssue1210ToJson(this); } + +@JsonSerializable(anyMap: true) +class CustomList extends ListBase { + // Regression test for issue: + // https://github.com/google/json_serializable.dart/issues/1512 + + final List _innerList = []; + + CustomList(); + + @override + int get length => _innerList.length; + + @override + set length(int newLength) { + _innerList.length = newLength; + } + + @override + String operator [](int index) => _innerList[index]; + + @override + void operator []=(int index, String value) { + _innerList[index] = value; + } + + factory CustomList.fromJson(Map json) => + _$CustomListFromJson(json); + + Map toJson() => _$CustomListToJson(this); +} diff --git a/json_serializable/test/integration/json_test_example.g_any_map.g.dart b/json_serializable/test/integration/json_test_example.g_any_map.g.dart index f25c7971..fdcd10b2 100644 --- a/json_serializable/test/integration/json_test_example.g_any_map.g.dart +++ b/json_serializable/test/integration/json_test_example.g_any_map.g.dart @@ -227,3 +227,15 @@ RegressionTestIssue1210 _$RegressionTestIssue1210FromJson(Map json) => Map _$RegressionTestIssue1210ToJson( RegressionTestIssue1210 instance, ) => {'field': instance.field}; + +CustomList _$CustomListFromJson(Map json) => CustomList() + ..first = json['first'] as String + ..last = json['last'] as String + ..length = (json['length'] as num).toInt(); + +Map _$CustomListToJson(CustomList instance) => + { + 'first': instance.first, + 'last': instance.last, + 'length': instance.length, + };