Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug when annotated classes also use mixins #1211

Merged
merged 2 commits into from Sep 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion json_serializable/CHANGELOG.md
@@ -1,5 +1,6 @@
## 6.4.1-dev
## 6.4.1

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

## 6.4.0
Expand Down
5 changes: 3 additions & 2 deletions json_serializable/lib/src/field_helpers.dart
Expand Up @@ -36,8 +36,9 @@ class _FieldSet implements Comparable<_FieldSet> {
int compareTo(_FieldSet other) => _sortByLocation(sortField, other.sortField);

static int _sortByLocation(FieldElement a, FieldElement b) {
final checkerA =
TypeChecker.fromStatic((a.enclosingElement3 as ClassElement).thisType);
final checkerA = TypeChecker.fromStatic(
(a.enclosingElement3 as InterfaceElement).thisType,
);

if (!checkerA.isExactly(b.enclosingElement3)) {
// in this case, you want to prioritize the enclosingElement that is more
Expand Down
2 changes: 1 addition & 1 deletion json_serializable/pubspec.yaml
@@ -1,5 +1,5 @@
name: json_serializable
version: 6.4.1-dev
version: 6.4.1
description: >-
Automatically generate code for converting to and from JSON by annotating
Dart classes.
Expand Down
18 changes: 18 additions & 0 deletions json_serializable/test/integration/json_test_example.dart
Expand Up @@ -239,3 +239,21 @@ class PrivateConstructor {
bool operator ==(Object other) =>
other is PrivateConstructor && id == other.id && value == other.value;
}

mixin RegressionTestIssue1210Mixin {
bool? get someProperty => null;

@override
int get hashCode => identityHashCode(this);
}

@JsonSerializable()
class RegressionTestIssue1210 with RegressionTestIssue1210Mixin {
const RegressionTestIssue1210(this.field);

factory RegressionTestIssue1210.fromJson(Map<String, dynamic> json) =>
_$RegressionTestIssue1210FromJson(json);
final String field;

Map<String, dynamic> toJson() => _$RegressionTestIssue1210ToJson(this);
}
12 changes: 12 additions & 0 deletions json_serializable/test/integration/json_test_example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -247,3 +247,23 @@ class PrivateConstructor {
bool operator ==(Object other) =>
other is PrivateConstructor && id == other.id && value == other.value;
}

mixin RegressionTestIssue1210Mixin {
bool? get someProperty => null;

@override
int get hashCode => identityHashCode(this);
}

@JsonSerializable(
anyMap: true,
)
class RegressionTestIssue1210 with RegressionTestIssue1210Mixin {
const RegressionTestIssue1210(this.field);

factory RegressionTestIssue1210.fromJson(Map<String, dynamic> json) =>
_$RegressionTestIssue1210FromJson(json);
final String field;

Map<String, dynamic> toJson() => _$RegressionTestIssue1210ToJson(this);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.