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(permalink): parsing urlParams as well #24160

Conversation

Always-prog
Copy link
Contributor

SUMMARY

When reading urlParams from a permalink, an error occurs because the urlencode function expects a dictionary, but urlParams is a 2D array. To resolve this, we simply need to use dict(urlParams) and it will work correctly.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@codecov
Copy link

codecov bot commented May 21, 2023

Codecov Report

Merging #24160 (3da5375) into master (66594ad) will increase coverage by 0.00%.
The diff coverage is 91.66%.

❗ Current head 3da5375 differs from pull request most recent head 52e3f7c. Consider uploading reports for the commit 52e3f7c to get more accurate results

@@           Coverage Diff           @@
##           master   #24160   +/-   ##
=======================================
  Coverage   68.26%   68.27%           
=======================================
  Files        1952     1952           
  Lines       75360    75367    +7     
  Branches     8208     8208           
=======================================
+ Hits        51448    51455    +7     
  Misses      21806    21806           
  Partials     2106     2106           
Flag Coverage Δ
hive 53.30% <75.00%> (+<0.01%) ⬆️
mysql 78.95% <91.66%> (+<0.01%) ⬆️
postgres 79.02% <91.66%> (+<0.01%) ⬆️
presto 53.23% <75.00%> (+<0.01%) ⬆️
python 82.82% <91.66%> (+<0.01%) ⬆️
sqlite 77.55% <91.66%> (+<0.01%) ⬆️
unit 53.27% <33.33%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
superset/views/core.py 75.28% <0.00%> (ø)
superset/sql_lab.py 86.36% <100.00%> (ø)
superset/sqllab/utils.py 100.00% <100.00%> (ø)
superset/views/utils.py 80.98% <100.00%> (+0.07%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@@ -1959,7 +1959,7 @@ def dashboard_permalink( # pylint: disable=no-self-use
dashboard_id, state = value["dashboardId"], value.get("state", {})
url = f"/superset/dashboard/{dashboard_id}?permalink_key={key}"
if url_params := state.get("urlParams"):
params = parse.urlencode(url_params)
params = parse.urlencode(dict(url_params))
Copy link
Member

Choose a reason for hiding this comment

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

I'm surprised this wasn't caught by mypy or related tests. Can you double check that the the types are accurate, and see if the tests are correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@villebro Yes, but at the superset/views/core.py:836 you're parsing url_params the same way using dict

        if key is not None:
            command = GetExplorePermalinkCommand(key)
            try:
                permalink_value = command.run()
                if permalink_value:
                    state = permalink_value["state"]
                    initial_form_data = state["formData"]
                    url_params = state.get("urlParams")
                    if url_params:
                        initial_form_data["url_params"] = dict(url_params)
                else:
                    return json_error_response(
                        _("Error: permalink state not found"), status=404
                    )
            except (ChartNotFoundError, ExplorePermalinkGetFailedError) as ex:
                flash(__("Error: %(msg)s", msg=ex.message), "danger")
                return redirect("/chart/list/")

Copy link
Contributor Author

@Always-prog Always-prog May 21, 2023

Choose a reason for hiding this comment

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

@villebro, I don't see any test cases that test urlParams in the permalink state.
The type of urlParams is Optional[List[Tuple[str, str]]], which means that parsing urlParams using dict is the correct (and works on me)

Copy link
Contributor Author

@Always-prog Always-prog May 21, 2023

Choose a reason for hiding this comment

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

@villebro So the urlencode function taking dictionary, it means that we need to make dictionary from urlParams that have type List[Tuple[str, str]] (as you doing it in superset/views/core.py:836)

Copy link
Member

Choose a reason for hiding this comment

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

The reason we want to keep it as a list/tuple of tuples is because url params can contain duplicate keys (this would be lost is it were converted to a dict). I believe this may be a regression from #23888 , which has lost some context on tuples, which has caused them to become lists, which isn't compatible with urlencode.

I think the correct solution is to decode the persisted JSON object with the relevant marshmallow schema, which could be implemented with a new MarshmallowCodec. I can open a PR with the required changes. Alternatively, feel free to take a stab at it if you want!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please open PR by yourself!

Copy link
Member

Choose a reason for hiding this comment

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

I'll do that, thanks for bringing this to our attention!

Copy link
Member

Choose a reason for hiding this comment

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

@Always-prog I was able to reproduce the issue, thanks again!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@villebro you are welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants