Skip to content

Conversation

xsahil03x
Copy link
Member

@xsahil03x xsahil03x commented Jun 5, 2025

Submit a pull request

Zendesk: 66002

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced type safety and validation for channel and user properties like name, image, disabled, hidden, and truncatedAt to prevent runtime errors and ensure valid values.
  • New Features
    • Introduced a new shortcut to easily retrieve the channel name when available.
  • Tests
    • Expanded test coverage to verify robust handling of channel and user property values from extra data under various conditions.

Copy link
Contributor

coderabbitai bot commented Jun 5, 2025

Walkthrough

Type-safe getters were introduced for several properties in the ChannelModel and User classes, ensuring values from extraData are validated before use. A new safeCast<T>() extension was added to support this. New and updated tests verify these changes, especially for handling missing, empty, or incorrectly-typed values in extraData.

Changes

File(s) Change Summary
.../lib/src/core/models/channel_model.dart Updated getters for disabled, hidden, and truncatedAt with type-safe casting; replaced name getter with safer variant; changed lastUpdatedAt to non-nullable.
.../lib/src/core/models/user.dart Refined name and image getters to use safe casting and handle empty or invalid values in extraData.
.../lib/src/core/util/extension.dart Added SafeCastExtension providing safeCast<T>() method for safe type casting without exceptions.
.../test/src/core/models/channel_test.dart Added tests for ChannelModel's name, disabled, hidden, and truncatedAt properties covering edge cases.
.../test/src/core/models/user_test.dart Added tests for User's name and image properties, covering valid, missing, empty, and invalid cases.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant ChannelModel
    participant User

    Client->>ChannelModel: Access .name/.disabled/.hidden/.truncatedAt
    ChannelModel->>ChannelModel: Use safeCast<T>() on extraData values
    ChannelModel-->>Client: Return validated value or null

    Client->>User: Access .name/.image
    User->>User: Use safeCast<T>() on extraData values
    User-->>Client: Return validated value or fallback (id/null)
Loading

Poem

In fields of code where data hides,
We rabbits hopped with careful strides.
Now getters check and tests abound,
No empty strings or types unsound.
With every hop, the bugs grow fewer—
Type-safe and strong, our models truer!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a383643 and 75e6763.

📒 Files selected for processing (3)
  • packages/stream_chat/lib/src/core/models/channel_model.dart (4 hunks)
  • packages/stream_chat/lib/src/core/models/user.dart (2 hunks)
  • packages/stream_chat/lib/src/core/util/extension.dart (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/stream_chat/lib/src/core/models/user.dart
  • packages/stream_chat/lib/src/core/models/channel_model.dart
⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: analyze_legacy_versions
  • GitHub Check: test
  • GitHub Check: format
  • GitHub Check: analyze
  • GitHub Check: stream_chat_flutter
  • GitHub Check: build (android)
  • GitHub Check: build (ios)
  • GitHub Check: stream_chat_localizations
  • GitHub Check: stream_chat_flutter_core
  • GitHub Check: stream_chat_persistence
  • GitHub Check: stream_chat
🔇 Additional comments (1)
packages/stream_chat/lib/src/core/util/extension.dart (1)

96-105: LGTM! Clean implementation of safe casting utility.

The SafeCastExtension provides a robust solution for type-safe casting from dynamic data structures like extraData maps. The implementation correctly:

  • Uses the is operator for type checking before casting
  • Returns null for both null objects and type mismatches
  • Leverages Dart's generic type system effectively
  • Includes clear documentation

This will significantly improve type safety when extracting values from extraData without risking runtime exceptions.


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/stream_chat/test/src/core/models/user_test.dart (1)

406-440: Comprehensive test coverage for the .image getter type safety.

The test cases thoroughly validate the type-safe .image getter behavior:

  • Valid string URL from extraData
  • Missing key returns null
  • Wrong type returns null
  • Empty string returns null

However, there's a minor issue in one test case:

-        extraData: const {'image': bool},
+        extraData: const {'image': true},

The current test uses bool (a Type) instead of a boolean value, which may not represent the intended test scenario.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e833cf7 and f2a73bc.

📒 Files selected for processing (4)
  • packages/stream_chat/lib/src/core/models/channel_model.dart (2 hunks)
  • packages/stream_chat/lib/src/core/models/user.dart (1 hunks)
  • packages/stream_chat/test/src/core/models/channel_test.dart (2 hunks)
  • packages/stream_chat/test/src/core/models/user_test.dart (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: test
  • GitHub Check: build (android)
  • GitHub Check: build (ios)
  • GitHub Check: analyze
  • GitHub Check: format
  • GitHub Check: analyze_legacy_versions
  • GitHub Check: stream_chat_localizations
  • GitHub Check: stream_chat_flutter
  • GitHub Check: stream_chat
  • GitHub Check: stream_chat_flutter_core
  • GitHub Check: stream_chat_persistence
🔇 Additional comments (9)
packages/stream_chat/test/src/core/models/user_test.dart (1)

370-404: Excellent test coverage for the .name getter type safety.

The test cases comprehensively cover all scenarios for the type-safe .name getter:

  • Valid string from extraData
  • Empty string fallback to id
  • Missing key fallback to id
  • Wrong type fallback to id

These tests align perfectly with the implementation changes in the User model.

packages/stream_chat/lib/src/core/models/user.dart (2)

85-90: Well-implemented type-safe .name getter.

The implementation correctly adds type safety with:

  • Explicit String type check
  • Non-empty validation
  • Appropriate fallback to id when invalid

This prevents runtime type errors and ensures consistent behavior when extraData contains unexpected values.


96-101: Excellent type-safe implementation for the .image getter.

The getter follows the same defensive pattern as .name:

  • Type validation with is String
  • Emptiness check with isNotEmpty
  • Null return for invalid cases

This ensures the getter only returns valid, non-empty image URLs or null, preventing potential issues with empty strings or wrong types.

packages/stream_chat/test/src/core/models/channel_test.dart (3)

194-228: Thorough test coverage for the .name getter.

The test cases properly validate all scenarios:

  • Valid string extraction from extraData
  • Null return for missing data
  • Null return for wrong type
  • Null return for empty string

These tests ensure the type-safe getter behaves correctly across all edge cases.


230-282: Complete test coverage for boolean getters .disabled and .hidden.

Both getter test suites follow the same pattern and correctly validate:

  • Proper boolean value extraction from extraData
  • Null return when key is missing
  • Null return when value is not a boolean

The consistent testing approach ensures reliable type safety for boolean properties.


284-319: Comprehensive test coverage for the .truncatedAt getter.

The tests properly validate the DateTime parsing logic:

  • Successful parsing from ISO8601 string in extraData
  • Null return for missing data
  • Null return for non-string values
  • Null return for empty strings

This ensures the getter safely handles DateTime parsing with appropriate fallbacks.

packages/stream_chat/lib/src/core/models/channel_model.dart (3)

121-135: Excellent type-safe implementation for boolean getters.

Both .disabled and .hidden getters follow the same robust pattern:

  • Explicit type checking with is bool
  • Null return for invalid types
  • Clean, readable implementation

This prevents runtime type errors when extraData contains unexpected values for these boolean properties.


140-146: Robust type-safe implementation for .truncatedAt getter.

The implementation correctly handles the complexity of DateTime parsing:

  • Type validation with is String
  • Emptiness check with isNotEmpty
  • Safe DateTime parsing with appropriate exception handling
  • Null fallback for invalid cases

This prevents both type errors and parsing exceptions when extraData contains malformed date strings.


156-161: Well-designed type-safe .name getter.

The new getter follows the established pattern consistently:

  • String type validation
  • Non-empty string check
  • Null return for invalid cases

This aligns with the User model's .name getter pattern, though with different fallback behavior (null vs id), which is appropriate for the different use cases.

Copy link

codecov bot commented Jun 5, 2025

Codecov Report

Attention: Patch coverage is 93.33333% with 1 line in your changes missing coverage. Please review.

Project coverage is 63.29%. Comparing base (e833cf7) to head (75e6763).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...stream_chat/lib/src/core/models/channel_model.dart 88.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2274      +/-   ##
==========================================
+ Coverage   63.28%   63.29%   +0.01%     
==========================================
  Files         404      404              
  Lines       25269    25274       +5     
==========================================
+ Hits        15992    15998       +6     
+ Misses       9277     9276       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@xsahil03x xsahil03x requested a review from renefloor June 6, 2025 11:34
@xsahil03x xsahil03x merged commit 6c1eee2 into master Jun 6, 2025
19 checks passed
@xsahil03x xsahil03x deleted the chore/improve-extra-data-getter-types-safety branch June 6, 2025 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants