Repository: jomei/notionapi
Pull request #90 (2022-06-03 18:26:44)

Fix File Property
### Issue

It seems that the current `Files` property type was not up to date. In the current version of `notionapi` any update to a page that contains a `Files` property will yield the following error

```
body failed validation. Fix one:
body.properties.Merge Request.files[0].file should be defined, instead was `undefined`.
body.properties.Merge Request.files[0].external should be defined, instead was `undefined`.
```

### Fix
This PR adds the missing fields to the struct such that the request succeeds


Patch:
diff --git a/object.go b/object.go
index b7a153b..325745e 100644
--- a/object.go
+++ b/object.go
@@ -111,7 +111,10 @@ func (d *Date) UnmarshalText(data []byte) error {
 type FileType string
 
 type File struct {
-	Name string `json:"name"`
+	Name     string      `json:"name"`
+	Type     FileType    `json:"type"`
+	File     *FileObject `json:"file,omitempty"`
+	External *FileObject `json:"external,omitempty"`
 }
 
 type FileObject struct {


Test patch:
diff --git a/page_test.go b/page_test.go
index 1ef5f72..d4bd23f 100644
--- a/page_test.go
+++ b/page_test.go
@@ -95,6 +95,19 @@ func TestPageClient(t *testing.T) {
 								},
 							},
 						},
+						"Some Files": &notionapi.FilesProperty{
+							ID:   "files",
+							Type: "files",
+							Files: []notionapi.File{
+								{
+									Name: "https://google.com",
+									Type: "external",
+									External: &notionapi.FileObject{
+										URL: "https://google.com",
+									},
+								},
+							},
+						},
 						"Name": &notionapi.TitleProperty{
 							ID:   "title",
 							Type: "title",
@@ -267,6 +280,25 @@ func TestPageClient(t *testing.T) {
 								},
 							},
 						},
+						"Important Files": notionapi.FilesProperty{
+							Type: "files",
+							Files: []notionapi.File{
+								{
+									Type: "external",
+									Name: "https://google.com",
+									External: &notionapi.FileObject{
+										URL: "https://google.com",
+									},
+								},
+								{
+									Type: "external",
+									Name: "https://123.com",
+									External: &notionapi.FileObject{
+										URL: "https://123.com",
+									},
+								},
+							},
+						},
 					},
 				},
 				want: &notionapi.Page{
diff --git a/testdata/page_get.json b/testdata/page_get.json
index beb4760..4c54c29 100644
--- a/testdata/page_get.json
+++ b/testdata/page_get.json
@@ -83,6 +83,19 @@
         "href": null
       }]
     },
+    "Some Files": {
+      "id": "files",
+      "type": "files",
+      "files": [
+        {
+          "name": "https://google.com",
+          "type": "external",
+          "external": {
+            "url": "https://google.com"
+          }
+        }
+      ]
+    },
     "RollupArray": {
       "id": "abcd",
       "type": "rollup",
diff --git a/testdata/page_update.json b/testdata/page_update.json
index 2380403..7e4b0b6 100644
--- a/testdata/page_update.json
+++ b/testdata/page_update.json
@@ -68,6 +68,19 @@
         }
       ]
     },
+    "Important Files": {
+      "id": "importantfiles!",
+      "type": "files",
+      "files": [
+        {
+          "name": "https://google.com",
+          "type": "external",
+          "external": {
+            "url": "https://google.com"
+          }
+        }
+      ]
+    },
     "Name": {
       "id": "title",
       "type": "title",