Repository: cloudflare/cloudflare-go (go)

Issue:
Email Routing Catch All does not match the API docs
### Confirmation

- [X] My issue isn't already found on the issue tracker.
- [X] I have replicated my issue using the latest version of the library and it is still present.

### cloudflare-go version

latest

### Go environment

N/A

### Expected output

Expect an API response of
```json
{
  "tag": "a7e6fb77503c41d8a7f3113c6918f10c",
  "name": "Send to user@example.net rule.",
  "enabled": true,
  "matchers": [
    {
      "type": "all"
    }
  ],
  "actions": [
    {
      "type": "forward",
      "value": [
        "destinationaddress@example.net"
      ]
    }
  ]
}
```

### Actual output

Seeing a response of
```json
{
  "result": {
    "tag": "dd4b8763079149b582cbe76bf86e8910",
    "name": "Send to user@example.net rule.",
    "matchers": [
      {
        "type": "all"
      }
    ],
    "actions": [
      {
        "type": "forward",
        "value": [
          "destinationaddress@example.net"
        ]
      }
    ],
    "enabled": true,
    "priority": 2147483647
  },
  "success": true,
  "errors": [],
  "messages": []
}
```

This causes the return values from both Get and Update to always return an empty `EmailRoutingCatchAllRule`.

### Code demonstrating the issue

```go
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/cloudflare/cloudflare-go"
)

func main() {
	// Construct a new API object using a global API key
	// alternatively, you can use a scoped API token
	api, err := cloudflare.NewWithAPIToken("<API TOKEN>", cloudflare.Debug(true))
	if err != nil {
		log.Fatal(err)
	}

	// Most API calls require a Context
	ctx := context.Background()

	res, err := api.GetEmailRoutingCatchAllRule(ctx, cloudflare.ZoneIdentifier("<ZONE ID>"))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%#v", res)
```
```bash
cloudflare-go [DEBUG] REQUEST Method:GET URI:https://api.cloudflare.com/client/v4/zones/<zone id>/email/routing/rules/catch_all Headers:http.Header(nil) Body:<nil>
cloudflare-go [DEBUG] RESPONSE StatusCode:200 RayID:74296582289d3010-BOS ContentType:application/json; charset=UTF-8 Body:"{\"result\":{\"tag\":\"dd4b8763079149b582cbe76bf86e8910\",\"name\":\"\",\"matchers\":[{\"type\":\"all\"}],\"actions\":[{\"type\":\"forward\",\"value\":[\"destinationaddress@example.net\"]}],\"enabled\":true,\"priority\":2147483647},\"success\":true,\"errors\":[],\"messages\":[]}"

{"result":{"tag":"dd4b8763079149b582cbe76bf86e8910","name":"","matchers":[{"type":"all"}],"actions":[{"type":"forward","value":["destinationaddress@example.net"]}],"enabled":true,"priority":2147483647},"success":true,"errors":[],"messages":[]}

cloudflare.EmailRoutingCatchAllRule{Tag:"", Name:"", Enabled:(*bool)(nil), Matchers:[]cloudflare.EmailRoutingRuleMatcher(nil), Actions:[]cloudflare.EmailRoutingRuleAction(nil)}
```

### Steps to reproduce

Run the code given

### References

API Docs Links
https://api.cloudflare.com/#email-routing-routing-rules-get-catch-all-rule
https://api.cloudflare.com/#email-routing-routing-rules-update-catch-all-rule


Pull request:
Fix responses for email routing catch all rules
## Description
Fixes the response for email routing catch all rules.

Fixes #1069 
## Has your change been tested?

Unit tests updated and pass.

Ran example in #1069 with branch and it works

## Types of changes

What sort of change does your code introduce/modify?

- [X] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Checklist:

- [ ] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [X] I have added tests to cover my changes.
- [X] All new and existing tests passed.
- [ ] This change is using publicly documented (api.cloudflare.com or developers.cloudflare.com) and stable APIs.


Patch:
diff --git a/.changelog/1070.txt b/.changelog/1070.txt
new file mode 100644
index 000000000..d3586c87e
--- /dev/null
+++ b/.changelog/1070.txt
@@ -0,0 +1,3 @@
+```release-note:bug
+email_routing_rules: Fix response for email routing catch all rule.
+```
\ No newline at end of file
diff --git a/email_routing_rules.go b/email_routing_rules.go
index aa3a694d1..22101b624 100644
--- a/email_routing_rules.go
+++ b/email_routing_rules.go
@@ -76,6 +76,11 @@ type EmailRoutingCatchAllRule struct {
 	Actions  []EmailRoutingRuleAction  `json:"actions,omitempty"`
 }
 
+type EmailRoutingCatchAllRuleResponse struct {
+	Result EmailRoutingCatchAllRule `json:"result"`
+	Response
+}
+
 // ListEmailRoutingRules Lists existing routing rules.
 //
 // API reference: https://api.cloudflare.com/#email-routing-routing-rules-list-routing-rules
@@ -227,21 +232,19 @@ func (api *API) GetEmailRoutingCatchAllRule(ctx context.Context, rc *ResourceCon
 	if rc.Identifier == "" {
 		return EmailRoutingCatchAllRule{}, ErrMissingZoneID
 	}
-
 	uri := fmt.Sprintf("/zones/%s/email/routing/rules/catch_all", rc.Identifier)
-
 	res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
 	if err != nil {
 		return EmailRoutingCatchAllRule{}, err
 	}
 
-	var r EmailRoutingCatchAllRule
+	var r EmailRoutingCatchAllRuleResponse
 	err = json.Unmarshal(res, &r)
 	if err != nil {
 		return EmailRoutingCatchAllRule{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
 	}
 
-	return r, nil
+	return r.Result, nil
 }
 
 // UpdateEmailRoutingCatchAllRule Enable or disable catch-all routing rule, or change action to forward to specific destination address.
@@ -259,11 +262,11 @@ func (api *API) UpdateEmailRoutingCatchAllRule(ctx context.Context, rc *Resource
 		return EmailRoutingCatchAllRule{}, err
 	}
 
-	var r EmailRoutingCatchAllRule
+	var r EmailRoutingCatchAllRuleResponse
 	err = json.Unmarshal(res, &r)
 	if err != nil {
 		return EmailRoutingCatchAllRule{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
 	}
 
-	return r, nil
+	return r.Result, nil
 }


Test patch:
diff --git a/email_routing_rules_test.go b/email_routing_rules_test.go
index 502f88ef2..5a3cdb4d7 100644
--- a/email_routing_rules_test.go
+++ b/email_routing_rules_test.go
@@ -280,22 +280,28 @@ func TestEmailRouting_GetAllRule(t *testing.T) {
 		assert.Equal(t, http.MethodGet, r.Method, "Expected method 'PUT', got %s", r.Method)
 		w.Header().Set("content-type", "application/json")
 		fmt.Fprint(w, `{
-  "tag": "a7e6fb77503c41d8a7f3113c6918f10c",
-  "name": "Rule send to user@example.net",
-  "enabled": true,
-  "matchers": [
-    {
-      "type": "all"
-    }
-  ],
-  "actions": [
-    {
-      "type": "forward",
-      "value": [
-        "destinationaddress@example.net"
-      ]
-    }
-  ]
+  "result": {
+    "tag": "a7e6fb77503c41d8a7f3113c6918f10c",
+    "name": "Send to user@example.net rule.",
+    "matchers": [
+      {
+        "type": "all"
+      }
+    ],
+    "actions": [
+      {
+        "type": "forward",
+        "value": [
+          "destinationaddress@example.net"
+        ]
+      }
+    ],
+    "enabled": true,
+    "priority": 2147483647
+  },
+  "success": true,
+  "errors": [],
+  "messages": []
 }`)
 	})
 
@@ -306,7 +312,7 @@ func TestEmailRouting_GetAllRule(t *testing.T) {
 
 	want := EmailRoutingCatchAllRule{
 		Tag:     "a7e6fb77503c41d8a7f3113c6918f10c",
-		Name:    "Rule send to user@example.net",
+		Name:    "Send to user@example.net rule.",
 		Enabled: BoolPtr(true),
 		Matchers: []EmailRoutingRuleMatcher{
 			{
@@ -335,22 +341,28 @@ func TestEmailRouting_UpdateAllRule(t *testing.T) {
 		assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method)
 		w.Header().Set("content-type", "application/json")
 		fmt.Fprint(w, `{
-  "tag": "a7e6fb77503c41d8a7f3113c6918f10c",
-  "name": "Rule send to user@example.net",
-  "enabled": true,
-  "matchers": [
-    {
-      "type": "all"
-    }
-  ],
-  "actions": [
-    {
-      "type": "forward",
-      "value": [
-        "destinationaddress@example.net"
-      ]
-    }
-  ]
+  "result": {
+    "tag": "a7e6fb77503c41d8a7f3113c6918f10c",
+    "name": "Send to user@example.net rule.",
+    "matchers": [
+      {
+        "type": "all"
+      }
+    ],
+    "actions": [
+      {
+        "type": "forward",
+        "value": [
+          "destinationaddress@example.net"
+        ]
+      }
+    ],
+    "enabled": true,
+    "priority": 2147483647
+  },
+  "success": true,
+  "errors": [],
+  "messages": []
 }`)
 	})
 
@@ -361,7 +373,7 @@ func TestEmailRouting_UpdateAllRule(t *testing.T) {
 
 	want := EmailRoutingCatchAllRule{
 		Tag:     "a7e6fb77503c41d8a7f3113c6918f10c",
-		Name:    "Rule send to user@example.net",
+		Name:    "Send to user@example.net rule.",
 		Enabled: BoolPtr(true),
 		Matchers: []EmailRoutingRuleMatcher{
 			{