diff --git a/Anthropic.SDK/Messaging/CacheControl.cs b/Anthropic.SDK/Messaging/CacheControl.cs index e915474..8e9792e 100644 --- a/Anthropic.SDK/Messaging/CacheControl.cs +++ b/Anthropic.SDK/Messaging/CacheControl.cs @@ -13,11 +13,11 @@ public class CacheControl /// [JsonPropertyName("ttl")] [JsonConverter(typeof(CacheDurationConverter))] - public CacheDuration TTL { get; set; } + public CacheDuration? TTL { get; set; } } [JsonConverter(typeof(JsonStringEnumConverter))] public enum CacheControlType { ephemeral -} \ No newline at end of file +} diff --git a/Anthropic.SDK/Messaging/CacheDurationConverter.cs b/Anthropic.SDK/Messaging/CacheDurationConverter.cs index 702c3eb..8cd90be 100644 --- a/Anthropic.SDK/Messaging/CacheDurationConverter.cs +++ b/Anthropic.SDK/Messaging/CacheDurationConverter.cs @@ -7,24 +7,30 @@ namespace Anthropic.SDK.Messaging; public enum CacheDuration { FiveMinutes, - OneHour + OneHour, } -public class CacheDurationConverter : JsonConverter +public class CacheDurationConverter : JsonConverter { - public override CacheDuration Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + public override CacheDuration? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { string? value = reader.GetString(); return value switch { + null => null, "5m" => CacheDuration.FiveMinutes, "1h" => CacheDuration.OneHour, _ => throw new JsonException($"Invalid cache duration: {value}") }; } - public override void Write(Utf8JsonWriter writer, CacheDuration value, JsonSerializerOptions options) + public override void Write(Utf8JsonWriter writer, CacheDuration? value, JsonSerializerOptions options) { + if (value is null) + { + return; + } + string str = value switch { CacheDuration.FiveMinutes => "5m", @@ -33,4 +39,4 @@ public override void Write(Utf8JsonWriter writer, CacheDuration value, JsonSeria }; writer.WriteStringValue(str); } -} \ No newline at end of file +}