From 74c4735b6643d3df1f85c8c7468bbb3f0b0001bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 14 May 2019 18:26:47 +0200 Subject: [PATCH 1/3] object put: allow empty objects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- core/coreapi/object.go | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/core/coreapi/object.go b/core/coreapi/object.go index 7cbd480c5c9..d10e1352262 100644 --- a/core/coreapi/object.go +++ b/core/coreapi/object.go @@ -78,12 +78,6 @@ func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Obj return nil, err } - // check that we have data in the Node to add - // otherwise we will add the empty object without raising an error - if nodeEmpty(node) { - return nil, errors.New("no data or links in this node") - } - dagnode, err = deserializeNode(node, options.DataType) if err != nil { return nil, err @@ -99,12 +93,6 @@ func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Obj return nil, err } - // check that we have data in the Node to add - // otherwise we will add the empty object without raising an error - if nodeEmpty(node) { - return nil, errors.New("no data or links in this node") - } - dagnode, err = deserializeNode(node, options.DataType) if err != nil { return nil, err @@ -368,7 +356,3 @@ func deserializeNode(nd *Node, dataFieldEncoding string) (*dag.ProtoNode, error) return dagnode, nil } - -func nodeEmpty(node *Node) bool { - return node.Data == "" && len(node.Links) == 0 -} From d005ec42b157f2e1899d425abd9b4734e7e86f01 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 14 May 2019 23:37:52 +0200 Subject: [PATCH 2/3] object put: detect unexpected fields MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- core/coreapi/object.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/coreapi/object.go b/core/coreapi/object.go index d10e1352262..2c1678d9d91 100644 --- a/core/coreapi/object.go +++ b/core/coreapi/object.go @@ -73,7 +73,9 @@ func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Obj switch options.InputEnc { case "json": node := new(Node) - err = json.Unmarshal(data, node) + decoder := json.NewDecoder(bytes.NewReader(data)) + decoder.DisallowUnknownFields() + err = decoder.Decode(node) if err != nil { return nil, err } From f18289531da2d7766c3fa4cc52ec090d3b44d12b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 14 May 2019 23:42:58 +0200 Subject: [PATCH 3/3] object: update sharness tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- test/sharness/t0051-object.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/sharness/t0051-object.sh b/test/sharness/t0051-object.sh index 53955253a30..4332b42c722 100755 --- a/test/sharness/t0051-object.sh +++ b/test/sharness/t0051-object.sh @@ -111,11 +111,11 @@ test_object_cmd() { cat ../t0051-object-data/testPut.xml | ipfs object put --inputenc=xml > actual_putStdinOut ' - test_expect_success "'ipfs object put broken.xml' should fail" ' + test_expect_failure "'ipfs object put broken.xml' should fail" ' test_expect_code 1 ipfs object put ../t0051-object-data/brokenPut.xml --inputenc=xml 2>actual_putBrokenErr >actual_putBroken ' - test_expect_success "'ipfs object put broken.hxml' output looks good" ' + test_expect_failure "'ipfs object put broken.hxml' output looks good" ' touch expected_putBroken && printf "Error: no data or links in this node\n" > expected_putBrokenErr && test_cmp expected_putBroken actual_putBroken && @@ -170,7 +170,7 @@ test_object_cmd() { test_expect_success "'ipfs object put broken.hjson' output looks good" ' touch expected_putBroken && - printf "Error: no data or links in this node\n" > expected_putBrokenErr && + printf "Error: json: unknown field \"this\"\n" > expected_putBrokenErr && test_cmp expected_putBroken actual_putBroken && test_cmp expected_putBrokenErr actual_putBrokenErr '