-
Notifications
You must be signed in to change notification settings - Fork 36
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
Ordering JSON keys of Python dictionary snapshots #618
Comments
It looks like we don't treat OrderedDicts any differently from regular dicts. Arguably we should do an instanceof OrderedDict check and if it's ordered, preserve the original key order rather than apply our key sort: https://github.com/tophat/syrupy/blob/1d3ae5779e1a19f998895f3812187e0ddc57fd79/src/syrupy/extensions/amber/serializer.py#L172 This would be a breaking change though but I think it makes sense. |
Makes sense! In the meantime, we have converted the dictionary to a list of key, value pairs, which seems to work ok. It would be cleaner to have it as JSON dictionary key, values but the workaround is ok for the time being. Thanks for the help! |
Why would it be a breaking change? |
This could be something that I can take a look at contributing too |
It'd be a breaking change to change the default snapshot behaviour because folks would have to run That being said, a non-breaking approach would be to extend the data serializer class or add a feature for custom key ordering. |
#673 will ensure OrderedDict keys are kept ordered which means you can either switch to using OrderedDict in your code, or just wrap the value in an OrderedDict prior to snapshoting. Something like:
|
🎉 This issue has been resolved in version 4.0.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Thanks for the great library!
We'd like to order the JSON keys alphabetically by information in the value, not just by the key. We are using the JSONSnapshotExtension. The reason is that we'd like to preserve sensible diffs in version control even if the names of the keys change, since the reordering appears as large deletions and additions of code.
For example, we'd like to make sure the snapshotted JSON is sorted by the value of the "Type" (first a, then b) etc...
Not obvious how to do this because Python dictionaries are unordered, and using
OrderedDict
doesn't seem to preserve order.Thanks!
The text was updated successfully, but these errors were encountered: