Skip to content

Commit f94edb2

Browse files
authored
Fix a bug when annotated classes also use mixins (#1211)
Swapped a cast to ClassElement to less specific InterfaceElement Fixes #1210 Prepare to release v6.4.1
1 parent 89d75b9 commit f94edb2

File tree

7 files changed

+67
-4
lines changed

7 files changed

+67
-4
lines changed

json_serializable/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## 6.4.1-dev
1+
## 6.4.1
22

3+
- Fixed a bug when an `@JsonSerializable` class uses a mixin with fields.
34
- Added more documentation `@JsonEnum`.
45

56
## 6.4.0

json_serializable/lib/src/field_helpers.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ class _FieldSet implements Comparable<_FieldSet> {
3636
int compareTo(_FieldSet other) => _sortByLocation(sortField, other.sortField);
3737

3838
static int _sortByLocation(FieldElement a, FieldElement b) {
39-
final checkerA =
40-
TypeChecker.fromStatic((a.enclosingElement3 as ClassElement).thisType);
39+
final checkerA = TypeChecker.fromStatic(
40+
(a.enclosingElement3 as InterfaceElement).thisType,
41+
);
4142

4243
if (!checkerA.isExactly(b.enclosingElement3)) {
4344
// in this case, you want to prioritize the enclosingElement that is more

json_serializable/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_serializable
2-
version: 6.4.1-dev
2+
version: 6.4.1
33
description: >-
44
Automatically generate code for converting to and from JSON by annotating
55
Dart classes.

json_serializable/test/integration/json_test_example.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,21 @@ class PrivateConstructor {
239239
bool operator ==(Object other) =>
240240
other is PrivateConstructor && id == other.id && value == other.value;
241241
}
242+
243+
mixin RegressionTestIssue1210Mixin {
244+
bool? get someProperty => null;
245+
246+
@override
247+
int get hashCode => identityHashCode(this);
248+
}
249+
250+
@JsonSerializable()
251+
class RegressionTestIssue1210 with RegressionTestIssue1210Mixin {
252+
const RegressionTestIssue1210(this.field);
253+
254+
factory RegressionTestIssue1210.fromJson(Map<String, dynamic> json) =>
255+
_$RegressionTestIssue1210FromJson(json);
256+
final String field;
257+
258+
Map<String, dynamic> toJson() => _$RegressionTestIssue1210ToJson(this);
259+
}

json_serializable/test/integration/json_test_example.g.dart

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

json_serializable/test/integration/json_test_example.g_any_map.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,23 @@ class PrivateConstructor {
247247
bool operator ==(Object other) =>
248248
other is PrivateConstructor && id == other.id && value == other.value;
249249
}
250+
251+
mixin RegressionTestIssue1210Mixin {
252+
bool? get someProperty => null;
253+
254+
@override
255+
int get hashCode => identityHashCode(this);
256+
}
257+
258+
@JsonSerializable(
259+
anyMap: true,
260+
)
261+
class RegressionTestIssue1210 with RegressionTestIssue1210Mixin {
262+
const RegressionTestIssue1210(this.field);
263+
264+
factory RegressionTestIssue1210.fromJson(Map<String, dynamic> json) =>
265+
_$RegressionTestIssue1210FromJson(json);
266+
final String field;
267+
268+
Map<String, dynamic> toJson() => _$RegressionTestIssue1210ToJson(this);
269+
}

json_serializable/test/integration/json_test_example.g_any_map.g.dart

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)