Repository: jomei/notionapi
Pull request #98 (2022-08-08 09:32:11)

fix: omitempty user
## What

- Make some fields of the User struct omitempty

## Why

- When creating a Page with a PeopleProperty, an error occurred.
- After applying this fix, that error will no longer occur.

### Code

```go
package main

import (
	"context"

	"github.com/jomei/notionapi"
)

func main() {
	cli := notionapi.NewClient("YOUR_TOKEN")
	_, err := cli.Page.Create(context.Background(), &notionapi.PageCreateRequest{
		Parent: notionapi.Parent{
			Type:       notionapi.ParentTypeDatabaseID,
			DatabaseID: "YOUR_DATABASE_ID",
		},
		Properties: notionapi.Properties{
			"Person": notionapi.PeopleProperty{
				People: []notionapi.User{
					{
						ID: "YOUR_USER_ID",
					},
				},
			},
		},
	})
	if err != nil {
		panic(err)
	}
}
```

### Error

```
body.properties.Person.people[0].object should be `"user"` or `undefined`, instead was `""`.
body.properties.Person.people[0].type should be not present, instead was `""`.
body.properties.Person.people[0].name should be not present, instead was `""`.
body.properties.Person.people[0].avatar_url should be not present, instead was `""`.
body.properties.Person.people[0].person should be defined, instead was `undefined`.
body.properties.Person.people[0].bot should be defined, instead was `undefined`.
```



Patch:
diff --git a/.gitignore b/.gitignore
index 8da8698..44c53ad 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,3 +15,6 @@
 vendor/
 
 http-client.private.env.json
+
+# Auto generated by vscode pytest
+*.pytest_cache
diff --git a/block.go b/block.go
index e3c44b7..1edd79e 100644
--- a/block.go
+++ b/block.go
@@ -247,6 +247,7 @@ type Paragraph struct {
 	Text     []RichText `json:"text,omitempty"`
 	RichText []RichText `json:"rich_text"`
 	Children Blocks     `json:"children,omitempty"`
+	Color    string     `json:"color,omitempty"`
 }
 
 type Heading1Block struct {
@@ -257,6 +258,7 @@ type Heading1Block struct {
 type Heading struct {
 	RichText []RichText `json:"rich_text"`
 	Children Blocks     `json:"children,omitempty"`
+	Color    string     `json:"color,omitempty"`
 }
 
 type Heading2Block struct {
@@ -280,6 +282,7 @@ type Callout struct {
 	RichText []RichText `json:"rich_text"`
 	Icon     *Icon      `json:"icon,omitempty"`
 	Children Blocks     `json:"children,omitempty"`
+	Color    string     `json:"color,omitempty"`
 }
 
 type QuoteBlock struct {
@@ -292,6 +295,7 @@ type Quote struct {
 	Text     []RichText `json:"text,omitempty"`
 	RichText []RichText `json:"rich_text"`
 	Children Blocks     `json:"children,omitempty"`
+	Color    string     `json:"color,omitempty"`
 }
 
 type TableBlock struct {
@@ -325,6 +329,7 @@ type ListItem struct {
 	Text     []RichText `json:"text,omitempty"`
 	RichText []RichText `json:"rich_text"`
 	Children Blocks     `json:"children,omitempty"`
+	Color    string     `json:"color,omitempty"`
 }
 
 type NumberedListItemBlock struct {
@@ -343,6 +348,7 @@ type ToDo struct {
 	RichText []RichText `json:"rich_text,omitempty"`
 	Children Blocks     `json:"children,omitempty"`
 	Checked  bool       `json:"checked"`
+	Color    string     `json:"color,omitempty"`
 }
 
 type ToggleBlock struct {
@@ -359,6 +365,7 @@ type Toggle struct {
 	Text     []RichText `json:"text,omitempty"`
 	RichText []RichText `json:"rich_text"`
 	Children Blocks     `json:"children,omitempty"`
+	Color    string     `json:"color,omitempty"`
 }
 
 type ChildPageBlock struct {
@@ -476,6 +483,7 @@ type TableOfContentsBlock struct {
 
 type TableOfContents struct {
 	// empty
+	Color string `json:"color,omitempty"`
 }
 
 type DividerBlock struct {
diff --git a/user.go b/user.go
index 35f947b..1ba6de9 100644
--- a/user.go
+++ b/user.go
@@ -70,13 +70,13 @@ func (uc *UserClient) List(ctx context.Context, pagination *Pagination) (*UsersL
 type UserType string
 
 type User struct {
-	Object    ObjectType `json:"object"`
+	Object    ObjectType `json:"object,omitempty"`
 	ID        UserID     `json:"id"`
-	Type      UserType   `json:"type"`
-	Name      string     `json:"name"`
-	AvatarURL string     `json:"avatar_url"`
-	Person    *Person    `json:"person"`
-	Bot       *Bot       `json:"bot"`
+	Type      UserType   `json:"type,omitempty"`
+	Name      string     `json:"name,omitempty"`
+	AvatarURL string     `json:"avatar_url,omitempty"`
+	Person    *Person    `json:"person,omitempty"`
+	Bot       *Bot       `json:"bot,omitempty"`
 }
 
 type Person struct {


Test patch:
diff --git a/block_test.go b/block_test.go
index 487b29f..2d43bfd 100644
--- a/block_test.go
+++ b/block_test.go
@@ -80,12 +80,13 @@ func TestBlockClient(t *testing.T) {
 							Heading2: struct {
 								RichText []notionapi.RichText `json:"rich_text"`
 								Children notionapi.Blocks     `json:"children,omitempty"`
+								Color    string               `json:"color,omitempty"`
 							}{[]notionapi.RichText{
 								{
 									Type: notionapi.ObjectTypeText,
 									Text: notionapi.Text{Content: "Hello"},
 								},
-							}, nil,
+							}, nil, "",
 							},
 						},
 					},
@@ -121,6 +122,7 @@ func TestBlockClient(t *testing.T) {
 										PlainText: "AAAAAA",
 									},
 								},
+								Color: "blue",
 							},
 						},
 					},
@@ -139,7 +141,7 @@ func TestBlockClient(t *testing.T) {
 				}
 				a, err := json.Marshal(got)
 				if err != nil {
-					t.Errorf("AppendChildren() marhall error = %v", err)
+					t.Errorf("AppendChildren() marshal error = %v", err)
 					return
 				}
 				b, err := json.Marshal(tt.want)
@@ -238,6 +240,7 @@ func TestBlockClient(t *testing.T) {
 								Text: notionapi.Text{Content: "Hello"},
 							},
 						},
+						Color: notionapi.ColorYellow.String(),
 					},
 				},
 				want: &notionapi.ParagraphBlock{
@@ -259,6 +262,7 @@ func TestBlockClient(t *testing.T) {
 								PlainText:   "Hello",
 							},
 						},
+						Color: notionapi.ColorYellow.String(),
 					},
 				},
 				wantErr: false,
@@ -344,6 +348,7 @@ func TestBlockArrayUnmarshal(t *testing.T) {
 								Type:  "emoji",
 								Emoji: &emoji,
 							},
+							Color: notionapi.ColorBlue.String(),
 						},
 					},
 					&notionapi.Heading1Block{
@@ -369,6 +374,7 @@ func TestBlockArrayUnmarshal(t *testing.T) {
 									PlainText: "History 340",
 								},
 							},
+							Color: notionapi.ColorBrownBackground.String(),
 						},
 					},
 					&notionapi.ChildDatabaseBlock{
@@ -423,6 +429,7 @@ func TestBlockArrayUnmarshal(t *testing.T) {
 									PlainText: "Assignment Submission",
 								},
 							},
+							Color: notionapi.ColorDefault.String(),
 						},
 					},
 					&notionapi.ParagraphBlock{
@@ -448,6 +455,7 @@ func TestBlockArrayUnmarshal(t *testing.T) {
 									PlainText: "All essays and papers are due in lecture (due dates are listed on the schedule). No electronic copies will be accepted!",
 								},
 							},
+							Color: notionapi.ColorRed.String(),
 						},
 					},
 				},
diff --git a/page_test.go b/page_test.go
index d4bd23f..8833ac8 100644
--- a/page_test.go
+++ b/page_test.go
@@ -457,10 +457,7 @@ func TestPageCreateRequest_MarshallJSON(t *testing.T) {
 							Object: notionapi.ObjectTypeBlock,
 							Type:   notionapi.BlockTypeHeading2,
 						},
-						Heading2: struct {
-							RichText []notionapi.RichText `json:"rich_text"`
-							Children notionapi.Blocks     `json:"children,omitempty"`
-						}{
+						Heading2: notionapi.Heading{
 							RichText: []notionapi.RichText{
 								{
 									Type: notionapi.ObjectTypeText,
diff --git a/testdata/block_append_children.json b/testdata/block_append_children.json
index 358815e..2cddc70 100644
--- a/testdata/block_append_children.json
+++ b/testdata/block_append_children.json
@@ -36,10 +36,11 @@
             "plain_text": "AAAAAA",
             "href": null
           }
-        ]
+        ],
+        "color": "blue"
       }
     }
   ],
   "next_cursor": null,
   "has_more": false
-}
\ No newline at end of file
+}
diff --git a/testdata/block_array_unmarshal.json b/testdata/block_array_unmarshal.json
index 5285f40..d87c25d 100644
--- a/testdata/block_array_unmarshal.json
+++ b/testdata/block_array_unmarshal.json
@@ -45,7 +45,8 @@
         "icon": {
             "type": "emoji",
             "emoji": "📌"
-        }
+        },
+        "color": "blue"
     }
 }, {
     "object": "block",
@@ -76,7 +77,8 @@
                 "color": "default"
             },
             "plain_text": "History 340"
-        }]
+        }],
+        "color":"brown_background"
     }
 }, {
     "object": "block",
@@ -140,7 +142,8 @@
                 "color": "default"
             },
             "plain_text": "Assignment Submission"
-        }]
+        }],
+        "color":"default"
     }
 }, {
     "object": "block",
@@ -171,6 +174,7 @@
                 "color": "default"
             },
             "plain_text": "All essays and papers are due in lecture (due dates are listed on the schedule). No electronic copies will be accepted!"
-        }]
+        }],
+        "color":"red"
     }
-}]
\ No newline at end of file
+}]
diff --git a/testdata/block_update.json b/testdata/block_update.json
index 6d370a4..11fba6c 100644
--- a/testdata/block_update.json
+++ b/testdata/block_update.json
@@ -25,6 +25,7 @@
         "plain_text": "Hello",
         "href": null
       }
-    ]
+    ],
+    "color": "yellow"
   }
 }
diff --git a/testdata/database_list.json b/testdata/database_list.json
index 207a9f8..b7e09a1 100644
--- a/testdata/database_list.json
+++ b/testdata/database_list.json
@@ -47,7 +47,7 @@
             ]
           }
         },
-        "Some another columg": {
+        "Some another column": {
           "id": "rJt\\",
           "type": "people",
           "people": {}
diff --git a/testdata/page_create.json b/testdata/page_create.json
index 2460941..455f83b 100644
--- a/testdata/page_create.json
+++ b/testdata/page_create.json
@@ -23,7 +23,7 @@
       "type": "multi_select",
       "multi_select": []
     },
-    "Some another columg": {
+    "Some another column": {
       "id": "rJt\\",
       "type": "people",
       "people": []
diff --git a/testdata/page_update.json b/testdata/page_update.json
index 7e4b0b6..c0998ec 100644
--- a/testdata/page_update.json
+++ b/testdata/page_update.json
@@ -29,7 +29,7 @@
         }
       ]
     },
-    "Some another columg": {
+    "Some another column": {
       "id": "rJt\\",
       "type": "people",
       "people": [
diff --git a/testdata/search.json b/testdata/search.json
index fae8fa6..46ef805 100644
--- a/testdata/search.json
+++ b/testdata/search.json
@@ -25,7 +25,7 @@
           "type": "multi_select",
           "multi_select": []
         },
-        "Some another columg": {
+        "Some another column": {
           "id": "rJt\\",
           "type": "people",
           "people": []