apuigsech / rest-layer-sql

Provide SQL Database using database/sql storage backend for rest-layer.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PUT behavior is not correct

apuigsech opened this issue · comments

According the RFC2616 (section 9.6):

"The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server."

How I understand it, is that there are two types of actions you can do with PUT; create a new object or replace an object with a newer version. I want to highlight I am using replace instead of update because I think it's important. How I think the replacement should work is that the data in the previous version is completely dismissed and replaced by the new one and this is not how PUT is behaving on REST layer with this Storage Backend. It is updating the provided fields instead of getting the full object as a new object.

I show one example of the actual behavior and I will explain the expected one.

I have a very simple objet type with two fields:

var (
	unit = schema.Schema{
		Fields: schema.Fields{
			"id": schema.IDField,
			"created": schema.CreatedField,
			"updated": schema.UpdatedField,
			"str": {
				Sortable: true,
				Filterable: true,
				Default: "null"
				Validator: &schema.String{
					MaxLen: 150,
				},
			},
			"int": {
				Sortable: true,
				Filterable: true,
				Default: 0
				Validator: &schema.Integer{},
			},
		},
	}
)

I am going to create an entry and then replace it by a new one:

$ http -b POST :8080/units str="foo" int:=1
{
    "created": "2019-01-08T20:44:51.71307+01:00",
    "id": "bgqfrcrmvbapjkskql4g",
    "int": 1,
    "str": "foo",
    "updated": "2019-01-08T20:44:51.71307+01:00"
}

$ http -b PUT :8080/units/bgqfrcrmvbapjkskql4g str="bar"
{
    "created": "2019-01-08T20:45:06.834083+01:00",
    "id": "bgqfrcrmvbapjkskql4g",
    "str": "bar",
    "updated": "2019-01-08T20:45:06.834086+01:00"
}

$ http -b GET :8080/units/bgqfrcrmvbapjkskql4g
{
    "created": "2019-01-08T20:45:06.834083+01:00",
    "id": "bgqfrcrmvbapjkskql4g",
    "int": 1,
    "str": "bar",
    "updated": "2019-01-08T20:45:06.834086+01:00"
}

Because in the PUT request the 'int' field has not been specified, and assuming that PUT must specify the entire entity, 'int' must adopt the default value which is 0 instead of retaining the one in the old version.