From a8aafd2837eda834364b9036baec7b16d9fdd1b1 Mon Sep 17 00:00:00 2001 From: Alexander Kirsch Date: Thu, 3 Feb 2022 13:41:02 +0100 Subject: [PATCH] fix for copyWith null for a non-nullable value (#46) * fix for copyWith null for a non-nullable value * version bump * minor ci correction * minor ci correction --- .github/workflows/run_tests.yml | 2 +- copy_with_extension_gen/CHANGELOG.md | 3 +++ copy_with_extension_gen/lib/src/copy_with_generator.dart | 4 +++- copy_with_extension_gen/pubspec.yaml | 2 +- .../test/gen_basic_functionality_test.dart | 6 +++--- .../generated_code_test_cases/test_case_basic_usage.dart | 2 +- 6 files changed, 12 insertions(+), 7 deletions(-) diff --git a/.github/workflows/run_tests.yml b/.github/workflows/run_tests.yml index 23563e2..1fe7185 100644 --- a/.github/workflows/run_tests.yml +++ b/.github/workflows/run_tests.yml @@ -100,4 +100,4 @@ jobs: run: | cmp -s example/example.dart ../copy_with_extension/example/example.dart || (echo "::error ::File example.dart is out of sync" && exit 1) cmp -s LICENSE ../copy_with_extension/LICENSE || (echo "::error ::File LICENSE is out of sync" && exit 1) - cmp -s CHANGELOG.md ../copy_with_extension/CHANGELOG.md || (echo "::error ::File CHANGELOG.md is out of sync" && exit 1) \ No newline at end of file + cmp -s CHANGELOG.md ../copy_with_extension/CHANGELOG.md || (echo "::warning ::File CHANGELOG.md is out of sync") \ No newline at end of file diff --git a/copy_with_extension_gen/CHANGELOG.md b/copy_with_extension_gen/CHANGELOG.md index 3f4a3bf..7cfdabb 100644 --- a/copy_with_extension_gen/CHANGELOG.md +++ b/copy_with_extension_gen/CHANGELOG.md @@ -1,3 +1,6 @@ +## 4.0.1 +* [Fix](https://github.com/numen31337/copy_with_extension/issues/45) for passing `null` into `copyWith` function for non-nullable values. + ## 4.0.0 * **BREAKING** `copyWith` function now correctly supports nullification of nullable fields like so `copyWith(id: null)`. * **BREAKING** `CopyWith` annotation for named constructor `namedConstructor` is renamed to `constructor` to be in sync with [json_serializable](https://pub.dev/packages/json_serializable). diff --git a/copy_with_extension_gen/lib/src/copy_with_generator.dart b/copy_with_extension_gen/lib/src/copy_with_generator.dart index bff4a05..6d6e275 100644 --- a/copy_with_extension_gen/lib/src/copy_with_generator.dart +++ b/copy_with_extension_gen/lib/src/copy_with_generator.dart @@ -74,10 +74,12 @@ class CopyWithGenerator extends GeneratorForAnnotation { '', (r, v) { if (v.immutable) return '$r ${v.name}: _value.${v.name},'; + final nullCheckForNonNullable = + v.nullable ? "" : "|| ${v.name} == null"; return ''' $r ${v.name}: - ${v.name} == const \$CopyWithPlaceholder() + ${v.name} == const \$CopyWithPlaceholder() $nullCheckForNonNullable ? _value.${v.name} // ignore: cast_nullable_to_non_nullable : ${v.name} as ${v.type},'''; diff --git a/copy_with_extension_gen/pubspec.yaml b/copy_with_extension_gen/pubspec.yaml index 08ad1cd..a53ca32 100644 --- a/copy_with_extension_gen/pubspec.yaml +++ b/copy_with_extension_gen/pubspec.yaml @@ -1,5 +1,5 @@ name: copy_with_extension_gen -version: 4.0.0 +version: 4.0.1 description: Automatically generating `copyWith` extensions code for classes with `@CopyWith()` annotation. repository: https://github.com/numen31337/copy_with_extension homepage: https://github.com/numen31337/copy_with_extension/tree/master/copy_with_extension_gen diff --git a/copy_with_extension_gen/test/gen_basic_functionality_test.dart b/copy_with_extension_gen/test/gen_basic_functionality_test.dart index 6964ff8..3bf14d9 100644 --- a/copy_with_extension_gen/test/gen_basic_functionality_test.dart +++ b/copy_with_extension_gen/test/gen_basic_functionality_test.dart @@ -16,9 +16,7 @@ class CopyWithValues { class CopyWithValuesOptional { final String? id; - const CopyWithValuesOptional({ - this.id, - }); + const CopyWithValuesOptional({this.id}); } @CopyWith() @@ -50,6 +48,8 @@ void main() { const CopyWithValues(id: '').copyWith(id: "test").id, "test", ); + + expect(const CopyWithValues(id: '').copyWith(id: null).id, ''); }); test('CopyWithValuesOptional', () { diff --git a/copy_with_extension_gen/test/generated_code_test_cases/test_case_basic_usage.dart b/copy_with_extension_gen/test/generated_code_test_cases/test_case_basic_usage.dart index 2a762ef..035b2da 100644 --- a/copy_with_extension_gen/test/generated_code_test_cases/test_case_basic_usage.dart +++ b/copy_with_extension_gen/test/generated_code_test_cases/test_case_basic_usage.dart @@ -44,7 +44,7 @@ class _$BasicClassCWProxyImpl> Object? optional = const $CopyWithPlaceholder(), }) { return BasicClass( - id: id == const $CopyWithPlaceholder() + id: id == const $CopyWithPlaceholder() || id == null ? _value.id // ignore: cast_nullable_to_non_nullable : id as String,