Skip to content

fix: prevent adding query string for empty parameters in router link href - #17290

Merged
mcollovati merged 2 commits into
mainfrom
issue/17287_queryparams_for_empty_string
Aug 3, 2023
Merged

mcollovati merged 2 commits into
mainfrom
issue/17287_queryparams_for_empty_string

Conversation

@mcollovati

Copy link
Copy Markdown
Collaborator

Description

When an empty query parameters object is given to a RouterLink the computed href contains an empty query string.
This change prevents the addition of the query string to the href attribute when query parameters object is empty. It also fixes parsing to return an empty query parameters object for blank string.

Fixes #17287

Type of change

  • Bugfix
  • Feature

Checklist

  • I have read the contribution guide: https://vaadin.com/docs/latest/guide/contributing/overview/
  • I have added a description following the guideline.
  • The issue is created in the corresponding repository and I have referenced it.
  • I have added tests to ensure my change is effective and works as intended.
  • New and existing tests are passing locally with my change.
  • I have performed self-review and corrected misspellings.

Additional for Feature type of change

  • Enhancement / new feature was discussed in a corresponding GitHub issue and Acceptance Criteria were created.

…href

When an empty query parameters object is given to a RouterLink
the computed href contains an empty query string.
This change prevents the addition of the query string to the
href attribute when query parameters object is empty.
It also fixes parsing to return an empty query parameters
object for blank string.

Fixes #17287
@sonatype-lift

sonatype-lift Bot commented Jul 26, 2023

Copy link
Copy Markdown

Sonatype Lift is retiring

Sonatype Lift will be retiring on Sep 12, 2023, with its analysis stopping on Aug 12, 2023. We understand that this news may come as a disappointment, and Sonatype is committed to helping you transition off it seamlessly. If you’d like to retain your data, please export your issues from the web console.
We are extremely grateful and thank you for your support over the years.

📖 Read about the impacts and timeline

@github-actions

github-actions Bot commented Jul 26, 2023 •

Copy link
Copy Markdown
Contributor

Test Results

1 000 files  ±0  1 000 suites  ±0   1h 5m 45s ⏱️ + 1m 14s
6 371 tests +3  6 330 ✔️ +3  41 💤 ±0  0 ❌ ±0 
6 622 runs   - 5  6 574 ✔️  - 5  48 💤 ±0  0 ❌ ±0 

Results for commit cf76eab. ± Comparison against base commit c09aa1e.

♻️ This comment has been updated with latest results.

@mshabarov
mshabarov self-requested a review July 31, 2023 11:13
@sonarqubecloud

Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

@czp13 czp13 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Two fixes were made here.
Copying this from the related issue:

- QueryParameters for an empty string should be empty, as reported
- RouterLink should not add query string for empty QueryParameters

Tests are added for multiple cases, imports are good as well.

The only minor thing is that:

        Map<String, String> paramMapEmpty = new HashMap<>();
        paramMapEmpty.put(" ", "test"); //    paramMapEmpty.put("", " "); paramMapEmpty.put(" ", "");  will fail too.
        link.setQueryParameters(QueryParameters.simple(paramMapEmpty));
        href = link.getHref();
        Assert.assertEquals("foo", href);

        //Also:
        //   paramMapEmpty.put("test", " ");
        //   paramMapEmpty.put("", "test");

These cases all will be added. For example:
paramMapEmpty.put("", "test"); -> foo?%20=test

We could handle some of them with a longer condition:

        if (queryParameters != null
                && !queryParameters.getParameters().isEmpty()
                && !queryParameters.getParameters().keySet().stream()
                        .allMatch(String::isBlank)
                && !queryParameters.getParameters().values().stream()
                        .allMatch(strings -> strings.stream()
                                .allMatch(String::isBlank))) {
            url += '?' + queryParameters.getQueryString();
        }

Not sure if the above condition is good enough, I think too strict as single query params without value will not be allowed, so maybe we could support this query param:

  • foo?%20=test

but could support:

  • foo?test (which is valid as I know)

url = UrlUtil.encodeURI(url);
if (queryParameters != null) {
if (queryParameters != null
&& !queryParameters.getParameters().isEmpty()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Potentially we could make this condition more strict to exclude this case paramMapEmpty.put("", "test");.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Actually, nothing prevents you from using blank as a key in a query string.
Taking a look at the specifications, ?=test seems to be a valid query string

@czp13 czp13 Aug 4, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice, thanks for checking it. I saw that the empty value is valid:

  • ?value=
    But was not sure that the empty key is also valid. Good that you checked it.

I only have one question, but to keep this clean (closed PR). I will follow up in DM about that :).

@mcollovati
mcollovati merged commit c9704e7 into main Aug 3, 2023
@mcollovati
mcollovati deleted the issue/17287_queryparams_for_empty_string branch August 3, 2023 14:36
vaadin-bot pushed a commit that referenced this pull request Aug 3, 2023
…href (#17290)

When an empty query parameters object is given to a RouterLink
the computed href contains an empty query string.
This change prevents the addition of the query string to the
href attribute when query parameters object is empty.
It also fixes parsing to return an empty query parameters
object for blank string.

Fixes #17287
vaadin-bot pushed a commit that referenced this pull request Aug 3, 2023
…href (#17290)

When an empty query parameters object is given to a RouterLink
the computed href contains an empty query string.
This change prevents the addition of the query string to the
href attribute when query parameters object is empty.
It also fixes parsing to return an empty query parameters
object for blank string.

Fixes #17287
vaadin-bot pushed a commit that referenced this pull request Aug 3, 2023
…href (#17290)

When an empty query parameters object is given to a RouterLink
the computed href contains an empty query string.
This change prevents the addition of the query string to the
href attribute when query parameters object is empty.
It also fixes parsing to return an empty query parameters
object for blank string.

Fixes #17287
vaadin-bot added a commit that referenced this pull request Aug 3, 2023
…href (#17290) (#17363)

When an empty query parameters object is given to a RouterLink
the computed href contains an empty query string.
This change prevents the addition of the query string to the
href attribute when query parameters object is empty.
It also fixes parsing to return an empty query parameters
object for blank string.

Fixes #17287

Co-authored-by: Marco Collovati <marco@vaadin.com>
vaadin-bot added a commit that referenced this pull request Aug 3, 2023
…href (#17290) (#17361)

When an empty query parameters object is given to a RouterLink
the computed href contains an empty query string.
This change prevents the addition of the query string to the
href attribute when query parameters object is empty.
It also fixes parsing to return an empty query parameters
object for blank string.

Fixes #17287

Co-authored-by: Marco Collovati <marco@vaadin.com>
vaadin-bot added a commit that referenced this pull request Aug 4, 2023
…href (#17290) (#17362)

When an empty query parameters object is given to a RouterLink
the computed href contains an empty query string.
This change prevents the addition of the query string to the
href attribute when query parameters object is empty.
It also fixes parsing to return an empty query parameters
object for blank string.

Fixes #17287

Co-authored-by: Marco Collovati <marco@vaadin.com>
@vaadin-bot

Copy link
Copy Markdown
Collaborator

This ticket/PR has been released with Vaadin 24.2.0.alpha6 and is also targeting the upcoming stable 24.2.0 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect handling of empty QueryParameters

5 participants