notion-dotnet / notion-sdk-net

A Notion SDK for .Net
MIT License
181 stars 44 forks source link

Enable the Unsupported Object #419

Open kosaku-hayashi opened 1 month ago

kosaku-hayashi commented 1 month ago

Description

Currently, if a property or block newly added to Notion is included in the response, a serializer exception is raised and the The client will stop working.

Example: Newtonsoft.Json.JsonSerializationException: 'Error converting value "{New property or block}" to type 'Notion.Client.PropertyValueType'

This is because there is no matching enum value for the type string, and the existing StringEnumConverter does not support returning a default value in such cases. The existing code provides a class that accepts unsupported blocks called UnsupportedBlock, but this is effectively a non-functional state.

// Some other attributes
[JsonSubtypes.KnownSubTypeAttribute(typeof(UnsupportedBlock), BlockType.Unsupported)] <-- It will never be deserialized.
public interface IBlock : IObject, IObjectModificationData
{
    [JsonProperty("type")]
    [JsonConverter(typeof(StringEnumConverter))] <-- Cannot return default value if enum value does not exist
    BlockType Type { get; set; }

    [JsonProperty("has_children")]
    bool HasChildren { get; set; }

    [JsonProperty("in_trash")]
    bool InTrash { get; set; }

    [JsonProperty("parent")]
    IBlockParent Parent { get; set; }
}
public enum BlockType
{
    // Some other Type values

    [EnumMember(Value = "unsupported")] <-- Newly added block type values are never "unsupported".
    Unsupported
}

This problem could be remedied by creating a custom converter that returns a default value (i.e., "UnSupported") if the type string does not match and defining it on the enum side. Property and PropertyValue also raise the same exception, so it may be necessary to create UnSupportedProperty and UnSupportedPropertyValue in a similar manner.