Skip to content

Conversation

@jayant-dhingra-cb
Copy link
Collaborator

fix: Handle null values in React Native bridge calls

@gemini-code-assist
Copy link

Summary of Changes

Hello @jayant-dhingra-cb, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on enhancing the DataAdapter in the Android codebase to improve data handling and validation, particularly for data received via the React Native bridge. The changes streamline array processing for index properties and introduce more robust error checking for blob data, preventing potential issues arising from null or malformed inputs. This addresses a fix for handling null values in React Native bridge calls.

Highlights

  • Array Handling Simplification: The code for processing arrays within indexProperties has been refactored to use chained null-safe calls (?.let), eliminating intermediate variables and making the logic more concise and readable.
  • Robust Blob Data Validation: The blob data parsing mechanism has been significantly improved with added explicit null checks and error handling for various components of the blob object (nested map, content type, raw data list, and double list conversion). This ensures that invalid or missing blob data results in clear exceptions.
  • Type Compatibility for Blob Data: The type casting for blob data has been updated from HashMap to Map and ArrayList to List, providing more general and compatible type handling during deserialization from React Native bridge calls.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request successfully refactors array handling for conciseness and enhances blob data validation, making the code more robust. My review includes suggestions to further improve the code by eliminating duplication in index property processing, using more specific exceptions for clearer error handling, and simplifying data conversion logic for better readability and maintainability.

Comment on lines 514 to 522
val nestedMap = value["data"] as? Map<*, *>
?: throw Exception("Error: Invalid blob object")
val contentType = nestedMap["contentType"] as? String
?: throw Exception("Error: Missing blob contentType")
val rawList = nestedMap["data"] as? List<*>
?: throw Exception("Error: Missing blob data array")
val doubleList = rawList.filterIsInstance<Double>()
.takeIf { it.size == rawList.size }
?: throw Exception("Error: Invalid blob data format")

Choose a reason for hiding this comment

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

medium

While the added validation is a great improvement for robustness, throwing generic Exception is not ideal. It's better practice to use more specific exception types like IllegalArgumentException to clearly indicate that the method received an invalid argument. This helps in better error handling and debugging. Additionally, including the key in the error message would provide more context for debugging.

Suggested change
val nestedMap = value["data"] as? Map<*, *>
?: throw Exception("Error: Invalid blob object")
val contentType = nestedMap["contentType"] as? String
?: throw Exception("Error: Missing blob contentType")
val rawList = nestedMap["data"] as? List<*>
?: throw Exception("Error: Missing blob data array")
val doubleList = rawList.filterIsInstance<Double>()
.takeIf { it.size == rawList.size }
?: throw Exception("Error: Invalid blob data format")
val nestedMap = value["data"] as? Map<*, *>
?: throw IllegalArgumentException("Error: Invalid blob object for key '$key'")
val contentType = nestedMap["contentType"] as? String
?: throw IllegalArgumentException("Error: Missing blob contentType for key '$key'")
val rawList = nestedMap["data"] as? List<*>
?: throw IllegalArgumentException("Error: Missing blob data array for key '$key'")
val doubleList = rawList.filterIsInstance<Double>()
.takeIf { it.size == rawList.size }
?: throw IllegalArgumentException("Error: Invalid blob data format for key '$key'")

Copy link
Contributor

@velicuvlad velicuvlad Oct 3, 2025

Choose a reason for hiding this comment

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

is this public API fun toMap(readableMap: ReadableMap): Map<String, Any>?

I am not sure that these will throw ever or should not ever throw? Would like to understand why would this throw is needed at RN level and not native JAK level.

On iOS, we do throw kCBLErrorMessageInvalidBlob when metadata is missing on get and kCBLErrorMessageBlobContentNull on set. I expect to similar for JAK as well.

Comment on lines 523 to 524
val intData = doubleList.map { it.toInt() }.toIntArray()
val data = ByteArray(intData.size) { i -> intData[i].toByte() }

Choose a reason for hiding this comment

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

medium

The conversion from a list of Double to a ByteArray can be made more concise. You can chain the map and conversion operations directly to create the ByteArray without needing an intermediate IntArray.

Suggested change
val intData = doubleList.map { it.toInt() }.toIntArray()
val data = ByteArray(intData.size) { i -> intData[i].toByte() }
val data = doubleList.map { it.toInt().toByte() }.toByteArray()

@velicuvlad
Copy link
Contributor

velicuvlad commented Oct 3, 2025

There are some pullFilter changes in between c4e3f7 and latest main 275695 for cbl-js-kotlin. My understanding is that the missing commit which was failing on CI earlier, was in-fact the PR's for that feature. Now that has been merged and so, I think we should update the submodule to point to main.

@jayant-dhingra-cb jayant-dhingra-cb merged commit 4cfe0d4 into main Oct 9, 2025
5 checks passed
@jayant-dhingra-cb jayant-dhingra-cb deleted the fix/null-issue-fix branch October 9, 2025 10:26
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.

3 participants