Skip to content

fix: Make copyWith parameters nullable#848

Merged
tilucasoli merged 1 commit intomainfrom
fix/copywith-nullable
Feb 6, 2026
Merged

fix: Make copyWith parameters nullable#848
tilucasoli merged 1 commit intomainfrom
fix/copywith-nullable

Conversation

@tilucasoli
Copy link
Collaborator

@tilucasoli tilucasoli commented Feb 6, 2026

Related issue

Fixes an issue where copyWith method parameters were not always optional, which could cause compilation errors when non-nullable types were used.

Description

The code generator now ensures all copyWith parameters are nullable (optional), regardless of whether the original field type is nullable or not. This is the expected behavior for copyWith methods, where you only provide the values you want to change.

Changes

  • spec_mixin_builder.dart: Modified copyWith parameter generation to append ? to types that are not already nullable
  • spec_mixin_builder_test.dart: Added comprehensive unit tests covering:
    • Nullable and non-nullable fields
    • Mixed type scenarios
    • Complex types (e.g., BoxDecoration, AlignmentGeometry)
    • List types
    • Null-coalescing usage in the copyWith body
    • Prevention of double ?? suffix

Review Checklist

  • Testing: Have you tested your changes, including unit tests and integration tests for affected code?
  • Breaking Changes: Does this change introduce breaking changes affecting existing code or users?
  • Documentation Updates: Are all relevant documentation files (e.g. README, API docs) updated to reflect the changes in this PR?
  • Website Updates: Is the website containing the updates you make on documentation?

Additional Information (optional)

This fix ensures that generated copyWith methods follow Dart's idiomatic pattern where all parameters are optional. For example:

// Before (broken for non-nullable types):
TestSpec copyWith({
  String name,  // Required because non-nullable
})

// After (correct):
TestSpec copyWith({
  String? name,  // Optional, uses ?? this.name fallback
})

Ensure copyWith parameters are always optional by making their types nullable: SpecMixinBuilder now appends a '?' to parameter types that are not already nullable. Add comprehensive unit tests covering nullable and non-nullable fields, mixed types, complex types, list types, null-coalescing usage in the copyWith body, and prevention of double '??' suffix. The tests introduce test helpers (createTestFieldModel) and minimal fake implementations (_FakeDartType, _FakeFieldElement) to exercise FieldModel-driven code generation.
@docs-page
Copy link

docs-page bot commented Feb 6, 2026

To view this pull requests documentation preview, visit the following URL:

docs.page/btwld/mix~848

Documentation is deployed and generated using docs.page.

@vercel
Copy link
Contributor

vercel bot commented Feb 6, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
mix-docs Ready Ready Preview, Comment Feb 6, 2026 1:41am

@tilucasoli tilucasoli merged commit be0cb9a into main Feb 6, 2026
9 checks passed
@tilucasoli tilucasoli deleted the fix/copywith-nullable branch February 6, 2026 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant