sealcode / sealious

An extensible, declarative node framework
25 stars 2 forks source link

Method PUT creates empty objects from fields which didn't exist #237

Closed arkadiusz-wieczorek closed 6 years ago

arkadiusz-wieczorek commented 9 years ago

I've sent POST method with minimal body for create new resource (latitude=2 & longitude=3)

{
    "id": "18es9cudfb",
    "type": "places",
    "body": {
        "longitude": 3,
        "latitude": 2,
        "image": "/img/places/default.jpg"
    },
    "created_context": {
        "timestamp": 1445621649845,
        "ip": "127.0.0.1",
        "user_id": "64ym4tumu9"
    }
}

After creating new resource I've sent PUT method also with minimal body (latitude=2 & longitude=4) to the above resource. As you can see this resource have unnecessary fields which are empty objects.

{
    "id": "18es9cudfb",
    "type": "places",
    "body": {
        "name": {},
        "address": {},
        "city": {},
        "country": {},
        "longitude": 4,
        "latitude": 2,
        "image": "/img/places/default.jpg",
        "image_name": {},
        "access": {}
    },
    "created_context": {
        "timestamp": 1445621649845,
        "ip": "127.0.0.1",
        "user_id": "64ym4tumu9"
    },
    "last_modified_context": {
        "timestamp": 1445621741729,
        "ip": "127.0.0.1",
        "user_id": "64ym4tumu9"
    }
}

Structure resource

new Sealious.ChipTypes.ResourceType({
    name: "places",
    fields: [
        {name: "name", type: "text"},
        {name: "address", type: "text"},
        {name: "city", type: "text"},
        {name: "country", type: "text"},
        {name: "category", type: "category"},
        {name: "longitude", type: "float", required: true}, //52
        {name: "latitude", type: "float", required: true},  //16
        {name: "image", type: "file", params: {no_file_value: "/img/places/default.jpg"}},
        {name: "image_name", type: "text"},
        {name: "access", type: "access"}
    ],
    access_strategy: {
        create : "create_only_logged",
        retrieve : "retrieve_with_access",
        delete : "create_only_logged",
        update : "create_only_logged"
    }
});
arkadiusz-wieczorek commented 9 years ago

It would be working otherwise after update https://github.com/Sealious/sealious/commit/bb165d234f8052238f76644ae0ce2e5a965f07b2

arkadiusz-wieczorek commented 9 years ago

This bug still existing. I've noticed that after operation field which is type int - field has null value.

From other resource type

{
    "id": "4wb6lxbspm",
    "type": "person",
    "body": {
        "name": {},
        "age": null,
        "lat": 98765432
    },
    "created_context": {
        "timestamp": 1447164292844,
        "ip": "127.0.0.1",
        "user_id": false
    },
    "last_modified_context": {
        "timestamp": 1447164329278,
        "ip": "127.0.0.1",
        "user_id": false
    }
}
arkadiusz-wieczorek commented 9 years ago

I've discovered quick fix on this bug, but it behaves as PATCH method because we are using patch_resource function in update_resource. We should rewrite Sealious.Dispatcher.datastore.update function to compatibility with any datastore driver. This function should have two cases supported by some parameter:

am I right?

this.update_resource = function(context, type_name, resource_id, body){
    return Promise.try(function(){
        var type = Sealious.ChipManager.get_chip("resource_type", type_name);
        for (var i in type.fields) {
            var field = type.fields[i];
            if (!body.hasOwnProperty(field.name)) {
                // body[field.name] = undefined;
                delete body[field.name];
            }
        }
        return self.patch_resource(context, type_name, resource_id, body, true);            
    })
}
kuba-orlik commented 8 years ago

It turned out it was an issue with www-server's way of preprocessing output. I've fixed that. You should ensure that your application uses the latest stable branch of www-server (v0.6.5). Changes will take full effect once https://github.com/Sealious/sealious/pull/262 is merged and a new patch version of Sealious' stable branch is released.

adwydman commented 8 years ago

Is this issue solved?

arkadiusz-wieczorek commented 8 years ago

From what I know it still generates this issue.

kuba-orlik commented 6 years ago

Issues moved do Sealcode's phabricator: https://hub.sealcode.org/project/view/11/