support skipping last node by index if parent node is a list #16
+167
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
And we would a different value for the
"value2"
item, settingpaths=["$..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.