Skip to content

Commit

Permalink
fix: a stackoverflow error when continuously passing unmodifiable col…
Browse files Browse the repository at this point in the history
…lections as parameter to freezed classes (#809)

fixes #793
  • Loading branch information
rrousselGit committed Nov 29, 2022
1 parent f785d1e commit 30abd86
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/freezed/CHANGELOG.md
@@ -1,6 +1,7 @@
# [Unreleased fix]

Fixes copyWith not working with `null` on some scenarios.
- Fixes copyWith not working with `null` on some scenarios.
- Fixes a stackoverflow error when continuously passing unmodifiable collections as parameter to freezed classes

# 2.2.1

Expand Down
7 changes: 7 additions & 0 deletions packages/freezed/lib/src/templates/concrete_template.dart
Expand Up @@ -235,15 +235,22 @@ ${copyWith?.abstractCopyWithGetter ?? ''}
}

if (viewType != null) {
// If the collection is already unmodifiable, we don't want to wrap
// it in an unmodifiable view again.
final isAlreadyUnmodifiableCheck =
'if (_${p.name} is $viewType) return _${p.name};';

return [
p.copyWith(name: '_${p.name}', decorators: const []),
if (p.isNullable) annotatedProperty.asGetter(''' {
final value = _${p.name};
if (value == null) return null;
$isAlreadyUnmodifiableCheck
// ignore: implicit_dynamic_type
return $viewType(value);
}
''') else annotatedProperty.asGetter(''' {
$isAlreadyUnmodifiableCheck
// ignore: implicit_dynamic_type
return $viewType(_${p.name});
}
Expand Down
18 changes: 18 additions & 0 deletions packages/freezed/test/single_class_constructor_test.dart
Expand Up @@ -80,6 +80,12 @@ Future<void> main() async {
UnmodifiableSetEqual({42}).hashCode,
UnmodifiableSetEqual({42}).hashCode,
);

final immutableCollection = UnmodifiableSetEqual({42}).dartSet;
expect(
UnmodifiableSetEqual(immutableCollection).dartSet,
same(immutableCollection),
);
});

test('CustomListEqual', () {
Expand Down Expand Up @@ -122,6 +128,12 @@ Future<void> main() async {
UnmodifiableListEqual([42]).hashCode,
UnmodifiableListEqual([42]).hashCode,
);

final immutableCollection = UnmodifiableListEqual([42]).list;
expect(
UnmodifiableListEqual(immutableCollection).list,
same(immutableCollection),
);
});

test('CustomMapEqual', () {
Expand Down Expand Up @@ -164,6 +176,12 @@ Future<void> main() async {
UnmodifiableMapEqual({'a': 42}).hashCode,
UnmodifiableMapEqual({'a': 42}).hashCode,
);

final immutableCollection = UnmodifiableMapEqual({'a': 42}).map;
expect(
UnmodifiableMapEqual(immutableCollection).map,
same(immutableCollection),
);
});

test('WithAlias', () {
Expand Down

0 comments on commit 30abd86

Please sign in to comment.