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

Accessing serializer.data in a validate method shouldn't return stale data #2276

Closed
pzrq opened this issue Dec 15, 2014 · 4 comments
Closed

Comments

@pzrq
Copy link
Contributor

pzrq commented Dec 15, 2014

Example test case I think illustrates the issue:
https://github.com/mathspace/django-rest-framework/compare/validation-race

If I call self.data inside a serializer.validate, the existing stale instance data ends up cached on serializer._data. The workaround that passed my tests was to access serializer.instance instead.

Curious as to thoughts on this, e.g.

  • Should serializer.data be accessible inside the validate methods, i.e. while the serializer is essentially still under construction?
  • Or should the _data cache be appropriately flushed so the test can pass?

I would lean towards the former only because the serializer.data by being partly constructed could be quite misleading.

@tomchristie
Copy link
Member

If I call self.data inside a serializer.validate

You certainly shouldn't be doing that, no. Not clear what the intention would be.
Don't know if we can protect against it easily tho.

@pzrq
Copy link
Contributor Author

pzrq commented Dec 15, 2014

Thanks for the quick reply. If you think of a reasonable way to protect
against it then great, if not feel free to close this as I reckon Google
will turn it up in case someone else runs into it.

On Monday, December 15, 2014, Tom Christie [email protected] wrote:

If I call self.data inside a serializer.validate

You certainly shouldn't be doing that, no. Not clear what the intention
would be.
Don't know if we can protect against it easily tho.


Reply to this email directly or view it on GitHub
#2276 (comment)
.

Cheers,

Peter Schmidt
[email protected] / [email protected]
+61 404 630 400

@tomchristie
Copy link
Member

Let's close it off for now - there might be further improvements we could make to restricting how the serializers can validly be called, but I can't see anything obvious at the moment.

@tomchristie
Copy link
Member

Opened #2289 to improve the constraints around this - eg raise a helpful error when this is attempted.

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

No branches or pull requests

2 participants