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

possible bug in document_etag function #16

Closed
ghost opened this issue Feb 22, 2013 · 7 comments
Closed

possible bug in document_etag function #16

ghost opened this issue Feb 22, 2013 · 7 comments
Labels

Comments

@ghost
Copy link

ghost commented Feb 22, 2013

please use in function document_etag something like this:
json.dumps(value, sort_keys=True) for dict,
instead of
str(value)

In the second case - order (and hash) will be different when script restart, or between different server instances

@nicolaiarocci
Copy link
Member

Excellent, thanks! Will review as soon as possible.

@ghost
Copy link
Author

ghost commented Feb 22, 2013

And little addon - json.dumps not support bson.ObjectIt, but bson.json_util does
I think something like this will work:
from bson.json_util import dumps
dumps(value, sort_keys=True)

@nicolaiarocci
Copy link
Member

Good call. However, have you actually seen the documents sorted differently upon restart/different server instances? I ask because I was under the impression that PyMongo/BSON documents use the SON class, which is basically a backport of Py3x/27 OrderedDict (I didn't verify it carefully though).

@LarryEitel
Copy link

Not working for me:

from bson.json_util import dumps

Traceback (most recent call last):
  File "<string>", line 1, in <fragment>
ImportError: cannot import name dumps

@nicolaiarocci
Copy link
Member

@LarryEitel it should work:

from bson.json_util import dumps

def document_etag(value):
    h = hashlib.sha1()
    h.update(dumps(value, sort_keys=True))
    return h.hexdigest()

Passes all tests (well except test_document_etag of course, which is not updated yet)

@ghost
Copy link
Author

ghost commented Feb 22, 2013

"However, have you actually seen the documents sorted differently upon restart/different server instances?"

Yes. I draw form, ajaxing data from form to eve server, and it pass till validation error (required field missing, in my case). I can do this several times with the same result.
Then, I Ctrl-C server and start it again (not closing form) and when I submit my form again, I ran into 412 error from server.

@nicolaiarocci
Copy link
Member

Gotcha, will fix.

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

No branches or pull requests

2 participants