Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: when value of array is empty, no need for data type recalculation #38794

Conversation

rahulbarwal
Copy link
Contributor

@rahulbarwal rahulbarwal commented Jan 22, 2025

Description

Problem
When we have a select/multi-select field type and the source data gives empty array to the JSONFormWidget, the widget tries to gauge the sub type for value inside the array, and since it is empty it got undefined

This led to re-evaluation of property config for the field.

Solution

  • Added a check to prevent unnecessary recalculation of sub data types when arrays are empty in checkIfArrayAndSubDataTypeChanged.

  • Changed parameter type from any to unknown in dataTypeFor and subDataTypeFor functions for improved type safety.

  • Added and refactored unit tests.

This refactor enhances type safety and optimizes performance in the schema parsing logic.

Fixes #37246
or
Fixes Issue URL

Warning

If no issue exists, please create an issue first, and check with the maintainers if the issue is valid.

Automation

/ok-to-test tags="@tag.JSONForm"

🔍 Cypress test results

Tip

🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
Workflow run: https://github.com/appsmithorg/appsmith/actions/runs/12905055896
Commit: cad7015
Cypress dashboard.
Tags: @tag.JSONForm
Spec:


Wed, 22 Jan 2025 09:50:23 UTC

Communication

Should the DevRel and Marketing teams inform users about this change?

  • Yes
  • No

Summary by CodeRabbit

  • Tests

    • Enhanced test suite for determining field types.
    • Added comprehensive test cases covering primitive values, email formats, date formats, array types, object types, and edge cases.
  • Refactor

    • Updated type annotations in schema parser functions to improve type safety.
    • Changed parameter types from any to unknown.
    • Added clarifying comment for handling empty arrays in type checking.

- Changed parameter type from `any` to `unknown` in `dataTypeFor` and `subDataTypeFor` functions for improved type safety.
- Added a check to prevent unnecessary recalculation of sub data types when arrays are empty in `checkIfArrayAndSubDataTypeChanged`.

This refactor enhances type safety and optimizes performance in the schema parsing logic.
- Updated the `.fieldTypeFor` tests to improve coverage and accuracy.
- Added specific test cases for primitive values, email detection, date formats, array handling, and object types.
- Refactored existing tests for clarity and consistency, ensuring all edge cases are addressed.
@rahulbarwal rahulbarwal self-assigned this Jan 22, 2025
Copy link
Contributor

coderabbitai bot commented Jan 22, 2025

Walkthrough

The pull request focuses on enhancing the test suite and type safety for the JSON Form Widget's schema parsing functionality. The changes involve updating test cases in schemaParser.test.ts to provide more comprehensive coverage of field type detection and improving type annotations in schemaParser.ts by replacing any with unknown for better type safety. The modifications aim to strengthen the robustness of type checking and testing for data type identification.

Changes

File Change Summary
app/client/src/widgets/JSONFormWidget/schemaParser.test.ts Replaced existing test case with multiple structured tests for fieldTypeFor function, covering primitive values, email/date formats, arrays, objects, and edge cases.
app/client/src/widgets/JSONFormWidget/schemaParser.ts Updated type annotations from any to unknown for dataTypeFor and subDataTypeFor functions; added clarifying comment for checkIfArrayAndSubDataTypeChanged function.

Assessment against linked issues

Objective Addressed Explanation
[#37246] Binding Reliability Unable to directly correlate these changes with the binding issue reported.

Possibly related PRs

Suggested Labels

Enhancement, ok-to-test, Test

Suggested Reviewers

  • ApekshaBhosale
  • rajatagrawal
  • sagar-qa007

Poem

In the realm of types and tests so bright,
Where any fades and unknown takes flight,
Schema parsing, now robust and clean,
A dance of code, a TypeScript dream! 🧙‍♂️✨

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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.

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.

@github-actions github-actions bot added Bug Something isn't working Community Reported issues reported by community members JSON Form Issue / features related to the JSON form wiget Low An issue that is neither critical nor breaks a user flow Needs More Info Needs additional information Needs Triaging Needs attention from maintainers to triage Production Widgets & Accelerators Pod Issues related to widgets & Accelerators Widgets Product This label groups issues related to widgets labels Jan 22, 2025
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 (3)
app/client/src/widgets/JSONFormWidget/schemaParser.test.ts (3)

1890-1902: Consider adding more edge cases for email validation.

While the current test cases cover basic email formats, consider adding tests for:

  • Emails with multiple dots in domain
  • Emails with special characters in local part
  • International email addresses (IDN)
 const testCases = [
   { input: "[email protected]", expected: FieldType.EMAIL_INPUT },
   { input: "[email protected]", expected: FieldType.EMAIL_INPUT },
+  { input: "[email protected]", expected: FieldType.EMAIL_INPUT },
+  { input: "[email protected]", expected: FieldType.EMAIL_INPUT },
+  { input: "valid@domain.みんな", expected: FieldType.EMAIL_INPUT },
   { input: "invalid.email@", expected: FieldType.TEXT_INPUT },
   { input: "@invalid.com", expected: FieldType.TEXT_INPUT },
   { input: "not-an-email", expected: FieldType.TEXT_INPUT },
 ];

1930-1960: Consider adding validation for mixed-type arrays.

The test cases handle homogeneous arrays well, but consider adding tests for arrays with mixed types to ensure robust handling.

 const testCases = [
   // ... existing cases ...
+  {
+    input: ["string", 42, true, { mixed: "types" }],
+    expected: FieldType.ARRAY,
+  },
 ];

1962-1981: Consider adding tests for objects with array values.

The object test cases could be enhanced by testing objects containing array values.

 const testCases = [
   // ... existing cases ...
+  {
+    input: { arrayField: [1, 2, 3], nested: { arrayField: ["a", "b"] } },
+    expected: FieldType.OBJECT,
+  },
 ];
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e773bb3 and cad7015.

📒 Files selected for processing (1)
  • app/client/src/widgets/JSONFormWidget/schemaParser.test.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: perform-test / rts-build / build
  • GitHub Check: perform-test / client-build / client-build
  • GitHub Check: perform-test / server-build / server-unit-tests
  • GitHub Check: client-unit-tests / client-unit-tests
  • GitHub Check: client-build / client-build
  • GitHub Check: client-lint / client-lint
  • GitHub Check: client-prettier / prettier-check
  • GitHub Check: client-check-cyclic-deps / check-cyclic-dependencies
🔇 Additional comments (3)
app/client/src/widgets/JSONFormWidget/schemaParser.test.ts (3)

1876-1888: Well-structured test cases for primitive values.

The test cases cover essential primitive types and edge cases like null and undefined.


1904-1928: Comprehensive date format test coverage.

Good coverage of various date formats including ISO, regional formats, and natural language dates.


1983-1993: Good coverage of special cases and edge values.

The test cases handle non-standard JavaScript types appropriately.

@rahulbarwal
Copy link
Contributor Author

/build-deploy-preview skip-tests=true

Copy link

Deploying Your Preview: https://github.com/appsmithorg/appsmith/actions/runs/12903364209.
Workflow: On demand build Docker image and deploy preview.
skip-tests: true.
env: ``.
PR: 38794.
recreate: .

@rahulbarwal rahulbarwal changed the title fix: when value array is empty, no need for data type recalculation fix: when value of array is empty, no need for data type recalculation Jan 22, 2025
@rahulbarwal rahulbarwal added ok-to-test Required label for CI and removed ok-to-test Required label for CI labels Jan 22, 2025
* as it would be an empty array, which will always be `undefined`.
* which leads to unnecessary re calculation of a type(https://github.com/appsmithorg/appsmith/issues/37246)
*/
if (currentData.length === 0 || prevData.length === 0) return false;
Copy link
Contributor

@ashit-rath ashit-rath Jan 23, 2025

Choose a reason for hiding this comment

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

Nit: Do you think we should move this condition above the subDataTypeFor and exit early?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ashit-rath In the scenario when it does not work, the source data for JSON from looks like this:
Screenshot 2025-01-23 at 10 27 24 AM

  • Here if I select row at index 0, then the value it receives in this line is [].
  • From the previous evaluation check JSONForm knows that value type is Multiselect and subtype is string
  • For the new row, value is Multiselect but subtype is undefined
  • This needs to revaluation of the type for the JSON form(which subsequently leads to resetting of data in property pane)

What we want to prevent is the check diff between prev and current when one of them turns out to be empty. I don't think that making subDataTypeFor exit early will help us in ☝️ .

@rahulbarwal rahulbarwal merged commit 0af11b9 into release Jan 23, 2025
63 of 67 checks passed
@rahulbarwal rahulbarwal deleted the rahulbarwal/issue37246/Bug-Lose-binding-to-a-script-or-api-with-no-errors branch January 23, 2025 09:44
github-actions bot pushed a commit to Zeral-Zhang/appsmith that referenced this pull request Feb 7, 2025
appsmithorg#38794)

## Description
<ins>Problem</ins>
When we have a `select/multi-select` field type and the source data
gives empty array to the `JSONFormWidget`, the widget tries to gauge the
sub type for value inside the array, and since it is empty it got
`undefined`

This led to re-evaluation of property config for the field. 

<ins>Solution</ins>
- Added a check to prevent unnecessary recalculation of sub data types
when arrays are empty in `checkIfArrayAndSubDataTypeChanged`.

- Changed parameter type from `any` to `unknown` in `dataTypeFor` and
`subDataTypeFor` functions for improved type safety.

- Added and refactored unit tests.

This refactor enhances type safety and optimizes performance in the
schema parsing logic.

Fixes appsmithorg#37246  
_or_  
Fixes `Issue URL`
> [!WARNING]  
> _If no issue exists, please create an issue first, and check with the
maintainers if the issue is valid._

## Automation

/ok-to-test tags="@tag.JSONForm"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/12905055896>
> Commit: cad7015
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=12905055896&attempt=1"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.JSONForm`
> Spec:
> <hr>Wed, 22 Jan 2025 09:50:23 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [x] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Tests**
	- Enhanced test suite for determining field types.
- Added comprehensive test cases covering primitive values, email
formats, date formats, array types, object types, and edge cases.

- **Refactor**
- Updated type annotations in schema parser functions to improve type
safety.
	- Changed parameter types from `any` to `unknown`.
	- Added clarifying comment for handling empty arrays in type checking.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Community Reported issues reported by community members JSON Form Issue / features related to the JSON form wiget Low An issue that is neither critical nor breaks a user flow Needs More Info Needs additional information Needs Triaging Needs attention from maintainers to triage ok-to-test Required label for CI Production Widgets & Accelerators Pod Issues related to widgets & Accelerators Widgets Product This label groups issues related to widgets
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Lose binding to a script or api with no errors
3 participants