Skip to content

Commit 74de312

Browse files
Dragomir-Ivanovsmyrman
authored andcommitted
Fix update field with Default value set.
Deleting a field with `Default` value set, will always be reset to its default value.
1 parent 7ae9e70 commit 74de312

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Below is an overview over recent breaking changes, starting from an arbitrary po
8989
- `filter` parameters will be validated for type match only, instead of type & constrains.
9090
- PR #228: `Reference` projection fields will be validated against referenced resource schema.
9191
- PR #230: `Connection` projection fields will be validated against connected resource schema.
92-
- PR #241: Always call `OnUpdate` field hook on HTTP PUT for existing documents.
92+
- PR #241: Always call `OnUpdate` field hook on HTTP PUT for existing documents. Deleting a field with `Default` value set, will always be reset to its default value.
9393

9494
From the next release and onwards (0.2), this list will summarize breaking changes done to master since the last release.
9595

@@ -1252,7 +1252,9 @@ Used to query a resource with its sub/embedded resources.
12521252
Used to create new resource document, where new `ID` is generated from the server.
12531253
12541254
### PUT
1255-
Used to create/update single resource document given its `ID`. Be aware when dealing with resource fields with `Default` set. Initial creation for such resources will set particular field to its default value if omitted, however on subsequent `PUT` calls this field will be deleted if omitted. If persistent `Default` field is needed use `{Required: true}` with it.
1255+
Used to create/update single resource document given its `ID`. On initial request, `onInsert` resource hook will be called. All subsequent `PUT` requests will call `onUpdate` resource hook.
1256+
1257+
Be aware when dealing with resource fields with `Default` set. If missing from payload or deleted, such fields will be reset to their `default value`.
12561258
12571259
### PATCH
12581260
Used to update/patch single resource document given its `ID`. REST Layer supports following update protocols:

rest/method_item_put_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,8 @@ func TestPutItemDefault(t *testing.T) {
388388
return http.NewRequest("PUT", "/foo/2", body)
389389
},
390390
ResponseCode: http.StatusOK,
391-
ResponseBody: `{"id": "2", "foo": "baz"}`,
392-
ExtraTest: checkPayload("foo", "2", map[string]interface{}{"id": "2", "foo": "baz"}),
391+
ResponseBody: `{"id": "2", "foo": "baz", "bar": "default"}`,
392+
ExtraTest: checkPayload("foo", "2", map[string]interface{}{"id": "2", "foo": "baz", "bar": "default"}),
393393
},
394394
}
395395

schema/schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (s Schema) Prepare(ctx context.Context, payload map[string]interface{}, ori
142142
// previous value as the client would have no way to resubmit the stored value.
143143
if def.Hidden && !def.ReadOnly {
144144
changes[field] = oValue
145-
} else if def.Required && def.Default != nil {
145+
} else if def.Default != nil {
146146
changes[field] = def.Default
147147
} else {
148148
changes[field] = Tombstone

0 commit comments

Comments
 (0)