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

Current implementation of parsing query params from href can lead to no results being returned #61

Open
WojciechMatuszewski opened this issue Jun 18, 2019 · 4 comments
Labels
bug Something isn't working

Comments

@WojciechMatuszewski
Copy link
Contributor

Describe the bug
Current implementation parses and creates queryParams from this.href (see page.ts or cursor-based-page.ts). That is completely fine, however, we should be very careful how some characters get encoded by axios library.

To Reproduce
Steps to reproduce the behavior:

  1. Perform any kind of search which returns Page and there are more results to fetch (query has to contain at least one space)
  2. invoke getNextPage() on that Page
  3. See results prop on returned Page is an empty array.
const page = await spotify.searchTracks('lil pe');
const page2 = await page.getNextPage();
console.log(page.hasNext(), page2.items.length); // true, 0

Expected behavior
returned Page contains newly fetched items.

Additional context
The cause of this bug was not obvious for me at first.
Call to getNextPage() uses queryParams which parses them from this.href.
That causes some params (query in this case) to have some symbols double parsed by axios (once from initial request, once from the subsequent request which passes already parsed URL params to axios)

Example:

// q=lil+pe => everything is fine (notice lil+pe) 👍
const page = await spotify.searchTracks('lil pe');

/*
query=lil%2Bpe
We passed {query: 'lil+pe'} instead of {query: 'lil pe'} to axios params.
That caused axios to parse + as %2B which causes Spotify to return no results
*/
const page2 = await page.getNextPage();
@JoseRenan JoseRenan added the bug Something isn't working label Jun 18, 2019
@JoseRenan
Copy link
Member

Nice observation! So, if I understand the problem, the solution could be decoding this value before getting the query params, right?

@WojciechMatuszewski
Copy link
Contributor Author

WojciechMatuszewski commented Jun 20, 2019

Not sure, the easiest solution would be to just replace + with spaces and then splitting this.href. But, imo, that is more like a bandaid than a solution

@JoseRenan
Copy link
Member

JoseRenan commented Jun 21, 2019

Hmm, we could keep the raw query params in the page object, I think it's the most elegant solution 🤔

@JRobsonJr
Copy link
Member

JRobsonJr commented Jun 22, 2019

I think using the decodeURI function would be the best solution in this case. It should work just fine for this purpose, right?

Edit: Just saw that Spotify itself encodes spaces as +. I think that we need to do as @JoseRenan suggested, because replacing + with spaces could modify the original intent of the person making the search (if they actually put + in their query).

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

No branches or pull requests

3 participants