Fix unflatten crashing on keys that contain a dollar sign#11
Open
c-tonneslan wants to merge 1 commit into
Open
Fix unflatten crashing on keys that contain a dollar sign#11c-tonneslan wants to merge 1 commit into
c-tonneslan wants to merge 1 commit into
Conversation
Round-tripping a dict like `{"foo$bar": 5}` blows up:
>>> from json_flatten import flatten, unflatten
>>> unflatten(flatten({"foo$bar": 5}))
ValueError: too many values to unpack (expected 2)
flatten produces `{"foo$bar$int": "5"}` correctly, but unflatten then
calls `rsplit("$", 2)` and gets back three pieces instead of two. The
greedy split was the only thing wrong; a `rsplit("$", 1)` keeps the
type suffix and leaves the rest of the key alone, so "foo$bar$int"
becomes ("foo$bar", "int"). Added two regression cases to the
parametrized test.
Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Found this while reading the code. Round-tripping a dict whose key has a
$in it crashes:flattendoes the right thing and produces{"foo$bar$int": "5"}, butunflattenthen callsrsplit("$", 2)and gets back three pieces (["foo", "bar", "int"]) instead of the two the unpack expects. The greedy split was the only issue;rsplit("$", 1)keeps the type suffix at the end and leaves the rest of the key intact, so"foo$bar$int"becomes("foo$bar", "int")and the round-trip works.Added two parametrized regression cases. The existing
dollar_signs_that_are_not_type_indicatorscase keeps passing because its value is a plain string, so no$typeis ever appended.