Skip to content

Commit

Permalink
Merge pull request #1906 from p0lyn0mial/jsonpatch-formatting
Browse files Browse the repository at this point in the history
API-1835: manifestclient: format jsonpatch payloads
  • Loading branch information
openshift-merge-bot[bot] authored Dec 11, 2024
2 parents 85e36bc + 29d1794 commit b9df8bb
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 5 deletions.
12 changes: 12 additions & 0 deletions pkg/manifestclient/write_roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ func (mrt *writeTrackingRoundTripper) roundTrip(req *http.Request) ([]byte, erro
}
generatedName = bodyObj.(*unstructured.Unstructured).GetGenerateName()
kindType = bodyObj.GetObjectKind().GroupVersionKind()
} else if (action == ActionPatch || action == ActionPatchStatus) && patchType == string(types.JSONPatchType) {
// the following code gives nice formatting for
// JSON patches that will be stored in files.
var jsonPatchOperations []map[string]interface{}
err = json.Unmarshal(bodyOutputBytes, &jsonPatchOperations)
if err != nil {
return nil, fmt.Errorf("unable to decode JSONPatch body: %w", err)
}
bodyOutputBytes, err = yaml.Marshal(jsonPatchOperations)
if err != nil {
return nil, fmt.Errorf("unable to encode JSONPatch body: %w", err)
}
}

fieldManagerName := ""
Expand Down
7 changes: 4 additions & 3 deletions pkg/manifestclienttest/client_write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,13 @@ func TestSimpleWritesChecks(t *testing.T) {
}

ctx := manifestclient.WithControllerInstanceNameFromContext(context.TODO(), "fooController")
jsonPatchPayload := []byte(`[{"op":"test","path":"/foo/path","value":"bar"},{"op":"remove","path":"/foo/path/2"}]`)

resultingObj, err := configClient.ConfigV1().FeatureGates().Patch(
ctx,
"instance-name",
types.JSONPatchType,
[]byte("json-patch"),
jsonPatchPayload,
metav1.PatchOptions{})
if err != nil {
t.Fatal(err)
Expand All @@ -189,7 +190,7 @@ func TestSimpleWritesChecks(t *testing.T) {
ctx,
"instance-name",
types.JSONPatchType,
[]byte("json-patch"),
jsonPatchPayload,
metav1.PatchOptions{},
"status")
if err != nil {
Expand All @@ -210,7 +211,7 @@ func TestSimpleWritesChecks(t *testing.T) {
ctx,
"instance-name",
types.JSONPatchType,
[]byte("json-patch"),
jsonPatchPayload,
metav1.PatchOptions{},
)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- op: test
path: /foo/path
value: bar
- op: remove
path: /foo/path/2

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- op: test
path: /foo/path
value: bar
- op: remove
path: /foo/path/2

This file was deleted.

0 comments on commit b9df8bb

Please sign in to comment.