Repository: cloudflare/cloudflare-go
Pull request #1379 (2023-08-27 05:08:15)

Fix issue parsing Image Details from API due to incorrect struct json field
<!-- 
Provide a general summary of your changes in the title above. You should
remove this overview, any sections and any section descriptions you
don't need below before submitting. There isn't a strict requirement to
use this template if you can structure your description and still cover
these points. 
-->

## Description

<!--
Describe your changes in detail through motivation and context. Why is
this change required? What problem does it solve? If it fixes an open
issue, link to the issue using GitHub's closing issues keywords[1].
-->

Issue is described here: https://github.com/cloudflare/cloudflare-go/issues/1378
API returns the metadata object under the field `meta`. However the SDK expects the field `metadata` thus it always drops this data from the response.

## Has your change been tested?

Yes - after changing the struct field for Metadata to `meta`, `GetImage` calls return images with Metadata field populated.

<!--
Explain how the change has been tested and what you ran to confirm your
change affects other parts of the code. Automated tests are generally
expected and changes without tests should explain why they aren't
required.
-->

## Screenshots (if appropriate):

https://developers.cloudflare.com/api/operations/cloudflare-images-image-details

Refer to the response example of Image details. Specifically the `meta` field. The current SDK expects `metadata` and thus it always drops the metadata from the response.

## 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:

- [x] 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.
- [x] This change is using publicly documented in [cloudflare/api-schemas](https://github.com/cloudflare/api-schemas) 
      and relies on stable APIs.

[1]: https://help.github.com/articles/closing-issues-using-keywords/



Patch:
diff --git a/.changelog/1379.txt b/.changelog/1379.txt
new file mode 100644
index 000000000..d3d637aa9
--- /dev/null
+++ b/.changelog/1379.txt
@@ -0,0 +1,7 @@
+```release-note:bug
+images: Fix issue parsing Image Details from API due to incorrect struct json field
+```
+
+```release-note:breaking-change
+images: Renamed Image struct "Metadata" field to "Meta"
+```
diff --git a/images.go b/images.go
index 12dea5ded..35208ea80 100644
--- a/images.go
+++ b/images.go
@@ -29,7 +29,7 @@ const (
 type Image struct {
 	ID                string                 `json:"id"`
 	Filename          string                 `json:"filename"`
-	Metadata          map[string]interface{} `json:"metadata,omitempty"`
+	Meta              map[string]interface{} `json:"meta,omitempty"`
 	RequireSignedURLs bool                   `json:"requireSignedURLs"`
 	Variants          []string               `json:"variants"`
 	Uploaded          time.Time              `json:"uploaded"`


Test patch:
diff --git a/images_test.go b/images_test.go
index a8f089cd9..f986a3e68 100644
--- a/images_test.go
+++ b/images_test.go
@@ -27,7 +27,7 @@ func timeMustParse(layout, value string) time.Time {
 var expectedImageStruct = Image{
 	ID:       "ZxR0pLaXRldlBtaFhhO2FiZGVnaA",
 	Filename: "avatar.png",
-	Metadata: map[string]interface{}{
+	Meta: map[string]interface{}{
 		"meta": "metaID",
 	},
 	RequireSignedURLs: true,
@@ -63,7 +63,7 @@ func TestUploadImage(t *testing.T) {
 			"result": {
 				"id": "ZxR0pLaXRldlBtaFhhO2FiZGVnaA",
 				"filename": "avatar.png",
-				"metadata": {
+				"meta": {
 					"meta": "metaID"
 				},
 				"requireSignedURLs": true,
@@ -121,7 +121,7 @@ func TestUploadImageByUrl(t *testing.T) {
 			"result": {
 				"id": "ZxR0pLaXRldlBtaFhhO2FiZGVnaA",
 				"filename": "avatar.png",
-				"metadata": {
+				"meta": {
 					"meta": "metaID"
 				},
 				"requireSignedURLs": true,
@@ -179,7 +179,7 @@ func TestUpdateImage(t *testing.T) {
 			"result": {
 				"id": "ZxR0pLaXRldlBtaFhhO2FiZGVnaA",
 				"filename": "avatar.png",
-				"metadata": {
+				"meta": {
 					"meta": "metaID"
 				},
 				"requireSignedURLs": true,
@@ -337,7 +337,7 @@ func TestListImages(t *testing.T) {
 					{
 						"id": "ZxR0pLaXRldlBtaFhhO2FiZGVnaA",
 						"filename": "avatar.png",
-						"metadata": {
+						"meta": {
 							"meta": "metaID"
 						},
 						"requireSignedURLs": true,
@@ -378,7 +378,7 @@ func TestImageDetails(t *testing.T) {
 			"result": {
 				"id": "ZxR0pLaXRldlBtaFhhO2FiZGVnaA",
 				"filename": "avatar.png",
-				"metadata": {
+				"meta": {
 					"meta": "metaID"
 				},
 				"requireSignedURLs": true,