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: Make querystrings consistent #20302

Merged
merged 5 commits into from
Oct 5, 2022
Merged

Conversation

sainthkh
Copy link
Contributor

@sainthkh sainthkh commented Feb 22, 2022

User facing changelog

cy.visit() and cy.request generate the same querystring.

Additional details

  • Why was this change necessary? => Given the same qs argument, cy.visit() and cy.request() created different querystrings.
  • What is affected by this change? => N/A
  • Any implementation details to explain? => I used mergeUrlWithParams function used in cy.visit() to generate querystring in cy.request. I decided it this way because cy.visit() behavior is consistent with the default URLSearchParams class.

How has the user experience changed?

Before:

cy.visit({ url: 'https://example.com', qs: { a: [1, 2], b: 3 } }) // => https://example.com?a=1%2C2&b=3
cy.request({ url: 'https://example.com', qs: { a: [1, 2], b: 3 } }) // => https://example.com?a%5B0%5D=1&a%5B1%5D=2&b=3

After:

cy.visit({ url: 'https://example.com', qs: { a: [1, 2], b: 3 } }) 
cy.request({ url: 'https://example.com', qs: { a: [1, 2], b: 3 } })
// Both => https://example.com?a=1%2C2&b=3

Why this PR doesn't close the issue?

AFAIK, there are 3 APIs in Cypress that use query string object. Before this PR, they all used the different libraries.

This is quite a mess because the standard library(URL and URLSearchParams classes) came out really late with Chrome 49 in 2016 while the libraries above are created in 2011(qs, nodejs default querystring), 2014(querystringify, url-parse).

And Cypress was created before 2016, it's natural that those libraries are used.

This PR deprecates qs in cy.request, and uses url-parse for both cy.visit and cy.request. It means cy.intercept still uses URLSearchParams.

I chose this way because there are 2 ways to merge parameters in URLSearchParams: append and set. I couldn't be sure mimicking current behavior is right.

Currently, when you provide parameters in both url and qs and names are conflicted, values in qs are used like below.

cy.visit({ url: '/?a=123&b=foo', qs: { a: 456 } }) // => /?a=456&b=foo

(It's happening because we simply merge objects like below:)

static mergeUrlWithParams (url, params) {
url = new UrlParse(url, null, true)
url.set('query', _.merge(url.query || {}, params))
return url.toString()
}

But when we use URLSearchParams, we can preserve both of them with append method like below:

const params = new URLSearchParams({ a: 123, b: 'foo' })
params.append('a', 456)
console.log(params.toString()) // a=123&b=foo&a=456

I think it is the better behavior. So, I decided to stop this PR here and seek your opinions about this.

PR Tasks

  • Have tests been added/updated?
  • [na] Has the original issue (or this PR, if no issue exists) been tagged with a release in ZenHub? (user-facing changes only)
  • [na] Has a PR for user-facing changes been opened in cypress-documentation?
  • [na] Have API changes been updated in the type definitions?
  • [na] Have new configuration options been added to the cypress.schema.json?

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Feb 22, 2022

Thanks for taking the time to open a PR!

@sainthkh sainthkh marked this pull request as ready for review February 22, 2022 03:26
@sainthkh sainthkh requested a review from a team as a code owner February 22, 2022 03:26
@sainthkh sainthkh requested review from jennifer-shehane and removed request for a team February 22, 2022 03:26
@jennifer-shehane
Copy link
Member

@sainthkh This looks like this change would be a breaking change, correct?

@sainthkh
Copy link
Contributor Author

sainthkh commented Feb 23, 2022

@jennifer-shehane It's somewhere between.

If we define qs behavior is correct, cy.request's behavior is also right, but different. In this case, it's a breaking change.

If we define qs behavior is wrong because it doesn't match URLSearchParams's behavior, then cy.request's behavior is also wrong. In this case, it's a bug.

@jennifer-shehane
Copy link
Member

@sainthkh Thanks for the clarification. I believe we'll need to sync on how we plan to proceed. Right now we don't have as much time to dedicate to review of this, so please be patient.

@jennifer-shehane jennifer-shehane added the type: breaking change Requires a new major release version label Apr 27, 2022
@BlueWinds BlueWinds self-requested a review June 16, 2022 20:12
@BlueWinds
Copy link
Contributor

I like the change - the consistency is valuable, but do think it's breaking. Currently proposing to the team that we merge this when we're ready to release another breaking change (but not cause 11.x immediately for this purpose alone).

Copy link
Contributor

@BlueWinds BlueWinds left a comment

Choose a reason for hiding this comment

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

We have a merge conflict, but otherwise looks good to me, with tests covering the changes.

@BlueWinds BlueWinds removed the request for review from jennifer-shehane June 21, 2022 16:16
Copy link
Contributor

@rachelruderman rachelruderman left a comment

Choose a reason for hiding this comment

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

I ❤️ consistency, thank you for the contribution and very detailed description!

@cypress
Copy link

cypress bot commented Jun 28, 2022



Test summary

37689 0 456 0Flakiness 9


Run details

Project cypress
Status Passed
Commit 6ccb1ce
Started Jun 27, 2022 11:48 PM
Ended Jun 28, 2022 12:05 AM
Duration 17:28 💡
OS Linux Debian - 10.11
Browser Multiple

View run in Cypress Dashboard ➡️


Flakiness

actions/click.cy.js Flakiness
1 ... > scroll-behavior > can scroll to and click elements in html with scroll-behavior: smooth
2 ... > scroll-behavior > can scroll to and click elements in html with scroll-behavior: smooth
xhr.cy.js Flakiness
1 ... > logs Method, Status, URL, and XHR
2 ... > logs response
3 ... > logs response
This comment includes only the first 5 flaky tests. See all 9 flaky tests in the Cypress Dashboard.

This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

@BlueWinds BlueWinds changed the base branch from develop to 11.0.0 October 5, 2022 18:36
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Dec 6, 2022

Released in 12.0.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v12.0.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Dec 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: breaking change Requires a new major release version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cy.request(), cy.visit() and cy.intercept() query strings are inconsistent
5 participants