From 538d85bc9b8b7a397925096d5ccb8082d2f641fe Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 3 Jul 2024 20:05:39 -0400 Subject: [PATCH] Fix null script_name field in worker script settings --- workers_bindings.go | 25 +++++++++++++++++-------- workers_bindings_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ workers_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 8 deletions(-) diff --git a/workers_bindings.go b/workers_bindings.go index 0516c5b3f72..e1b6b107959 100644 --- a/workers_bindings.go +++ b/workers_bindings.go @@ -143,7 +143,7 @@ func (b WorkerKvNamespaceBinding) serialize(bindingName string) (workerBindingMe // https://api.cloudflare.com/#durable-objects-namespace-properties type WorkerDurableObjectBinding struct { ClassName string - ScriptName string + ScriptName *string } // Type returns the type of the binding. @@ -156,12 +156,17 @@ func (b WorkerDurableObjectBinding) serialize(bindingName string) (workerBinding return nil, nil, fmt.Errorf(`ClassName for binding "%s" cannot be empty`, bindingName) } - return workerBindingMeta{ - "name": bindingName, - "type": b.Type(), - "class_name": b.ClassName, - "script_name": b.ScriptName, - }, nil, nil + meta := workerBindingMeta{ + "name": bindingName, + "type": b.Type(), + "class_name": b.ClassName, + } + + if b.ScriptName != nil { + meta["script_name"] = *b.ScriptName + } + + return meta, nil, nil } // WorkerWebAssemblyBinding is a binding to a WebAssembly module. @@ -507,7 +512,11 @@ func (api *API) ListWorkerBindings(ctx context.Context, rc *ResourceContainer, p switch WorkerBindingType(bType) { case WorkerDurableObjectBindingType: class_name := jsonBinding["class_name"].(string) - script_name := jsonBinding["script_name"].(string) + var script_name *string + if jsonBinding["script_name"] != nil { + script_name_str := jsonBinding["script_name"].(string) + script_name = &script_name_str + } bindingListItem.Binding = WorkerDurableObjectBinding{ ClassName: class_name, ScriptName: script_name, diff --git a/workers_bindings_test.go b/workers_bindings_test.go index bb7f141fa8b..16954ddfae2 100644 --- a/workers_bindings_test.go +++ b/workers_bindings_test.go @@ -193,6 +193,46 @@ func TestListWorkerBindings_Wfp(t *testing.T) { assert.Equal(t, WorkerD1DataseBindingType, res.BindingList[8].Binding.Type()) } +func TestListWorkerBindings_NullScriptName(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/accounts/"+testAccountID+"/workers/scripts/my-script/bindings", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method) + w.Header().Set("content-type", "application/json") + fmt.Fprint(w, `{ + "result": [ + { + "name": "MY_DURABLE_OBJECT", + "type": "durable_object_namespace", + "class_name": "MyClass", + "script_name": null + } + ], + "success": true, + "errors": [], + "messages": [] + }`) + }) + + res, err := client.ListWorkerBindings(context.Background(), AccountIdentifier(testAccountID), ListWorkerBindingsParams{ + ScriptName: "my-script", + }) + assert.NoError(t, err) + + assert.Equal(t, successResponse, res.Response) + assert.Equal(t, 1, len(res.BindingList)) + + assert.Equal(t, res.BindingList[0], WorkerBindingListItem{ + Name: "MY_DURABLE_OBJECT", + Binding: WorkerDurableObjectBinding{ + ClassName: "MyClass", + ScriptName: nil, + }, + }) + assert.Equal(t, WorkerDurableObjectBindingType, res.BindingList[0].Binding.Type()) +} + func ExampleUnsafeBinding() { pretty := func(meta workerBindingMeta) string { buf := bytes.NewBufferString("") diff --git a/workers_test.go b/workers_test.go index 6175440a130..b436435fe7c 100644 --- a/workers_test.go +++ b/workers_test.go @@ -1449,3 +1449,43 @@ func TestUploadWorker_UnsafeBinding(t *testing.T) { }) assert.NoError(t, err) } + +func TestUploadWorker_WithNullScriptName(t *testing.T) { + setup() + defer teardown() + + handler := func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method) + + mpUpload, err := parseMultipartUpload(r) + assert.NoError(t, err) + + expectedBindings := map[string]workerBindingMeta{ + "b1": { + "name": "b1", + "type": "durable_object_namespace", + "class_name": "TheClass", + }, + } + assert.Equal(t, workerScript, mpUpload.Script) + assert.Equal(t, expectedBindings, mpUpload.BindingMeta) + + w.Header().Set("content-type", "application/json") + fmt.Fprint(w, workersScriptResponse(t)) + } + + mux.HandleFunc("/accounts/"+testAccountID+"/workers/scripts/bar", handler) + + _, err := client.UploadWorker(context.Background(), AccountIdentifier(testAccountID), CreateWorkerParams{ + ScriptName: "bar", + Script: workerScript, + Bindings: map[string]WorkerBinding{ + "b1": WorkerDurableObjectBinding{ + ClassName: "TheClass", + ScriptName: nil, + }, + }, + }) + + assert.NoError(t, err) +}