From 89c80850c2c9ef5ae48568c2693acefcdd31ad1f Mon Sep 17 00:00:00 2001 From: Herman Schoenfeld Date: Fri, 5 Aug 2022 11:59:06 +1000 Subject: [PATCH 1/3] Refactored Color abstraction - Color is now an enum in the DTO's - Added missing Color to ToggleBlock --- .../PropertySchema/SelectOptionSchema.cs | 4 +- Src/Notion.Client/Models/Blocks/BlockType.cs | 1 + .../Models/Blocks/BulletedListItemBlock.cs | 5 ++ .../Models/Blocks/CalloutBlock.cs | 5 ++ Src/Notion.Client/Models/Blocks/Color.cs | 65 +++++++++++++++++++ .../Models/Blocks/HeadingOneBlock.cs | 6 ++ .../Models/Blocks/HeadingThreeeBlock.cs | 6 ++ .../Models/Blocks/HeadingTwoBlock.cs | 6 ++ .../Models/Blocks/NumberedListItemBlock.cs | 5 ++ .../Models/Blocks/ParagraphBlock.cs | 5 ++ Src/Notion.Client/Models/Blocks/QuoteBlock.cs | 5 ++ .../Models/Blocks/TableOfContentsBlock.cs | 4 ++ Src/Notion.Client/Models/Blocks/ToDoBlock.cs | 5 ++ .../Models/Blocks/ToggleBlock.cs | 5 ++ .../Database/Properties/SelectProperty.cs | 4 +- .../Models/Database/RichText/RichTextBase.cs | 4 +- 16 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 Src/Notion.Client/Models/Blocks/Color.cs diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesCreateParameters/PropertySchema/SelectOptionSchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesCreateParameters/PropertySchema/SelectOptionSchema.cs index 12989d1f..c31aef9f 100644 --- a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesCreateParameters/PropertySchema/SelectOptionSchema.cs +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesCreateParameters/PropertySchema/SelectOptionSchema.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace Notion.Client { @@ -8,6 +9,7 @@ public class SelectOptionSchema public string Name { get; set; } [JsonProperty("color")] - public string Color { get; set; } + [JsonConverter(typeof(StringEnumConverter))] + public Color Color { get; set; } } } diff --git a/Src/Notion.Client/Models/Blocks/BlockType.cs b/Src/Notion.Client/Models/Blocks/BlockType.cs index f3181b22..6ad45f73 100644 --- a/Src/Notion.Client/Models/Blocks/BlockType.cs +++ b/Src/Notion.Client/Models/Blocks/BlockType.cs @@ -100,4 +100,5 @@ public enum BlockType [EnumMember(Value = "unsupported")] Unsupported } + } diff --git a/Src/Notion.Client/Models/Blocks/BulletedListItemBlock.cs b/Src/Notion.Client/Models/Blocks/BulletedListItemBlock.cs index 145a37e5..e6711de5 100644 --- a/Src/Notion.Client/Models/Blocks/BulletedListItemBlock.cs +++ b/Src/Notion.Client/Models/Blocks/BulletedListItemBlock.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace Notion.Client { @@ -15,6 +16,10 @@ public class Info [JsonProperty("rich_text")] public IEnumerable RichText { get; set; } + [JsonProperty("color")] + [JsonConverter(typeof(StringEnumConverter))] + public Color Color { get; set; } + [JsonProperty("children")] public IEnumerable Children { get; set; } } diff --git a/Src/Notion.Client/Models/Blocks/CalloutBlock.cs b/Src/Notion.Client/Models/Blocks/CalloutBlock.cs index f995e3be..d694e33c 100644 --- a/Src/Notion.Client/Models/Blocks/CalloutBlock.cs +++ b/Src/Notion.Client/Models/Blocks/CalloutBlock.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace Notion.Client { @@ -18,6 +19,10 @@ public class Info [JsonProperty("icon")] public IPageIcon Icon { get; set; } + [JsonProperty("color")] + [JsonConverter(typeof(StringEnumConverter))] + public Color Color { get; set; } + [JsonProperty("children")] public IEnumerable Children { get; set; } } diff --git a/Src/Notion.Client/Models/Blocks/Color.cs b/Src/Notion.Client/Models/Blocks/Color.cs new file mode 100644 index 00000000..ae75df22 --- /dev/null +++ b/Src/Notion.Client/Models/Blocks/Color.cs @@ -0,0 +1,65 @@ +using System.Runtime.Serialization; + +namespace Notion.Client +{ + public enum Color + { + [EnumMember(Value = "default")] + Default, + + [EnumMember(Value = "gray")] + Gray, + + [EnumMember(Value = "brown")] + Brown, + + [EnumMember(Value = "orange")] + Orange, + + [EnumMember(Value = "yellow")] + Yellow, + + [EnumMember(Value = "green")] + Green, + + [EnumMember(Value = "blue")] + Blue, + + [EnumMember(Value = "purple")] + Purple, + + [EnumMember(Value = "pink")] + Pink, + + [EnumMember(Value = "red")] + Red, + + [EnumMember(Value = "gray_background")] + GrayBackground, + + [EnumMember(Value = "brown_background")] + BrownBackground, + + [EnumMember(Value = "orange_background")] + OrangeBackground, + + [EnumMember(Value = "yellow_background")] + YellowBackground, + + [EnumMember(Value = "green_background")] + GreenBackground, + + [EnumMember(Value = "blue_background")] + BlueBackground, + + [EnumMember(Value = "purple_background")] + PurpleBackground, + + [EnumMember(Value = "pink_background")] + PinkBackground, + + [EnumMember(Value = "red_background")] + RedBackground, + + } +} diff --git a/Src/Notion.Client/Models/Blocks/HeadingOneBlock.cs b/Src/Notion.Client/Models/Blocks/HeadingOneBlock.cs index 67132fe5..8c8d1659 100644 --- a/Src/Notion.Client/Models/Blocks/HeadingOneBlock.cs +++ b/Src/Notion.Client/Models/Blocks/HeadingOneBlock.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace Notion.Client { @@ -16,6 +17,11 @@ public class Info { [JsonProperty("rich_text")] public IEnumerable RichText { get; set; } + + [JsonProperty("color")] + [JsonConverter(typeof(StringEnumConverter))] + public Color Color { get; set; } + } } } diff --git a/Src/Notion.Client/Models/Blocks/HeadingThreeeBlock.cs b/Src/Notion.Client/Models/Blocks/HeadingThreeeBlock.cs index 2e4dde99..a9c3efd3 100644 --- a/Src/Notion.Client/Models/Blocks/HeadingThreeeBlock.cs +++ b/Src/Notion.Client/Models/Blocks/HeadingThreeeBlock.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace Notion.Client { @@ -16,6 +17,11 @@ public class Info { [JsonProperty("rich_text")] public IEnumerable RichText { get; set; } + + [JsonProperty("color")] + [JsonConverter(typeof(StringEnumConverter))] + public Color Color { get; set; } + } } } diff --git a/Src/Notion.Client/Models/Blocks/HeadingTwoBlock.cs b/Src/Notion.Client/Models/Blocks/HeadingTwoBlock.cs index 82c75047..47756093 100644 --- a/Src/Notion.Client/Models/Blocks/HeadingTwoBlock.cs +++ b/Src/Notion.Client/Models/Blocks/HeadingTwoBlock.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace Notion.Client { @@ -16,6 +17,11 @@ public class Info { [JsonProperty("rich_text")] public IEnumerable RichText { get; set; } + + [JsonProperty("color")] + [JsonConverter(typeof(StringEnumConverter))] + public Color Color { get; set; } + } } } diff --git a/Src/Notion.Client/Models/Blocks/NumberedListItemBlock.cs b/Src/Notion.Client/Models/Blocks/NumberedListItemBlock.cs index 581d8977..189eedf1 100644 --- a/Src/Notion.Client/Models/Blocks/NumberedListItemBlock.cs +++ b/Src/Notion.Client/Models/Blocks/NumberedListItemBlock.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace Notion.Client { @@ -15,6 +16,10 @@ public class Info [JsonProperty("rich_text")] public IEnumerable RichText { get; set; } + [JsonProperty("color")] + [JsonConverter(typeof(StringEnumConverter))] + public Color Color { get; set; } + [JsonProperty("children")] public IEnumerable Children { get; set; } } diff --git a/Src/Notion.Client/Models/Blocks/ParagraphBlock.cs b/Src/Notion.Client/Models/Blocks/ParagraphBlock.cs index 02560aee..ff3ae2a3 100644 --- a/Src/Notion.Client/Models/Blocks/ParagraphBlock.cs +++ b/Src/Notion.Client/Models/Blocks/ParagraphBlock.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace Notion.Client { @@ -14,6 +15,10 @@ public class Info { [JsonProperty("rich_text")] public IEnumerable RichText { get; set; } + + [JsonProperty("color")] + [JsonConverter(typeof(StringEnumConverter))] + public Color Color { get; set; } [JsonProperty("children")] public IEnumerable Children { get; set; } diff --git a/Src/Notion.Client/Models/Blocks/QuoteBlock.cs b/Src/Notion.Client/Models/Blocks/QuoteBlock.cs index 9a8dda8e..68479075 100644 --- a/Src/Notion.Client/Models/Blocks/QuoteBlock.cs +++ b/Src/Notion.Client/Models/Blocks/QuoteBlock.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace Notion.Client { @@ -15,6 +16,10 @@ public class Info [JsonProperty("rich_text")] public IEnumerable RichText { get; set; } + [JsonProperty("color")] + [JsonConverter(typeof(StringEnumConverter))] + public Color Color { get; set; } + [JsonProperty("children")] public IEnumerable Children { get; set; } } diff --git a/Src/Notion.Client/Models/Blocks/TableOfContentsBlock.cs b/Src/Notion.Client/Models/Blocks/TableOfContentsBlock.cs index 49e11bf4..29305ca9 100644 --- a/Src/Notion.Client/Models/Blocks/TableOfContentsBlock.cs +++ b/Src/Notion.Client/Models/Blocks/TableOfContentsBlock.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace Notion.Client { @@ -11,6 +12,9 @@ public class TableOfContentsBlock : Block, IColumnChildrenBlock, INonColumnBlock public class Data { + [JsonProperty("color")] + [JsonConverter(typeof(StringEnumConverter))] + public Color Color { get; set; } } } } diff --git a/Src/Notion.Client/Models/Blocks/ToDoBlock.cs b/Src/Notion.Client/Models/Blocks/ToDoBlock.cs index 0417827d..d2fcc75e 100644 --- a/Src/Notion.Client/Models/Blocks/ToDoBlock.cs +++ b/Src/Notion.Client/Models/Blocks/ToDoBlock.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace Notion.Client { @@ -18,6 +19,10 @@ public class Info [JsonProperty("checked")] public bool IsChecked { get; set; } + [JsonProperty("color")] + [JsonConverter(typeof(StringEnumConverter))] + public Color Color { get; set; } + [JsonProperty("children")] public IEnumerable Children { get; set; } } diff --git a/Src/Notion.Client/Models/Blocks/ToggleBlock.cs b/Src/Notion.Client/Models/Blocks/ToggleBlock.cs index 2653cd1b..1367f40b 100644 --- a/Src/Notion.Client/Models/Blocks/ToggleBlock.cs +++ b/Src/Notion.Client/Models/Blocks/ToggleBlock.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace Notion.Client { @@ -15,6 +16,10 @@ public class Info [JsonProperty("rich_text")] public IEnumerable RichText { get; set; } + [JsonProperty("color")] + [JsonConverter(typeof(StringEnumConverter))] + public Color Color { get; set; } + [JsonProperty("children")] public IEnumerable Children { get; set; } } diff --git a/Src/Notion.Client/Models/Database/Properties/SelectProperty.cs b/Src/Notion.Client/Models/Database/Properties/SelectProperty.cs index 47e6fa64..dd1dfeb1 100644 --- a/Src/Notion.Client/Models/Database/Properties/SelectProperty.cs +++ b/Src/Notion.Client/Models/Database/Properties/SelectProperty.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace Notion.Client { @@ -33,7 +34,8 @@ public class SelectOption /// Color of the option. Possible values are: "default", "gray", "brown", "red", "orange", "yellow", "green", "blue", "purple", "pink". Defaults to "default". /// [JsonProperty("color")] - public string Color { get; set; } + [JsonConverter(typeof(StringEnumConverter))] + public Color Color { get; set; } } public class MultiSelectProperty : Property diff --git a/Src/Notion.Client/Models/Database/RichText/RichTextBase.cs b/Src/Notion.Client/Models/Database/RichText/RichTextBase.cs index 1c9a7610..0b509325 100644 --- a/Src/Notion.Client/Models/Database/RichText/RichTextBase.cs +++ b/Src/Notion.Client/Models/Database/RichText/RichTextBase.cs @@ -42,7 +42,7 @@ public class Annotations public bool IsCode { get; set; } [JsonProperty("color")] - // color: Color | BackgroundColor - public string Color { get; set; } + [JsonConverter(typeof(StringEnumConverter))] + public Color Color { get; set; } } } From 3978493bb33d35cf608066428aa390226d4312d1 Mon Sep 17 00:00:00 2001 From: Herman Schoenfeld Date: Fri, 5 Aug 2022 12:11:06 +1000 Subject: [PATCH 2/3] Build error and code style compliance --- Src/Notion.Client/Models/Blocks/BlockType.cs | 1 - Src/Notion.Client/Models/Blocks/Color.cs | 1 - .../Models/Blocks/HeadingOneBlock.cs | 1 - .../Models/Blocks/HeadingThreeeBlock.cs | 1 - .../Models/Blocks/HeadingTwoBlock.cs | 1 - Test/Notion.UnitTests/DatabasesClientTests.cs | 24 +++++++++---------- 6 files changed, 12 insertions(+), 17 deletions(-) diff --git a/Src/Notion.Client/Models/Blocks/BlockType.cs b/Src/Notion.Client/Models/Blocks/BlockType.cs index 6ad45f73..f3181b22 100644 --- a/Src/Notion.Client/Models/Blocks/BlockType.cs +++ b/Src/Notion.Client/Models/Blocks/BlockType.cs @@ -100,5 +100,4 @@ public enum BlockType [EnumMember(Value = "unsupported")] Unsupported } - } diff --git a/Src/Notion.Client/Models/Blocks/Color.cs b/Src/Notion.Client/Models/Blocks/Color.cs index ae75df22..b4682a7b 100644 --- a/Src/Notion.Client/Models/Blocks/Color.cs +++ b/Src/Notion.Client/Models/Blocks/Color.cs @@ -60,6 +60,5 @@ public enum Color [EnumMember(Value = "red_background")] RedBackground, - } } diff --git a/Src/Notion.Client/Models/Blocks/HeadingOneBlock.cs b/Src/Notion.Client/Models/Blocks/HeadingOneBlock.cs index 8c8d1659..ccf29220 100644 --- a/Src/Notion.Client/Models/Blocks/HeadingOneBlock.cs +++ b/Src/Notion.Client/Models/Blocks/HeadingOneBlock.cs @@ -21,7 +21,6 @@ public class Info [JsonProperty("color")] [JsonConverter(typeof(StringEnumConverter))] public Color Color { get; set; } - } } } diff --git a/Src/Notion.Client/Models/Blocks/HeadingThreeeBlock.cs b/Src/Notion.Client/Models/Blocks/HeadingThreeeBlock.cs index a9c3efd3..73897da0 100644 --- a/Src/Notion.Client/Models/Blocks/HeadingThreeeBlock.cs +++ b/Src/Notion.Client/Models/Blocks/HeadingThreeeBlock.cs @@ -21,7 +21,6 @@ public class Info [JsonProperty("color")] [JsonConverter(typeof(StringEnumConverter))] public Color Color { get; set; } - } } } diff --git a/Src/Notion.Client/Models/Blocks/HeadingTwoBlock.cs b/Src/Notion.Client/Models/Blocks/HeadingTwoBlock.cs index 47756093..7b4e161e 100644 --- a/Src/Notion.Client/Models/Blocks/HeadingTwoBlock.cs +++ b/Src/Notion.Client/Models/Blocks/HeadingTwoBlock.cs @@ -21,7 +21,6 @@ public class Info [JsonProperty("color")] [JsonConverter(typeof(StringEnumConverter))] public Color Color { get; set; } - } } } diff --git a/Test/Notion.UnitTests/DatabasesClientTests.cs b/Test/Notion.UnitTests/DatabasesClientTests.cs index b87fdd5e..a3254131 100644 --- a/Test/Notion.UnitTests/DatabasesClientTests.cs +++ b/Test/Notion.UnitTests/DatabasesClientTests.cs @@ -214,17 +214,17 @@ public async Task CreateDatabaseAsync() { new SelectOptionSchema { - Color = "green", + Color = Color.Green, Name = "🥦Vegetable" }, new SelectOptionSchema { - Color = "red", + Color = Color.Red, Name = "🍎Fruit" }, new SelectOptionSchema { - Color = "yellow", + Color = Color.Yellow, Name = "💪Protein" } } @@ -248,17 +248,17 @@ public async Task CreateDatabaseAsync() option => { option.Name.Should().Be("🥦Vegetable"); - option.Color.Should().Be("green"); + option.Color.Should().Be(Color.Green); }, option => { option.Name.Should().Be("🍎Fruit"); - option.Color.Should().Be("red"); + option.Color.Should().Be(Color.Red); }, option => { option.Name.Should().Be("💪Protein"); - option.Color.Should().Be("yellow"); + option.Color.Should().Be(Color.Yellow); } ); } @@ -303,17 +303,17 @@ public async Task UpdateDatabaseAsync() { new SelectOption { - Color = "green", + Color = Color.Green, Name = "🥦Vegetables" }, new SelectOption { - Color = "red", + Color = Color.Red, Name = "🍎Fruit" }, new SelectOption { - Color = "yellow", + Color = Color.Yellow, Name = "💪Protein" } } @@ -346,17 +346,17 @@ public async Task UpdateDatabaseAsync() option => { option.Name.Should().Be("🥦Vegetables"); - option.Color.Should().Be("green"); + option.Color.Should().Be(Color.Green); }, option => { option.Name.Should().Be("🍎Fruit"); - option.Color.Should().Be("red"); + option.Color.Should().Be(Color.Red); }, option => { option.Name.Should().Be("💪Protein"); - option.Color.Should().Be("yellow"); + option.Color.Should().Be(Color.Yellow); } ); From 0024ce711502fe7ecefc24235661afc98c1a58be Mon Sep 17 00:00:00 2001 From: Herman Schoenfeld Date: Thu, 11 Aug 2022 10:54:11 +1000 Subject: [PATCH 3/3] Formatting issue on ParagraphBlock.cs --- Src/Notion.Client/Models/Blocks/ParagraphBlock.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/Notion.Client/Models/Blocks/ParagraphBlock.cs b/Src/Notion.Client/Models/Blocks/ParagraphBlock.cs index ff3ae2a3..f45e8628 100644 --- a/Src/Notion.Client/Models/Blocks/ParagraphBlock.cs +++ b/Src/Notion.Client/Models/Blocks/ParagraphBlock.cs @@ -15,7 +15,7 @@ public class Info { [JsonProperty("rich_text")] public IEnumerable RichText { get; set; } - + [JsonProperty("color")] [JsonConverter(typeof(StringEnumConverter))] public Color Color { get; set; }