locked
Ignoring properties not explicitly specified in Update requests RRS feed

  • Question

  • Hi,

    I've implemented an API with a custom data source provider based on the code from the OData Provider Toolkit. Basically it seems to run very smoothly, but there is still a little snag which I would like to remove if any possible.

    Supposing I have a "Project" resource type with three properties: ID(an integer), Name (a string) and Active (a boolean).

    The Data service holds one such Project resource with the following value:

    ID=123

    Name="Lunlar Landing"

    Active="true"

    Now I'd like to update my project resource via OData so that the name is changed to "Mars Mission". My PUT request has the following content:

     

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
       <content type="application/xml">
        <m:properties>
               <d:Name>Mars Mission</d:Name>                     
        </m:properties>
      </content>
    </entry>

    Now, because I have not explicitly specified a value for the "Active" property, one would think that the property is not altered. But in fact it is changed to it's the default value, false. This is certainly not what the average developer would have in mind.

    I could cope with this, but third party developers are hopefully also going to use my API and there is a great risk for data corruption if they get this wrong.

    Is there any way to change this behavior so that properties which are not explicitly specified in the PUT request are simply ignored?

    Thanks,

    Adrian

     

     

     

     

    Tuesday, August 3, 2010 6:52 PM

Answers

  • Hi,

    PUT operation means "replace" or "overwrite". You can see that in the IUpdatable/IDataServiceUpdateProvider. When PUT operation is executed the service will first call your ResetResource method and then SetValue on all the properties which were in the payload. Since the ResetResource should reset all properties to their default values, the end result is what looks like an overwrite operation.

    MERGE operation on the other hand doesn't have the "overwrite" meaning. It will behave the same as PUT except that it won't call the ResetResource. As a result only the properties which were in the payload will be applied via SetValue, nothing else will be done.

    So you want to use MERGE instead of PUT. If you're using .NET client it will use MERGE by default, you can ask for PUT through SaveChangesOptions.ReplaceOnUpdate flag.

    Thanks,


    Vitek Karas [MSFT]
    Tuesday, August 3, 2010 7:36 PM
    Moderator

All replies

  • Hi,

    PUT operation means "replace" or "overwrite". You can see that in the IUpdatable/IDataServiceUpdateProvider. When PUT operation is executed the service will first call your ResetResource method and then SetValue on all the properties which were in the payload. Since the ResetResource should reset all properties to their default values, the end result is what looks like an overwrite operation.

    MERGE operation on the other hand doesn't have the "overwrite" meaning. It will behave the same as PUT except that it won't call the ResetResource. As a result only the properties which were in the payload will be applied via SetValue, nothing else will be done.

    So you want to use MERGE instead of PUT. If you're using .NET client it will use MERGE by default, you can ask for PUT through SaveChangesOptions.ReplaceOnUpdate flag.

    Thanks,


    Vitek Karas [MSFT]
    Tuesday, August 3, 2010 7:36 PM
    Moderator
  • Thanks a lot, Vitek! :-)
    Tuesday, August 3, 2010 7:59 PM