From 54589bd2256117a991bc06cff0f95130b6baf431 Mon Sep 17 00:00:00 2001 From: favonia Date: Tue, 22 Aug 2023 18:21:22 -0500 Subject: [PATCH 1/2] fix(dns): use pointers for the Comment field --- dns.go | 10 ++++----- dns_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/dns.go b/dns.go index 842fa2f2fe2..d69e2cb2cd9 100644 --- a/dns.go +++ b/dns.go @@ -34,7 +34,7 @@ type DNSRecord struct { Proxied *bool `json:"proxied,omitempty"` Proxiable bool `json:"proxiable,omitempty"` Locked bool `json:"locked,omitempty"` - Comment string `json:"comment,omitempty"` + Comment string `json:"comment,omitempty"` // the server will omit the comment field when the comment is empty Tags []string `json:"tags,omitempty"` } @@ -57,8 +57,8 @@ type ListDNSRecordsParams struct { Name string `url:"name,omitempty"` Content string `url:"content,omitempty"` Proxied *bool `url:"proxied,omitempty"` - Comment string `url:"comment,omitempty"` - Tags []string `url:"tag,omitempty"` // potentially multiple `tag=` + Comment string `url:"comment,omitempty"` // currently, the server does not support searching for records with an empty comment + Tags []string `url:"tag,omitempty"` // potentially multiple `tag=` TagMatch string `url:"tag-match,omitempty"` Order string `url:"order,omitempty"` Direction ListDirection `url:"direction,omitempty"` @@ -77,7 +77,7 @@ type UpdateDNSRecordParams struct { Priority *uint16 `json:"priority,omitempty"` TTL int `json:"ttl,omitempty"` Proxied *bool `json:"proxied,omitempty"` - Comment string `json:"comment"` + Comment *string `json:"comment,omitempty"` // nil will keep the current comment, while StringPtr("") will empty it Tags []string `json:"tags"` } @@ -193,7 +193,7 @@ type CreateDNSRecordParams struct { Proxied *bool `json:"proxied,omitempty" url:"proxied,omitempty"` Proxiable bool `json:"proxiable,omitempty"` Locked bool `json:"locked,omitempty"` - Comment string `json:"comment,omitempty" url:"comment,omitempty"` + Comment string `json:"comment,omitempty" url:"comment,omitempty"` // to the server, there's no difference between "no comment" and "empty comment" Tags []string `json:"tags,omitempty"` } diff --git a/dns_test.go b/dns_test.go index 1c575288da2..97e034a57bf 100644 --- a/dns_test.go +++ b/dns_test.go @@ -616,7 +616,63 @@ func TestUpdateDNSRecord_ClearComment(t *testing.T) { _, err := client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), UpdateDNSRecordParams{ ID: dnsRecordID, - Comment: "", + Comment: StringPtr(""), + }) + require.NoError(t, err) +} + +func TestUpdateDNSRecord_KeepComment(t *testing.T) { + setup() + defer teardown() + + input := DNSRecord{ + ID: "372e67954025e0ba6aaa6d586b9e0b59", + } + + handler := func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPatch, r.Method, "Expected method 'PATCH', got %s", r.Method) + + var v DNSRecord + err := json.NewDecoder(r.Body).Decode(&v) + require.NoError(t, err) + v.ID = "372e67954025e0ba6aaa6d586b9e0b59" + assert.Equal(t, input, v) + + w.Header().Set("content-type", "application/json") + fmt.Fprint(w, `{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "372e67954025e0ba6aaa6d586b9e0b59", + "type": "A", + "name": "example.com", + "content": "198.51.100.4", + "proxiable": true, + "proxied": false, + "ttl": 120, + "locked": false, + "zone_id": "d56084adb405e0b7e32c52321bf07be6", + "zone_name": "example.com", + "created_on": "2014-01-01T05:20:00Z", + "modified_on": "2014-01-01T05:20:00Z", + "comment":null, + "tags":[], + "data": {}, + "meta": { + "auto_added": true, + "source": "primary" + } + } + }`) + } + + dnsRecordID := "372e67954025e0ba6aaa6d586b9e0b59" + + mux.HandleFunc("/zones/"+testZoneID+"/dns_records/"+dnsRecordID, handler) + + _, err := client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), UpdateDNSRecordParams{ + ID: dnsRecordID, }) require.NoError(t, err) } From 2ce602304c7e4603c175adbc93ec65dfb337760b Mon Sep 17 00:00:00 2001 From: favonia Date: Sun, 10 Sep 2023 08:32:28 -0500 Subject: [PATCH 2/2] docs: add the changelog entry --- .changelog/1393.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/1393.txt diff --git a/.changelog/1393.txt b/.changelog/1393.txt new file mode 100644 index 00000000000..a3ef6c8ae61 --- /dev/null +++ b/.changelog/1393.txt @@ -0,0 +1,3 @@ +```release-note:bug +dns: keep comments when calling UpdateDNSRecord with zero values of UpdateDNSRecordParams +```