diff --git a/copy_with_extension/CHANGELOG.md b/copy_with_extension/CHANGELOG.md index e0a165b..4096a13 100644 --- a/copy_with_extension/CHANGELOG.md +++ b/copy_with_extension/CHANGELOG.md @@ -1,3 +1,6 @@ +## 5.0.2 +* [Fix](https://github.com/numen31337/copy_with_extension/issues/79) Allow having a nullable constructor parameter with a fallback for a non-nullable class field. + ## 5.0.1 * [Fix](https://github.com/numen31337/copy_with_extension/issues/72) Warnings when using the `?` operator on a dynamic type. * [Fix](https://github.com/numen31337/copy_with_extension/issues/75) Warnings when using the `!` operator on a non-nullable type. diff --git a/copy_with_extension/pubspec.yaml b/copy_with_extension/pubspec.yaml index f7af70b..1121e1f 100644 --- a/copy_with_extension/pubspec.yaml +++ b/copy_with_extension/pubspec.yaml @@ -1,5 +1,5 @@ name: copy_with_extension -version: 5.0.1 +version: 5.0.2 description: Annotation for generating `copyWith` extensions code using `copy_with_extension_gen`. homepage: https://github.com/numen31337/copy_with_extension/tree/master/copy_with_extension repository: https://github.com/numen31337/copy_with_extension diff --git a/copy_with_extension_gen/CHANGELOG.md b/copy_with_extension_gen/CHANGELOG.md index e0a165b..4096a13 100644 --- a/copy_with_extension_gen/CHANGELOG.md +++ b/copy_with_extension_gen/CHANGELOG.md @@ -1,3 +1,6 @@ +## 5.0.2 +* [Fix](https://github.com/numen31337/copy_with_extension/issues/79) Allow having a nullable constructor parameter with a fallback for a non-nullable class field. + ## 5.0.1 * [Fix](https://github.com/numen31337/copy_with_extension/issues/72) Warnings when using the `?` operator on a dynamic type. * [Fix](https://github.com/numen31337/copy_with_extension/issues/75) Warnings when using the `!` operator on a non-nullable type. 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 9e39ecd..0ab7be7 100644 --- a/copy_with_extension_gen/lib/src/copy_with_generator.dart +++ b/copy_with_extension_gen/lib/src/copy_with_generator.dart @@ -38,9 +38,10 @@ class CopyWithGenerator extends GeneratorForAnnotation { for (final field in sortedFields) { if (field.classFieldInfo != null && - field.nullable != field.classFieldInfo?.nullable) { + field.nullable == false && + field.classFieldInfo?.nullable == true) { throw InvalidGenerationSourceError( - 'The nullability of the constructor parameter "${field.name}" does not match the nullability of the corresponding field in the object.', + 'The constructor parameter "${field.name}" is not nullable, whereas the corresponding class field is nullable. This use case is not supported.', element: element, ); } diff --git a/copy_with_extension_gen/pubspec.yaml b/copy_with_extension_gen/pubspec.yaml index e69eae8..3b156a0 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: 5.0.1 +version: 5.0.2 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_nullability_test.dart b/copy_with_extension_gen/test/gen_nullability_test.dart index 5dfb266..35c00dc 100644 --- a/copy_with_extension_gen/test/gen_nullability_test.dart +++ b/copy_with_extension_gen/test/gen_nullability_test.dart @@ -5,10 +5,8 @@ part 'gen_nullability_test.g.dart'; @CopyWith() class TestNullability { - TestNullability( - this.dynamicField, - this.integers, - ); + TestNullability(this.dynamicField, this.integers, {int? constructorFallback}) + : constructorFallback = constructorFallback ?? 0; /// https://github.com/numen31337/copy_with_extension/issues/74 /// Test for crash on `instance.dynamicField!`. @@ -18,6 +16,10 @@ class TestNullability { /// Warnings during compilation when using `!` on non-nullable value. /// Use `dart run copy_with_extension_gen/test/gen_nullability_test.dart` to reproduce the warning. final List integers; + + /// https://github.com/numen31337/copy_with_extension/issues/79 + /// Case when a class has non-nullable type, but the constructor accepts nullable and falls back. + final int constructorFallback; } void main() { @@ -30,5 +32,21 @@ void main() { expect(TestNullability(null, [1]).copyWith.dynamicField(1).dynamicField, 1); expect( TestNullability(null, [1]).copyWith.integers([2]).dynamicField, null); + + // Test fallback is working + expect( + TestNullability(1, [1], constructorFallback: 1).constructorFallback, 1); + expect( + TestNullability(1, [1], constructorFallback: 1) + .copyWith + .constructorFallback(null) + .constructorFallback, + 0); + expect( + TestNullability(1, [1], constructorFallback: 1) + .copyWith + .constructorFallback(2) + .constructorFallback, + 2); }); } diff --git a/copy_with_extension_gen/test/generated_code_test_cases/test_cases_exceptions.dart b/copy_with_extension_gen/test/generated_code_test_cases/test_cases_exceptions.dart index 553cd65..dab06a6 100644 --- a/copy_with_extension_gen/test/generated_code_test_cases/test_cases_exceptions.dart +++ b/copy_with_extension_gen/test/generated_code_test_cases/test_cases_exceptions.dart @@ -26,7 +26,7 @@ class NoDefaultConstructor { } @ShouldThrow( - 'The nullability of the constructor parameter "nullableWithNonNullableConstructor" does not match the nullability of the corresponding field in the object.', + 'The constructor parameter "nullableWithNonNullableConstructor" is not nullable, whereas the corresponding class field is nullable. This use case is not supported.', ) @CopyWith() class TestNullability {