From c02484d380f805484cda4dab0480a2af84c1b3de Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Larsen Date: Mon, 23 Jan 2017 23:39:46 +0100 Subject: [PATCH] vendor: Update github.com/evanphx/json-patch Updates github.com/evanphx/json-patch dependency to a version that doesn't crash when handling an invalid json patch. Includes fix from https://github.com/evanphx/json-patch/pull/35 Fix #40218 --- Godeps/Godeps.json | 2 +- .../k8s.io/apimachinery/Godeps/Godeps.json | 2 +- .../src/k8s.io/apiserver/Godeps/Godeps.json | 2 +- .../k8s.io/kube-aggregator/Godeps/Godeps.json | 2 +- .../sample-apiserver/Godeps/Godeps.json | 2 +- vendor/github.com/evanphx/json-patch/merge.go | 33 +++++++++++---- vendor/github.com/evanphx/json-patch/patch.go | 42 +++++++++++-------- 7 files changed, 56 insertions(+), 29 deletions(-) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index c954f1a8e7..a3b88cd148 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -960,7 +960,7 @@ }, { "ImportPath": "github.com/evanphx/json-patch", - "Rev": "465937c80b3c07a7c7ad20cc934898646a91c1de" + "Rev": "ba18e35c5c1b36ef6334cad706eb681153d2d379" }, { "ImportPath": "github.com/exponent-io/jsonpath", diff --git a/staging/src/k8s.io/apimachinery/Godeps/Godeps.json b/staging/src/k8s.io/apimachinery/Godeps/Godeps.json index f591561b0e..37359166f0 100644 --- a/staging/src/k8s.io/apimachinery/Godeps/Godeps.json +++ b/staging/src/k8s.io/apimachinery/Godeps/Godeps.json @@ -40,7 +40,7 @@ }, { "ImportPath": "github.com/evanphx/json-patch", - "Rev": "465937c80b3c07a7c7ad20cc934898646a91c1de" + "Rev": "ba18e35c5c1b36ef6334cad706eb681153d2d379" }, { "ImportPath": "github.com/ghodss/yaml", diff --git a/staging/src/k8s.io/apiserver/Godeps/Godeps.json b/staging/src/k8s.io/apiserver/Godeps/Godeps.json index dc7e489c0e..7c03827899 100644 --- a/staging/src/k8s.io/apiserver/Godeps/Godeps.json +++ b/staging/src/k8s.io/apiserver/Godeps/Godeps.json @@ -312,7 +312,7 @@ }, { "ImportPath": "github.com/evanphx/json-patch", - "Rev": "465937c80b3c07a7c7ad20cc934898646a91c1de" + "Rev": "ba18e35c5c1b36ef6334cad706eb681153d2d379" }, { "ImportPath": "github.com/ghodss/yaml", diff --git a/staging/src/k8s.io/kube-aggregator/Godeps/Godeps.json b/staging/src/k8s.io/kube-aggregator/Godeps/Godeps.json index c8bb828e7e..fdf3339fb7 100644 --- a/staging/src/k8s.io/kube-aggregator/Godeps/Godeps.json +++ b/staging/src/k8s.io/kube-aggregator/Godeps/Godeps.json @@ -116,7 +116,7 @@ }, { "ImportPath": "github.com/evanphx/json-patch", - "Rev": "465937c80b3c07a7c7ad20cc934898646a91c1de" + "Rev": "ba18e35c5c1b36ef6334cad706eb681153d2d379" }, { "ImportPath": "github.com/ghodss/yaml", diff --git a/staging/src/k8s.io/sample-apiserver/Godeps/Godeps.json b/staging/src/k8s.io/sample-apiserver/Godeps/Godeps.json index a55dce8e08..f7a85ea4e0 100644 --- a/staging/src/k8s.io/sample-apiserver/Godeps/Godeps.json +++ b/staging/src/k8s.io/sample-apiserver/Godeps/Godeps.json @@ -108,7 +108,7 @@ }, { "ImportPath": "github.com/evanphx/json-patch", - "Rev": "465937c80b3c07a7c7ad20cc934898646a91c1de" + "Rev": "ba18e35c5c1b36ef6334cad706eb681153d2d379" }, { "ImportPath": "github.com/ghodss/yaml", diff --git a/vendor/github.com/evanphx/json-patch/merge.go b/vendor/github.com/evanphx/json-patch/merge.go index 330b9b528e..84e00b1489 100644 --- a/vendor/github.com/evanphx/json-patch/merge.go +++ b/vendor/github.com/evanphx/json-patch/merge.go @@ -7,7 +7,7 @@ import ( "strings" ) -func merge(cur, patch *lazyNode) *lazyNode { +func merge(cur, patch *lazyNode, mergeMerge bool) *lazyNode { curDoc, err := cur.intoDoc() if err != nil { @@ -21,16 +21,20 @@ func merge(cur, patch *lazyNode) *lazyNode { return patch } - mergeDocs(curDoc, patchDoc) + mergeDocs(curDoc, patchDoc, mergeMerge) return cur } -func mergeDocs(doc, patch *partialDoc) { +func mergeDocs(doc, patch *partialDoc, mergeMerge bool) { for k, v := range *patch { k := decodePatchKey(k) if v == nil { - delete(*doc, k) + if mergeMerge { + (*doc)[k] = nil + } else { + delete(*doc, k) + } } else { cur, ok := (*doc)[k] @@ -38,7 +42,7 @@ func mergeDocs(doc, patch *partialDoc) { pruneNulls(v) (*doc)[k] = v } else { - (*doc)[k] = merge(cur, v) + (*doc)[k] = merge(cur, v, mergeMerge) } } } @@ -88,8 +92,19 @@ func pruneAryNulls(ary *partialArray) *partialArray { var errBadJSONDoc = fmt.Errorf("Invalid JSON Document") var errBadJSONPatch = fmt.Errorf("Invalid JSON Patch") +// MergeMergePatches merges two merge patches together, such that +// applying this resulting merged merge patch to a document yields the same +// as merging each merge patch to the document in succession. +func MergeMergePatches(patch1Data, patch2Data []byte) ([]byte, error) { + return doMergePatch(patch1Data, patch2Data, true) +} + // MergePatch merges the patchData into the docData. func MergePatch(docData, patchData []byte) ([]byte, error) { + return doMergePatch(docData, patchData, false) +} + +func doMergePatch(docData, patchData []byte, mergeMerge bool) ([]byte, error) { doc := &partialDoc{} docErr := json.Unmarshal(docData, doc) @@ -117,7 +132,11 @@ func MergePatch(docData, patchData []byte) ([]byte, error) { if docErr != nil || patchErr != nil { // Not an error, just not a doc, so we turn straight into the patch if patchErr == nil { - doc = pruneDocNulls(patch) + if mergeMerge { + doc = patch + } else { + doc = pruneDocNulls(patch) + } } else { patchAry := &partialArray{} patchErr = json.Unmarshal(patchData, patchAry) @@ -137,7 +156,7 @@ func MergePatch(docData, patchData []byte) ([]byte, error) { return out, nil } } else { - mergeDocs(doc, patch) + mergeDocs(doc, patch, mergeMerge) } return json.Marshal(doc) diff --git a/vendor/github.com/evanphx/json-patch/patch.go b/vendor/github.com/evanphx/json-patch/patch.go index 8988da54bb..3b33010538 100644 --- a/vendor/github.com/evanphx/json-patch/patch.go +++ b/vendor/github.com/evanphx/json-patch/patch.go @@ -257,11 +257,15 @@ Loop: return false } -func findObject(pd *partialDoc, path string) (container, string) { - doc := container(pd) +func findObject(pd *container, path string) (container, string) { + doc := *pd split := strings.Split(path, "/") + if len(split) < 2 { + return nil, "" + } + parts := split[1 : len(split)-1] key := split[len(split)-1] @@ -311,7 +315,7 @@ func (d *partialDoc) get(key string) (*lazyNode, error) { func (d *partialDoc) remove(key string) error { _, ok := (*d)[key] if !ok { - return fmt.Errorf("Unable to remove nonexistant key: %s", key) + return fmt.Errorf("Unable to remove nonexistent key: %s", key) } delete(*d, key) @@ -341,7 +345,7 @@ func (d *partialArray) set(key string, val *lazyNode) error { copy(ary, cur) if idx >= len(ary) { - fmt.Printf("huh?: %#v[%d] %s, %s\n", ary, idx) + return fmt.Errorf("Unable to access invalid index: %d", idx) } ary[idx] = val @@ -409,7 +413,7 @@ func (d *partialArray) remove(key string) error { } -func (p Patch) add(doc *partialDoc, op operation) error { +func (p Patch) add(doc *container, op operation) error { path := op.path() con, key := findObject(doc, path) @@ -421,7 +425,7 @@ func (p Patch) add(doc *partialDoc, op operation) error { return con.add(key, op.value()) } -func (p Patch) remove(doc *partialDoc, op operation) error { +func (p Patch) remove(doc *container, op operation) error { path := op.path() con, key := findObject(doc, path) @@ -433,7 +437,7 @@ func (p Patch) remove(doc *partialDoc, op operation) error { return con.remove(key) } -func (p Patch) replace(doc *partialDoc, op operation) error { +func (p Patch) replace(doc *container, op operation) error { path := op.path() con, key := findObject(doc, path) @@ -445,7 +449,7 @@ func (p Patch) replace(doc *partialDoc, op operation) error { return con.set(key, op.value()) } -func (p Patch) move(doc *partialDoc, op operation) error { +func (p Patch) move(doc *container, op operation) error { from := op.from() con, key := findObject(doc, from) @@ -475,7 +479,7 @@ func (p Patch) move(doc *partialDoc, op operation) error { return con.set(key, val) } -func (p Patch) test(doc *partialDoc, op operation) error { +func (p Patch) test(doc *container, op operation) error { path := op.path() con, key := findObject(doc, path) @@ -493,9 +497,8 @@ func (p Patch) test(doc *partialDoc, op operation) error { if val == nil { if op.value().raw == nil { return nil - } else { - return fmt.Errorf("Testing value %s failed", path) } + return fmt.Errorf("Testing value %s failed", path) } if val.equal(op.value()) { @@ -540,7 +543,12 @@ func (p Patch) Apply(doc []byte) ([]byte, error) { // ApplyIndent mutates a JSON document according to the patch, and returns the new // document indented. func (p Patch) ApplyIndent(doc []byte, indent string) ([]byte, error) { - pd := &partialDoc{} + var pd container + if doc[0] == '[' { + pd = &partialArray{} + } else { + pd = &partialDoc{} + } err := json.Unmarshal(doc, pd) @@ -553,15 +561,15 @@ func (p Patch) ApplyIndent(doc []byte, indent string) ([]byte, error) { for _, op := range p { switch op.kind() { case "add": - err = p.add(pd, op) + err = p.add(&pd, op) case "remove": - err = p.remove(pd, op) + err = p.remove(&pd, op) case "replace": - err = p.replace(pd, op) + err = p.replace(&pd, op) case "move": - err = p.move(pd, op) + err = p.move(&pd, op) case "test": - err = p.test(pd, op) + err = p.test(&pd, op) default: err = fmt.Errorf("Unexpected kind: %s", op.kind()) }