forked from apache/apisix-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: PATCH method bug * test: use sub path patch in e2e test * fix: lint * fix: naming stype * fix: according to review * fix: style
- Loading branch information
Showing
6 changed files
with
358 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
jsonpatch "github.com/evanphx/json-patch/v5" | ||
) | ||
|
||
func MergeJson(doc, patch []byte) ([]byte, error) { | ||
out, err := jsonpatch.MergePatch(doc, patch) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
return out, nil | ||
} | ||
|
||
func PatchJson(doc []byte, path, val string) ([]byte, error) { | ||
patch := []byte(`[ { "op": "replace", "path": "` + path + `", "value": ` + val + `}]`) | ||
obj, err := jsonpatch.DecodePatch(patch) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
out, err := obj.Apply(doc) | ||
|
||
if err != nil { | ||
// try to add if field not exist | ||
patch = []byte(`[ { "op": "add", "path": "` + path + `", "value": ` + val + `}]`) | ||
obj, err = jsonpatch.DecodePatch(patch) | ||
if err != nil { | ||
return nil, err | ||
} | ||
out, err = obj.Apply(doc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
func MergePatch(obj interface{}, subPath string, reqBody []byte) ([]byte, error) { | ||
var res []byte | ||
jsonBytes, err := json.Marshal(obj) | ||
if err != nil { | ||
return res, err | ||
} | ||
|
||
if subPath != "" { | ||
res, err = PatchJson(jsonBytes, subPath, string(reqBody)) | ||
} else { | ||
res, err = MergeJson(jsonBytes, reqBody) | ||
} | ||
|
||
if err != nil { | ||
return res, err | ||
} | ||
return res, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func compareJSON(a, b string) bool { | ||
var objA, objB interface{} | ||
json.Unmarshal([]byte(a), &objA) | ||
json.Unmarshal([]byte(b), &objB) | ||
|
||
return reflect.DeepEqual(objA, objB) | ||
} | ||
|
||
func formatJSON(j string) string { | ||
buf := new(bytes.Buffer) | ||
|
||
json.Indent(buf, []byte(j), "", " ") | ||
|
||
return buf.String() | ||
} | ||
|
||
func TestMergeJson(t *testing.T) { | ||
cases := []struct { | ||
doc, patch, result, desc string | ||
}{ | ||
{ | ||
desc: "simple merge", | ||
doc: `{ | ||
"id": "1", | ||
"status": 1, | ||
"key": "fake key", | ||
"cert": "fake cert", | ||
"create_time": 1, | ||
"update_time": 2 | ||
}`, | ||
patch: `{ | ||
"id": "1", | ||
"status": 0, | ||
"key": "fake key1", | ||
"cert": "fake cert1" | ||
}`, | ||
result: `{ | ||
"id": "1", | ||
"status": 0, | ||
"key": "fake key1", | ||
"cert": "fake cert1", | ||
"create_time": 1, | ||
"update_time": 2 | ||
}`, | ||
}, | ||
{ | ||
desc: `array merge`, | ||
doc: `{ | ||
"uri": "/index.html", | ||
"upstream": { | ||
"type": "roundrobin", | ||
"nodes": [{ | ||
"host": "39.97.63.215", | ||
"port": 80, | ||
"weight" : 1 | ||
}] | ||
} | ||
}`, | ||
patch: `{ | ||
"upstream": { | ||
"nodes": [{ | ||
"host": "39.97.63.216", | ||
"port": 80, | ||
"weight" : 1 | ||
},{ | ||
"host": "39.97.63.217", | ||
"port": 80, | ||
"weight" : 1 | ||
}] | ||
} | ||
}`, | ||
result: `{ | ||
"uri": "/index.html", | ||
"upstream": { | ||
"type": "roundrobin", | ||
"nodes": [{ | ||
"host": "39.97.63.216", | ||
"port": 80, | ||
"weight" : 1 | ||
},{ | ||
"host": "39.97.63.217", | ||
"port": 80, | ||
"weight" : 1 | ||
}] | ||
} | ||
}`, | ||
}, | ||
} | ||
for _, c := range cases { | ||
out, err := MergeJson([]byte(c.doc), []byte(c.patch)) | ||
|
||
if err != nil { | ||
t.Errorf("Unable to merge patch: %s", err) | ||
} | ||
|
||
if !compareJSON(string(out), c.result) { | ||
t.Errorf("Merge failed. Expected:\n%s\n\nActual:\n%s", | ||
formatJSON(c.result), formatJSON(string(out))) | ||
} | ||
} | ||
} | ||
|
||
func TestPatchJson(t *testing.T) { | ||
cases := []struct { | ||
doc, path, value, result, desc string | ||
}{ | ||
{ | ||
desc: "patch array", | ||
doc: `{ | ||
"uri": "/index.html", | ||
"upstream": { | ||
"type": "roundrobin", | ||
"nodes": [{ | ||
"host": "39.97.63.215", | ||
"port": 80, | ||
"weight" : 1 | ||
}] | ||
} | ||
}`, | ||
path: `/upstream/nodes`, | ||
value: `[{ | ||
"host": "39.97.63.216", | ||
"port": 80, | ||
"weight" : 1 | ||
},{ | ||
"host": "39.97.63.217", | ||
"port": 80, | ||
"weight" : 1 | ||
}]`, | ||
result: `{ | ||
"uri": "/index.html", | ||
"upstream": { | ||
"type": "roundrobin", | ||
"nodes": [{ | ||
"host": "39.97.63.216", | ||
"port": 80, | ||
"weight" : 1 | ||
},{ | ||
"host": "39.97.63.217", | ||
"port": 80, | ||
"weight" : 1 | ||
}] | ||
} | ||
}`, | ||
}, | ||
{ | ||
desc: "patch field that non existent", | ||
doc: `{ | ||
"uri": "/index.html", | ||
"upstream": { | ||
"type": "roundrobin", | ||
"nodes": [{ | ||
"host": "39.97.63.215", | ||
"port": 80, | ||
"weight" : 1 | ||
}] | ||
} | ||
}`, | ||
path: `/upstream/labels`, | ||
value: `{"app": "test"}`, | ||
result: `{ | ||
"uri": "/index.html", | ||
"upstream": { | ||
"type": "roundrobin", | ||
"nodes": [{ | ||
"host": "39.97.63.215", | ||
"port": 80, | ||
"weight" : 1 | ||
}], | ||
"labels": {"app": "test"} | ||
} | ||
}`, | ||
}, | ||
} | ||
for _, c := range cases { | ||
out, err := PatchJson([]byte(c.doc), c.path, c.value) | ||
if err != nil { | ||
t.Errorf("Unable to patch: %s", err) | ||
} | ||
|
||
if !compareJSON(string(out), c.result) { | ||
t.Errorf("Patch failed. Expected:\n%s\n\nActual:\n%s", | ||
formatJSON(c.result), formatJSON(string(out))) | ||
} | ||
} | ||
} |
Oops, something went wrong.