Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dns): keep comments intact with zero values of UpdateDNSRecordParams #1393

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/1393.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
dns: keep comments when calling UpdateDNSRecord with zero values of UpdateDNSRecordParams
```
10 changes: 5 additions & 5 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -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"`
Expand All @@ -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"`
}

Expand Down Expand Up @@ -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"`
}

Expand Down
58 changes: 57 additions & 1 deletion dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down