You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PUT calls don't fully "replace the state of the target resource" #4231
EDIT: For the current status of the issue, skip to #4231 (comment)
===
I'm having an issue implementing an Optimistic Concurrency library on an application that uses DRF to interact with the database. I'm trying to:
Confirm that the behavior I'm seeing is attributable to DRF
Confirm that this is the intended behavior
Determine if there's any practical way to overcome this behavior
I recently added optimistic concurrency to my Django application. To save you the Wiki lookup:
Every model has a version field
When an editor edits an object, they get the version of the object they're editing
When the editor saves the object, the included version number is compared to the database
If the versions match, the editor updated the latest document and the save goes through
If the versions don't match, we assume a "conflicting" edit was submitted between the time the editor loaded and saved so we reject the edit
If the version is missing, we cannot do testing and should reject the edit
I had a legacy UI talking through DRF. The legacy UI did not handle version numbers. I expected this to cause concurrency errors, but it did not. If I understand the discussion in #3648 correctly:
DRF merges the PUT with the existing record. This causes a missing version ID to be filled with the current database ID
Since this always provides a match, omitting this variable will always break an optimistic concurrency system that communicates across DRF
There are no easy options (like making the field "required") to ensure the data is submitted every time. (edit: you can workaround the issue by making it required as demonstrated in this comment)
Steps to reproduce
Setup an Optimistic Concurrency field on a model
Create a new instance and update several times (to ensure you no longer have a default version number)
Submit an update (PUT) through DRF excluding the version ID
Expected behavior
The missing version ID should not match the database and cause a concurrency issue.
Actual behavior
The missing version ID is filled by DRF with the current ID so the concurrency check passes.
Reacted by Asif Saif Uddin {"Auvi":"অভি"} and Ryan
Okay, can't promise I'll be able to review this pretty in-depth ticket immediately, as the upcoming 3.4 release takes priority. But thanks for such a detailed, well thought through issue. This'll most likely like be looked at on the scale of weeks, not days or months. If you make any progress, have any further thoughts yourself, please do update the ticket and keep us informed.
OK. I'm pretty sure my issue is the combination of two factors:
DRF doesn't require the field in the PUT (even though it is required in the model) because it has a default (version=0)
DRF merges the PUT fields with the current object (without injecting the default)
As a result, DRF uses the current (database) value and breaks the concurrency control. The second half of the issue is related to the discussion in #3648 (also cited above) and there's a (pre 3.x) discussion in #1445 that still appears to be relevant.
I'm hoping a concrete (and increasingly common) case where the default behavior is perverse will be enough to reopen the discussion about the "ideal" behavior of a ModelSerializer. Obviously, I'm only an inch deep on DRF, but my intuition is that the following behavior is appropriate for a required field and a PUT:
When using a non-partial serializer, we should either receive the value, use the default, or (if no default is available) raise a validation error. Model-wide validation should apply to only the inputs/defaults.
When using a partial serializer, we should either receive the value or fallback on the current values. Model-wide validation should apply to that combined data.
I believe the current "non-partial" serializer is really quasi-partial:
It's non-partial for fields that are required and have no default
It's partial for fields that are required and have a default (since the default isn't used)
It's partial for fields that are not required
We can't change bullet (1) above or the defaults become useless (we require the input even though we know the default). That means we have to fix the issue by changing #2 above. I agree with your argument in #2683 that:
Model defaults are model defaults. The serializer should omit the value and defer responsibility to the Model.object.create() to handle this.
To be consistent with that separation of concerns, update should create a new instance (delegating all defaults to the model) and apply the submitted values to that new instance. This results in the behavior requested in #3648.
Trying to describe the migration path helps highlight how odd the current behavior is. The end goal is to
Fix the ModelSerializer,
Add a flag for this quasi-partial state, and
Make that flag the default (for backwards compatibility)
What is the name of that flag? The current Model Serializer is actually a partial serializer that (somewhat arbitrarily) requires fields that meet the condition required==True and default==None. We can't explicitly use the partial flag without breaking backwards compatibility so we need a new (hopefully temporary) flag. I'm left with quasi_partial, but my inability to express the arbitrary requirement required==True and default==None is why it's so clear to me that this behavior should be deprecated urgently.
Thanks @anoopmalev. That'll keep me on the production branch.
After "sleeping on it" I realize there's an extra wrinkle. Everything I said should apply to the serializer's fields. If a field isn't included in the serializer, it should not be modified. In this way, all serilaizers are (and should be) partial for the non-included fields. This is a little more complicated than my "make a new instance" above.
I believe this issue needs to be reduced to a more constrained proposal in order to move forward.
Seems to broad to be actionable in it's current state.
For now I'm closing this - if anyone can reduce it to a concise, actionable statement of desired behavior then we can reconsider. Until then I think it's simply to broad.
Yes (and more). That's how I understand the partial (all fields optional) vs. non-partial (all fields required) distinction. The only time a non-partial serializer doesn't require a field is the presence of a default (narrowly or broadly defined) since the serializer can use that default if no value is provided.
The italicized section is what DRF is not currently doing and the more important change in my proposal. The current implementation just skips the field.
I had a second proposal mixed in, but it's really a separate question of how generous you want to be with the idea of a "default". The current behavior is "strict" in that only a default is treated as such. If you really wanted to reduce the amount of required data, you could make blank=True fields optional as well... assuming that an absent value is a blank value.
@claytondaley I'm using OOL with DRF since 2x this way:
classVersionModelSerializer(serializers.ModelSerializer, BaseSerializer):
_initial_version=0_version=VersionField()
def__init__(self, *args, **kwargs):
super(VersionModelSerializer, self).__init__(*args, **kwargs)
# version field should not be required if there is no objectifself.instanceisNoneand'_version'inself.fieldsand\
getattr(self, 'parent', None) isNone:
self.fields['_version'].read_only=Trueself.fields['_version'].required=False# version field is required while updating instanceifself.instanceisnotNoneand'_version'inself.fields:
self.fields['_version'].required=Trueifself.instanceisnotNoneandhasattr(self.instance, '_version'):
self._initial_version=self.instance._versiondefvalidate__version(self, value):
ifself.instanceisnotNone:
ifnotvalueandnotisinstance(value, int):
raiseserializers.ValidationError(_(u"This field is required"))
returnvalue# more code & helpers
it works just great with all kind of business logic and never caused any problem.
You've hard-coded the OOL into the serializer. This is the wrong place to do it because you have a race condition. Parallel updates (with the same prior version) would all pass at the serializer... but only one would win at the save action.
I'm using django-concurrency which puts the OOL logic into the save action (where it belongs). Basically UPDATE... WHERE version = submitted_version. This is atomic so there's no race condition. However, it exposes a flaw in the serialization logic::
If default is set on a field in the model, DRF sets required=False. The (valid) idea is that DRF can use that default if no value is submitted.
If that field is missing, however, DRF doesn't use the default. Instead it merges the submitted data with the current version of the object.
When we don't require the field, we do so because we have a default to use. DRF doesn't fulfill that contract because it doesn't use the default... it uses the existing value.
The underlying issue was discussed before, but they didn't have a nice, concrete case. OOL is that ideal case. The existing value of a version field always passes OOL so you can bypass the entire OOL system by leaving out version. That's (obviously) not the desired behavior of an OOL system.
Up to you guys, but I want to make sure you're clear that this is not a Ticket to add concurrency support. The real issue is that a single serializer cannot correctly validate both a PUT and POST in the current architecture. Concurrency just provided the "failing test".
TL;DR You can see why this issue is blocked by starting at Tom's proposed fix.
In summary, the proposed solution is to make all fields required for a PUT request. There are (at least) two problems with this approach:
Serializers think in actions not HTTP methods so there isn't a one-to-one mapping. The obvious example is create because it's shared by PUT and POST. Note that create-by-PUT is disabled by default so the proposed fix is probably better than nothing.
The real issue is how we handle missing data and the basic proposal in #3648, #4703, and here remain the right solution. We can support all of the HTTP modes (including create-by-PUT) if we introduce a concept like if_missing_use_default. My original proposal presented it as a replacement for partial, but it's easier (and may be necessary) to think of it as an orthogonal concept.
if we introduce a concept like if_missing_use_default.
There's nothing preventing anyone from implementing either this, or a strict "require all fields" as a base serializer class, and wrapping that up as a third party library.
My opinion is that a strict "require all fields" mode might also be able to make it into core, it's very clear obvious behavior, and I can see why that'd be useful.
I'm not convinced that a "allow fields to be optional, but replace everything, using model defaults if they exist" - That seems like it'd present some very counter-intuitive behavior (eg. "created_at" fields, that automatically end up updating themselves). If we want a stricter behavior, we should just have a stricter behavior.
Either way around, the right way to approach this is to validate it as a third party package, then update our docs so we can link to that.
Alternatively, if you're convinced that we're missing a behavior from core that our users really do need, then you're welcome to make a pull request, updating the behavior and the documentation, so we can assess the merits in a very concrete way.
Happy to take pull requests as a starting point for this, and even happier to include a third party package demonstrating this behavior.
coming around to the value in having an option of PUT-is-strict behavior.
This still stands. I think we could consider that aspect in core, if someone cares enough about it to make a pull request along those lines. It'd need to be an optional behavior.
That seems like it'd present some very counter-intuitive behavior (eg. "created_at" fields, that automatically end up updating themselves).
A created_at field should be read_only (or excluded from the serializer). In both of these cases, it would be unchanged (the normal serializer behavior). In the counter-intuitive case that the field is not read-only in the serializer, you would get the counter-intuitive behavior of automatically changing it.
Happy to take pull requests as a starting point for this, and even happier to include a third party package demonstrating this behavior.
Absolutely. The "use defaults" variation is an ideal case for a 3rd party package because the change is a trivial wrapper around (one method of) the existing behavior and (if you buy into the defaults argument) works for all non-partial serializers.
tomchristie closed this 4 hours ago
Perhaps you'd consider adding a label like "PR Welcome" or "3rd Party Plugin" and leaving valid/acknowledged issues like this open. I often search open issues to see if a problem has already been reported and its progress towards resolution. I perceive closed issues as "invalid" or "fixed". Mixing a few "valid but closed" issues into the thousands of invalid/fixed issues doesn't invite efficient searching (even if you knew they might be there).
Perhaps you'd consider adding a label like "PR Welcome" or "3rd Party Plugin"
That'd be reasonable enough, but we'd like our issue tracker to reflect active or actionable work on the project itself.
It's really important for us to try to keep our issues tightly scoped. Changing priorities might mean that we sometime choose to reopen issues that we've previously closed. Right now I think this has fallen out of the "the core team want to address this in the immediate future".
If it comes up repeatedly, and there continues to be no third party solution, then perhaps we would reassess it.
Activity
lovelydinosaur commentedon Jun 30, 2016
Okay, can't promise I'll be able to review this pretty in-depth ticket immediately, as the upcoming 3.4 release takes priority. But thanks for such a detailed, well thought through issue. This'll most likely like be looked at on the scale of weeks, not days or months. If you make any progress, have any further thoughts yourself, please do update the ticket and keep us informed.
claytondaley commentedon Jun 30, 2016
OK. I'm pretty sure my issue is the combination of two factors:
As a result, DRF uses the current (database) value and breaks the concurrency control. The second half of the issue is related to the discussion in #3648 (also cited above) and there's a (pre 3.x) discussion in #1445 that still appears to be relevant.
I'm hoping a concrete (and increasingly common) case where the default behavior is perverse will be enough to reopen the discussion about the "ideal" behavior of a ModelSerializer. Obviously, I'm only an inch deep on DRF, but my intuition is that the following behavior is appropriate for a required field and a PUT:
We can't change bullet (1) above or the defaults become useless (we require the input even though we know the default). That means we have to fix the issue by changing #2 above. I agree with your argument in #2683 that:
To be consistent with that separation of concerns, update should create a new instance (delegating all defaults to the model) and apply the submitted values to that new instance. This results in the behavior requested in #3648.
Trying to describe the migration path helps highlight how odd the current behavior is. The end goal is to
What is the name of that flag? The current Model Serializer is actually a partial serializer that (somewhat arbitrarily) requires fields that meet the condition
required==True and default==None. We can't explicitly use thepartialflag without breaking backwards compatibility so we need a new (hopefully temporary) flag. I'm left withquasi_partial, but my inability to express the arbitrary requirementrequired==True and default==Noneis why it's so clear to me that this behavior should be deprecated urgently.anoopmalav commentedon Jul 1, 2016
You can add
extra_kwargsin serializer's Meta, makingversiona required field.claytondaley commentedon Jul 1, 2016
Thanks @anoopmalev. That'll keep me on the production branch.
After "sleeping on it" I realize there's an extra wrinkle. Everything I said should apply to the serializer's fields. If a field isn't included in the serializer, it should not be modified. In this way, all serilaizers are (and should be) partial for the non-included fields. This is a little more complicated than my "make a new instance" above.
lovelydinosaur commentedon Jul 4, 2016
I believe this issue needs to be reduced to a more constrained proposal in order to move forward.
Seems to broad to be actionable in it's current state.
For now I'm closing this - if anyone can reduce it to a concise, actionable statement of desired behavior then we can reconsider. Until then I think it's simply to broad.
claytondaley commentedon Jul 4, 2016
Here's a concise proposal... for a non-partial serializer:
blankand/ornullFor clarity, validation is run on the final product of this process.
lovelydinosaur commentedon Jul 4, 2016
Ie you want to set
required=Trueon any serializer field that doesn't have a model default, for updates?Have I got that correct?
claytondaley commentedon Jul 4, 2016
Yes (and more). That's how I understand the
partial(all fields optional) vs.non-partial(all fields required) distinction. The only time anon-partialserializer doesn't require a field is the presence of a default (narrowly or broadly defined) since the serializer can use that default if no value is provided.The italicized section is what DRF is not currently doing and the more important change in my proposal. The current implementation just skips the field.
I had a second proposal mixed in, but it's really a separate question of how generous you want to be with the idea of a "default". The current behavior is "strict" in that only a
defaultis treated as such. If you really wanted to reduce the amount of required data, you could makeblank=Truefields optional as well... assuming that an absent value is a blank value.pySilver commentedon Jul 25, 2016
@claytondaley I'm using OOL with DRF since 2x this way:
it works just great with all kind of business logic and never caused any problem.
claytondaley commentedon Jul 30, 2016
Was this left closed on accident? I responded to the specific question and didn't hear a reason what was wrong with the proposal.
pySilver commentedon Jul 30, 2016
@claytondaley why OOL should be a part of DRF? Check my code – it works just find in a large app (1400 tests).
VersionFieldis just anIntegerField.claytondaley commentedon Jul 30, 2016
You've hard-coded the OOL into the serializer. This is the wrong place to do it because you have a race condition. Parallel updates (with the same prior version) would all pass at the serializer... but only one would win at the save action.
I'm using
django-concurrencywhich puts the OOL logic into the save action (where it belongs). BasicallyUPDATE... WHERE version = submitted_version. This is atomic so there's no race condition. However, it exposes a flaw in the serialization logic::required=False. The (valid) idea is that DRF can use that default if no value is submitted.When we don't require the field, we do so because we have a default to use. DRF doesn't fulfill that contract because it doesn't use the default... it uses the existing value.
The underlying issue was discussed before, but they didn't have a nice, concrete case. OOL is that ideal case. The existing value of a version field always passes OOL so you can bypass the entire OOL system by leaving out version. That's (obviously) not the desired behavior of an OOL system.
pySilver commentedon Jul 30, 2016
@claytondaley
Did I? Have you found any OOL logic in my serializer beside field requirement?
Sry, I just cant see where is the race condition here.
I'm also using
django-concurrency:) But thats model level, not serializer. On the serializer level you just need to:83 remaining items
carltongibson commentedon Sep 28, 2017
I'm going to de-milestone this for now. We can reassess after v3.7
claytondaley commentedon Sep 28, 2017
Up to you guys, but I want to make sure you're clear that this is not a Ticket to add concurrency support. The real issue is that a single serializer cannot correctly validate both a PUT and POST in the current architecture. Concurrency just provided the "failing test".
[-]Optimistic Concurrency and PUT Logic[/-][+]PUT calls don't fully "replace the state of the target resource"[/+]claytondaley commentedon Sep 28, 2017
TL;DR You can see why this issue is blocked by starting at Tom's proposed fix.
In summary, the proposed solution is to make all fields required for a
PUTrequest. There are (at least) two problems with this approach:createbecause it's shared byPUTandPOST. Note that create-by-PUTis disabled by default so the proposed fix is probably better than nothing.PUT(a sentiment shared by PUT does not completely replace object #3648, ModelSerializer fields don't obey default from the model #4703). If a nillable field is absent, we know it can be None. If a field with a default is absent, we know we can use the default.PUTs actually have the same (Model-derived) field requirements asPOST.The real issue is how we handle missing data and the basic proposal in #3648, #4703, and here remain the right solution. We can support all of the HTTP modes (including create-by-
PUT) if we introduce a concept likeif_missing_use_default. My original proposal presented it as a replacement forpartial, but it's easier (and may be necessary) to think of it as an orthogonal concept.lovelydinosaur commentedon Sep 29, 2017
There's nothing preventing anyone from implementing either this, or a strict "require all fields" as a base serializer class, and wrapping that up as a third party library.
My opinion is that a strict "require all fields" mode might also be able to make it into core, it's very clear obvious behavior, and I can see why that'd be useful.
I'm not convinced that a "allow fields to be optional, but replace everything, using model defaults if they exist" - That seems like it'd present some very counter-intuitive behavior (eg. "created_at" fields, that automatically end up updating themselves). If we want a stricter behavior, we should just have a stricter behavior.
Either way around, the right way to approach this is to validate it as a third party package, then update our docs so we can link to that.
Alternatively, if you're convinced that we're missing a behavior from core that our users really do need, then you're welcome to make a pull request, updating the behavior and the documentation, so we can assess the merits in a very concrete way.
Happy to take pull requests as a starting point for this, and even happier to include a third party package demonstrating this behavior.
lovelydinosaur commentedon Sep 29, 2017
This still stands. I think we could consider that aspect in core, if someone cares enough about it to make a pull request along those lines. It'd need to be an optional behavior.
claytondaley commentedon Sep 29, 2017
A
created_atfield should beread_only(or excluded from the serializer). In both of these cases, it would be unchanged (the normal serializer behavior). In the counter-intuitive case that the field is not read-only in the serializer, you would get the counter-intuitive behavior of automatically changing it.Absolutely. The "use defaults" variation is an ideal case for a 3rd party package because the change is a trivial wrapper around (one method of) the existing behavior and (if you buy into the defaults argument) works for all non-partial serializers.
Perhaps you'd consider adding a label like "PR Welcome" or "3rd Party Plugin" and leaving valid/acknowledged issues like this open. I often search open issues to see if a problem has already been reported and its progress towards resolution. I perceive closed issues as "invalid" or "fixed". Mixing a few "valid but closed" issues into the thousands of invalid/fixed issues doesn't invite efficient searching (even if you knew they might be there).
lovelydinosaur commentedon Sep 29, 2017
That'd be reasonable enough, but we'd like our issue tracker to reflect active or actionable work on the project itself.
It's really important for us to try to keep our issues tightly scoped. Changing priorities might mean that we sometime choose to reopen issues that we've previously closed. Right now I think this has fallen out of the "the core team want to address this in the immediate future".
If it comes up repeatedly, and there continues to be no third party solution, then perhaps we would reassess it.
lovelydinosaur commentedon Sep 29, 2017
A bit more context on the issue management style - https://www.dabapps.com/blog/sustainable-open-source-management/