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: add null check in deepFindPathToProperty function #137

Merged
merged 1 commit into from
Sep 20, 2024

Conversation

igwejk
Copy link
Contributor

@igwejk igwejk commented Dec 9, 2023

Resolves #58


Before the change?

The deepFindPathToProperty function would occasionally fail with TypeError: Cannot read properties of null (reading 'hasOwnProperty').

After the change?

The error has been fixed:

  • src/object-helpers.ts: The deepFindPathToProperty function was refactored and its return type updated from string[] to string[] | null. The refactoring includes the introduction of a new type TreeNode and several new helper functions like getDirectPropertyPath, makeTreeNodeChildrenFromData, and findPathToObjectContainingProperty. The findPaginatedResourcePath function was also updated to accommodate these changes.
  • test/typescript-validate.ts: The pageInfoBackwaredExported function was renamed to pageInfoBackwardExported for correct spelling.
  • test/object-helpers.test.ts: New tests were added for the findPaginatedResourcePath function to validate its behavior in different scenarios.

Pull request checklist

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)

Does this introduce a breaking change?

Please see our docs on breaking changes to help!

  • Yes
  • No

@gr2m gr2m requested a review from davelosert December 9, 2023 19:36
@davelosert
Copy link
Collaborator

Hey @igwejk, thank you very much for the PR and for trying to fix #58!

Could you elaborate a bit more as to why you chose to entirely reimplement the deepFindPathToProperty-Function with a lot more code than we had previously, given that you were only trying to fix errors with possible null-values of certain fields?

It seems like you are covering other use-cases with this implementation as well and I don't quite understand 'which' and 'why' yet. 🙂

@igwejk
Copy link
Contributor Author

igwejk commented Dec 12, 2023

👋 Hey @davelosert,

I honestly got carried away 🤦‍♂️ when considering other capabilities I would like the plugin to have

  • potentially handle queries with multiple pageInfo that can be arbitrarily nested
  • do the search without worrying about overflowing the call stack

Those are needs I came across first when querying repository statistics across an enterprise (and how I came about the motivating issue). This change also serves as a first baby step towards those goals.

Let me clearly say that I understand your line of question, and it makes total sense. So, I can offer to trim things down to a simple null check with additional tests if you would prefer that.

@davelosert
Copy link
Collaborator

Hey @igwejk,

as already discussed internally, we've chosen to not support multiple (or nested) pagination with this library on purpose, as this can lead to all sort of problems (you can find more info and a link to a more in depth-explanation in the README Section: Unsupported Nested Pagination.

As I also don't see a GraphQL Response that would be so nested that the current, recursive implementation would cause a overflowing call stack, I'd rather keep the current, more lightweight implementation and just use a null-check to avoid the error reported in #58.

Still, thank you again for the contribution and your work here! 👍🏻

@karpikpl
Copy link

Hey @igwejk,

as already discussed internally, we've chosen to not support multiple (or nested) pagination with this library on purpose, as this can lead to all sort of problems (you can find more info and a link to a more in depth-explanation in the README Section: Unsupported Nested Pagination.

As I also don't see a GraphQL Response that would be so nested that the current, recursive implementation would cause a overflowing call stack, I'd rather keep the current, more lightweight implementation and just use a null-check to avoid the error reported in #58.

Still, thank you again for the contribution and your work here! 👍🏻

Was that null check ever implemented?

@igwejk igwejk closed this Sep 20, 2024
@igwejk igwejk reopened this Sep 20, 2024
Previously the `deepFindPathToProperty` function would occasionally fail with `TypeError: Cannot read properties of null (reading 'hasOwnProperty')`. This change fixes that by adding a null check to the `findPaginatedResourcePath` function.

Resolves octokit#58
@igwejk
Copy link
Contributor Author

igwejk commented Sep 20, 2024

@karpikpl It was fixed with with an addition of support for deep nested pageInfo which was not required. I've now updated the PR to keep only the null check.

@wolfy1339 wolfy1339 added the Type: Bug Something isn't working as documented label Sep 20, 2024
@wolfy1339 wolfy1339 changed the title Improve implementation of deepFindPathToProperty function fix: add null check in deepFindPathToProperty function Sep 20, 2024
@wolfy1339 wolfy1339 enabled auto-merge (squash) September 20, 2024 14:21
@wolfy1339 wolfy1339 disabled auto-merge September 20, 2024 14:21
@wolfy1339 wolfy1339 enabled auto-merge (squash) September 20, 2024 14:21
@wolfy1339 wolfy1339 merged commit cfda527 into octokit:main Sep 20, 2024
6 checks passed
Copy link

🎉 This PR is included in version 5.2.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

responseData,
"pageInfo",
);
if (paginatedResourcePath.length === 0) {
if (paginatedResourcePath === null || paginatedResourcePath.length === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Did this line fix #58? The stacktrace and the description looks like it happens in deepFindPathToProperty implementation, not for the returned value.

This causes currentValue.hasOwnProperty(searchProp) to throw TypeError: Cannot read properties of null (reading 'hasOwnProperty') error.

if (currentValue.hasOwnProperty(searchProp)) {
return currentPath;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

kachick added a commit to kachick/plugin-paginate-graphql.js that referenced this pull request Sep 24, 2024
wolfy1339 pushed a commit that referenced this pull request Oct 11, 2024
* chore: add failing test for response having null value

* Revert "fix: add `null` check in `deepFindPathToProperty` function (#137)"

This reverts commit cfda527.

* fix: hasOwnProperty error in deepFindPathToProperty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released Type: Bug Something isn't working as documented
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

[BUG]: deepFindPathToProperty() throws TypeError if graphql response field is null
5 participants