Skip to content

Commit

Permalink
fix: deep copy now correctly handles cases where a property is a Free…
Browse files Browse the repository at this point in the history
…zed class with disabled `copyWith` (#810)

fixes #747
  • Loading branch information
rrousselGit committed Nov 29, 2022
1 parent 30abd86 commit ef7044f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/freezed/CHANGELOG.md
Expand Up @@ -2,7 +2,7 @@

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

- fix: Deep copy now correctly handles cases where a property is a Freezed class with disabled `copyWith`
# 2.2.1

Upgrade analyzer
Expand Down
15 changes: 10 additions & 5 deletions packages/freezed/lib/src/freezed_generator.dart
Expand Up @@ -424,19 +424,24 @@ Read here: https://github.com/rrousselGit/freezed/blob/master/packages/freezed/C

final parameterType = parameter.type;
if (parameterType is! InterfaceType) continue;
final element = parameterType.element;
if (element is! ClassElement) continue;
final typeElement = parameterType.element;
if (typeElement is! ClassElement) continue;

final classElement = element;
final freezedAnnotation = typeChecker.firstAnnotationOf(typeElement);

if (!typeChecker.hasAnnotationOf(classElement)) continue;
/// Handles classes annotated with Freezed
if (freezedAnnotation == null) continue;

final configs = _parseConfig(typeElement);
// copyWith not enabled, so the property is not cloneable
if (configs.copyWith != true) continue;

yield CloneableProperty(
name: parameter.name,
type: type!,
nullable:
parameter.type.nullabilitySuffix == NullabilitySuffix.question,
typeName: classElement.name,
typeName: typeElement.name,
genericParameters: GenericsParameterTemplate(
(parameter.type as InterfaceType)
.typeArguments
Expand Down
10 changes: 10 additions & 0 deletions packages/freezed/test/integration/deep_copy.dart
Expand Up @@ -49,3 +49,13 @@ class Generic<T> with _$Generic<T> {
class Recursive with _$Recursive {
factory Recursive([Recursive? value]) = _Recursive;
}

@freezed
class DisabledDeepCopyWith with _$DisabledDeepCopyWith {
factory DisabledDeepCopyWith(DisabledCopy disabled) = _DisabledDeepCopyWith;
}

@Freezed(copyWith: false)
class DisabledCopy with _$DisabledCopy {
factory DisabledCopy(DisabledCopy disabled) = _DisabledCopy;
}

0 comments on commit ef7044f

Please sign in to comment.