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: Revert "fix: Correct Select Widget Mapping for Dynamic Label and Value Keys (#35862)" #38250

Open
wants to merge 1 commit into
base: release
Choose a base branch
from

Conversation

rahulbarwal
Copy link
Contributor

@rahulbarwal rahulbarwal commented Dec 19, 2024

This reverts commit 9f387d5.

Description

Tip

Add a TL;DR when the description is longer than 500 words or extremely technical (helps the content, marketing, and DevRel team).

Please also include relevant motivation and context. List any dependencies that are required for this change. Add links to Notion, Figma or any other documents that might be relevant to the PR.

Fixes #Issue Number
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=""

🔍 Cypress test results

Caution

If you modify the content in this section, you are likely to disrupt the CI result for your PR.

Communication

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

  • Yes
  • No

Summary by CodeRabbit

  • Bug Fixes

    • Removed a function that validated "Label key" and "Value key" properties in the select widget, leading to a reduction in test coverage for these functionalities.
  • New Features

    • Simplified logic for populating option labels and values when provided as arrays in the select widget.
  • Documentation

    • Updated method signatures and descriptions to reflect changes in the handling of option labels and values.

Copy link
Contributor

coderabbitai bot commented Dec 19, 2024

Walkthrough

This pull request involves modifications to the Select widget's functionality and test suite. The changes primarily focus on simplifying the option label and value handling in the getOptions method of the derived JavaScript file. Simultaneously, a related test case in the Cypress test suite for the Select widget has been removed, which previously validated label and value key functionalities.

Changes

File Change Summary
app/client/cypress/e2e/Regression/ClientSide/Widgets/Select/Select2_Spec.ts Removed validateLabelAndValueKey function and associated test case
app/client/src/widgets/SelectWidget/widget/derived.js Updated getOptions method to directly assign arrays for labels and values when props.optionLabel or props.optionValue are arrays

Possibly related PRs

Suggested labels

Bug, Widgets Product, Table Widget, ok-to-test, Community Reported

Suggested reviewers

  • sagar-qa007
  • ApekshaBhosale

Poem

🔍 Select widget's dance of change,
Arrays now flow with elegant range,
Test cases trim, code becomes lean,
Simplicity reigns, a developer's dream!
🧩 Refactoring magic, smooth and bright


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 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.

@rahulbarwal
Copy link
Contributor Author

/build-deploy-preview skip-tests=true

@rahulbarwal rahulbarwal changed the title Revert "fix: Correct Select Widget Mapping for Dynamic Label and Value Keys (#35862)" fix: Revert "fix: Correct Select Widget Mapping for Dynamic Label and Value Keys (#35862)" Dec 19, 2024
Copy link

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

@github-actions github-actions bot added the Bug Something isn't working label Dec 19, 2024
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: 1

🔭 Outside diff range comments (2)
app/client/src/widgets/SelectWidget/widget/derived.js (2)

Test coverage needed for getOptions function

The derived.test.ts file lacks test coverage for the getOptions function, particularly for:

  • Array-based label handling (props.optionLabel as array)
  • Array-based value handling (props.optionValue as array)
  • String-based property mapping from sourceData

Add test cases to cover these scenarios:

  • When optionLabel is a string (property name)
  • When optionLabel is an array
  • When optionValue is a string (property name)
  • When optionValue is an array
  • When sourceData is empty/undefined
🔗 Analysis chain

Line range hint 1-24: Restore or replace removed test coverage

The AI summary indicates that validateLabelAndValueKey test was removed. Consider adding alternative test coverage for the array-based label and value handling.

Let's check existing test coverage:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for Select widget test files
fd -e ts -e js "Select.*test|Select.*spec" 

Length of output: 4079


Script:

#!/bin/bash
# Search for test files specifically for SelectWidget
fd "Select.*test" app/client/src/widgets/SelectWidget/

# Check for any test files in the widget's directory
fd -e test.js -e test.ts -e spec.js -e spec.ts . app/client/src/widgets/SelectWidget/

# Search for test cases related to optionLabel and optionValue in Select widget tests
rg -i "optionLabel|optionValue" app/client/cypress/e2e/Regression/ClientSide/Widgets/Select/

Length of output: 1484


Script:

#!/bin/bash
# Check the content of the derived.test.ts file
cat app/client/src/widgets/SelectWidget/widget/derived.test.ts

# Look for any test cases related to getOptions function
rg -A 10 "getOptions.*test" app/client/src/widgets/SelectWidget/

Length of output: 9427


Add array length validation to prevent misaligned mappings

The code shows that while sourceData is initialized with a fallback (props.sourceData || []), there's no validation to ensure labels and values arrays have the same length as sourceData. This could lead to undefined values when:

  • props.optionLabel is an array with different length than sourceData
  • props.optionValue is an array with different length than sourceData
  • Property access via d[props.optionLabel] or d[props.optionValue] returns undefined

Add length validation before the final mapping:

if (labels.length !== sourceData.length || values.length !== sourceData.length) {
  return [];
}
🔗 Analysis chain

Line range hint 20-24: Verify array indices alignment

The mapping logic assumes that labels[i] and values[i] will always exist and correspond to the correct sourceData entry. Without validation, this could lead to undefined values or incorrect mappings.

Let's check for existing validation or error handling:


Let me check for more context around the SelectWidget's data handling.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Look for validation patterns in Select widget
ast-grep --pattern 'if ($_ === undefined || $_ === null) { $$$ }' 

Length of output: 2620


Script:

#!/bin/bash
# Look for the full implementation context of the SelectWidget's derived.js
rg -B 10 -A 10 "sourceData.map\(\(d, i\) => \({" app/client/src/widgets/SelectWidget/

# Check for any validation in SelectWidget
ast-grep --pattern 'function $_($$$) { 
  if ($$$) {
    $$$
  }
  return sourceData.map($$$)
}'

Length of output: 312


Script:

#!/bin/bash
# Look for the SelectWidget's derived.js content
rg --type js -A 10 -B 10 "sourceData\.map" app/client/src/widgets/SelectWidget/

# Check for any array length validation
ast-grep --pattern 'if ($_.length !== $_.length) { $$$ }'

# Look for any validation before the map operation
rg --type js -B 5 "sourceData\.map" app/client/src/widgets/SelectWidget/

Length of output: 9548

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ad810c0 and 62f44eb.

📒 Files selected for processing (2)
  • app/client/cypress/e2e/Regression/ClientSide/Widgets/Select/Select2_Spec.ts (0 hunks)
  • app/client/src/widgets/SelectWidget/widget/derived.js (1 hunks)
💤 Files with no reviewable changes (1)
  • app/client/cypress/e2e/Regression/ClientSide/Widgets/Select/Select2_Spec.ts
🔇 Additional comments (1)
app/client/src/widgets/SelectWidget/widget/derived.js (1)

11-11: ⚠️ Potential issue

Direct array assignment could lead to data inconsistency

The direct assignment of arrays (props.optionLabel and props.optionValue) removes the previous mapping logic that ensured synchronization with sourceData. This could cause mismatches if the arrays don't align with sourceData.

Let's verify the usage patterns:

Also applies to: 17-17

Comment on lines 8 to 18
if (typeof props.optionLabel === "string") {
labels = sourceData.map((d) => d[props.optionLabel]);
} else if (_.isArray(props.optionLabel)) {
labels = sourceData.map((d, i) => d[props.optionLabel[i]]);
labels = props.optionLabel;
}

if (typeof props.optionValue === "string") {
values = sourceData.map((d) => d[props.optionValue]);
} else if (_.isArray(props.optionValue)) {
values = sourceData.map((d, i) => d[props.optionValue[i]]);
values = props.optionValue;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider adding type validation for array inputs

Since this is reverting a previous fix, we should ensure type safety for array inputs to prevent runtime errors.

Consider adding validation:

    } else if (_.isArray(props.optionLabel)) {
+     if (!props.optionLabel.every(label => label !== undefined)) {
+       console.warn("Invalid labels found in optionLabel array");
+     }
      labels = props.optionLabel;
    }

    if (typeof props.optionValue === "string") {
      values = sourceData.map((d) => d[props.optionValue]);
    } else if (_.isArray(props.optionValue)) {
+     if (!props.optionValue.every(value => value !== undefined)) {
+       console.warn("Invalid values found in optionValue array");
+     }
      values = props.optionValue;
    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (typeof props.optionLabel === "string") {
labels = sourceData.map((d) => d[props.optionLabel]);
} else if (_.isArray(props.optionLabel)) {
labels = sourceData.map((d, i) => d[props.optionLabel[i]]);
labels = props.optionLabel;
}
if (typeof props.optionValue === "string") {
values = sourceData.map((d) => d[props.optionValue]);
} else if (_.isArray(props.optionValue)) {
values = sourceData.map((d, i) => d[props.optionValue[i]]);
values = props.optionValue;
}
if (typeof props.optionLabel === "string") {
labels = sourceData.map((d) => d[props.optionLabel]);
} else if (_.isArray(props.optionLabel)) {
if (!props.optionLabel.every(label => label !== undefined)) {
console.warn("Invalid labels found in optionLabel array");
}
labels = props.optionLabel;
}
if (typeof props.optionValue === "string") {
values = sourceData.map((d) => d[props.optionValue]);
} else if (_.isArray(props.optionValue)) {
if (!props.optionValue.every(value => value !== undefined)) {
console.warn("Invalid values found in optionValue array");
}
values = props.optionValue;
}

Copy link

Deploy-Preview-URL: https://ce-38250.dp.appsmith.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant