Skip to content

support skipping last node by index if parent node is a list #16

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bentsku
Copy link
Contributor

@bentsku bentsku commented Jul 2, 2025

I've encountered the issue in 2 separates occasions, where I could not set a snapshot skip if the value I wanted to escape was an item inside a list, and it was the last "node".

For example, a list in a snapshot would look like:

{
  "test": [
    "value1",
    "value2"
  ]
}

And we would a different value for the "value2" item, setting paths=["$..test[1]"] would not be skipped, it was ignored by our "skip_snapshot_verify" utility.

See those 2 PRs, where I had to transform the list returned into a dictionary so I could skip by index:

Sometimes, we'd just skip the whole list because of the issue like in localstack/localstack#12831, which took the snapshot skip for existing tests in the file.

For example, skipping a dictionary key from a dictionary living inside a list, but only in one list item would work, if the index as part of the path but not the last part.

I've tried to implemented and quickly realized why we had not done it yet. If you refer to a value by index, and then mutate said list to remove it, the next skip paths will refer to a wrong index, as the list has shifted.

One solution to this is to replace those values we want to remove by placeholders, and remove them all at once. This way, we're not dealing with indexes anymore.

Not sure if it's the cleanest solution, the _remove_skip_verification_paths is starting to be a little bit crowded.

I also thought about updating the reporting to have better suggestion around indexes instead of suggesting recursive descent pretty often, but this is another can of worms with deduplication and reasoning about the diff (what if we have 8 items that are wrong in an array of 10 items, do we want to suggest to individually skip the 8 items, each in their own line? I believe we could use the [index1,index2,…] format to skip multiple indexes, but it looks our JSONPath library does not support it. But again, this could be tackled in a follow up).

At least this PR enables better JSONPath support in our skip paths for "advanced" usage.

@bentsku bentsku self-assigned this Jul 2, 2025
@@ -218,7 +220,7 @@ def _assert_all(
self.skip_verification_paths = skip_verification_paths or []
if skip_verification_paths:
SNAPSHOT_LOGGER.warning(
f"Snapshot verification disabled for paths: {skip_verification_paths}"
"Snapshot verification disabled for paths: %s", skip_verification_paths
Copy link
Contributor Author

@bentsku bentsku Jul 2, 2025

Choose a reason for hiding this comment

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

see https://docs.astral.sh/ruff/rules/logging-f-string/
note: could also be added to the project rules as we did for LocalStack

@bentsku bentsku force-pushed the support-skip-list-last-node branch from 22179f0 to 60834ef Compare July 2, 2025 12:19
@bentsku bentsku force-pushed the support-skip-list-last-node branch from 60834ef to c827c51 Compare July 2, 2025 12:21
@bentsku bentsku requested review from dominikschubert and tiurin July 2, 2025 15:17
@bentsku bentsku marked this pull request as ready for review July 2, 2025 15:17
@bentsku bentsku changed the title support skipping last node by index if node is a list support skipping last node by index if parent node is a list Jul 2, 2025
@bentsku bentsku closed this Jul 4, 2025
@bentsku bentsku reopened this Jul 4, 2025
@bentsku
Copy link
Contributor Author

bentsku commented Jul 4, 2025

Sorry, the PR got closed with the automatic linked of GitHub with another PR. I didn't know it also worked with PRs and not only issues 🤔

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

Successfully merging this pull request may close these issues.

1 participant