From 3c5a596bd7ac02bf5e936c3e83c5122e943dd23e Mon Sep 17 00:00:00 2001 From: Fei Xu Date: Sun, 15 Dec 2024 17:11:06 -0500 Subject: [PATCH] Fix when no "SENT-BY" defined, get SentBy will throw exception (#669) * Fix when no "SENT-BY" defined, get SentBy will throw exception * Enable nullable reference types and add Journal3 test. Nullable reference types were enabled in the `Organizer` class, ensuring properties like `SentBy`, `DirectoryEntry`, and `Value` handle nullability appropriately. A new `Journal3` ICS file and its associated test were added to validate `SENT-BY` behavior in journals. --------- Co-authored-by: Fei Xu --- Ical.Net.Tests/Calendars/Journal/JOURNAL3.ics | 24 +++++++++++++ Ical.Net.Tests/IcsFiles.cs | 3 +- Ical.Net.Tests/JournalTest.cs | 13 ++++++- Ical.Net/DataTypes/Organizer.cs | 36 ++++++++++++++----- 4 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 Ical.Net.Tests/Calendars/Journal/JOURNAL3.ics diff --git a/Ical.Net.Tests/Calendars/Journal/JOURNAL3.ics b/Ical.Net.Tests/Calendars/Journal/JOURNAL3.ics new file mode 100644 index 000000000..80b319cd5 --- /dev/null +++ b/Ical.Net.Tests/Calendars/Journal/JOURNAL3.ics @@ -0,0 +1,24 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//ABC Corporation//NONSGML My Product//EN +BEGIN:VJOURNAL +DTSTAMP:19970324T120000Z +UID:uid5@host1.com +ORGANIZER;SENT-BY=;CN=JohnSmith;DIR="ldap://host.com:6666/ + o=3DDC%20Associates,c=3DUS??(cn=3DJohn%20Smith)":MAILTO:jsmith@host.com +STATUS:FINAL +CLASS:PRIVATE +CATEGORY:Project Report, XYZ, Weekly Meeting +DESCRIPTION:Project xyz Review Meeting Minutes\n + Agenda\n1. Review of project version 1.0 requirements.\n2. + Definition of project processes.\n3. Review of project schedule.\n + Participants: John Smith, Jane Doe, Jim Dandy\n-It was + decided that the requirements need to be signed off by + product marketing.\n-Project processes were accepted.\n + -Project schedule needs to account for scheduled holidays + and employee vacation time. Check with HR for specific + dates.\n-New schedule will be distributed by Friday.\n- + Next weeks meeting is cancelled. No meeting until 3/23. +SUMMARY:Project xyz Review Meeting +END:VJOURNAL +END:VCALENDAR diff --git a/Ical.Net.Tests/IcsFiles.cs b/Ical.Net.Tests/IcsFiles.cs index 135080b2f..a891521a4 100644 --- a/Ical.Net.Tests/IcsFiles.cs +++ b/Ical.Net.Tests/IcsFiles.cs @@ -82,6 +82,7 @@ internal static string ReadStream(string manifestResource) internal static string HourlyUntil1 => ReadStream("Ical.Net.Tests.Calendars.Recurrence.HourlyUntil1.ics"); internal static string Journal1 => ReadStream("Ical.Net.Tests.Calendars.Journal.JOURNAL1.ics"); internal static string Journal2 => ReadStream("Ical.Net.Tests.Calendars.Journal.JOURNAL2.ics"); + internal static string Journal3 => ReadStream("Ical.Net.Tests.Calendars.Journal.JOURNAL3.ics"); internal static string Language1 => ReadStream("Ical.Net.Tests.Calendars.Serialization.Language1.ics"); internal static string Language2 => ReadStream("Ical.Net.Tests.Calendars.Serialization.Language2.ics"); internal static string Language3 => ReadStream("Ical.Net.Tests.Calendars.Serialization.Language3.ics"); @@ -168,4 +169,4 @@ internal static string ReadStream(string manifestResource) internal static string LibicalIcalrecurTest => ReadStream("Ical.Net.Tests.contrib.libical.icalrecur_test.out"); -} \ No newline at end of file +} diff --git a/Ical.Net.Tests/JournalTest.cs b/Ical.Net.Tests/JournalTest.cs index 7fa4fff86..271447fe2 100644 --- a/Ical.Net.Tests/JournalTest.cs +++ b/Ical.Net.Tests/JournalTest.cs @@ -69,4 +69,15 @@ public void Journal2() Assert.That(j.Start, Is.Null); }); } -} \ No newline at end of file + + [Test, Category("Journal")] + public void Journal3() + { + var iCal = Calendar.Load(IcsFiles.Journal3); + ProgramTest.TestCal(iCal); + Assert.That(iCal.Journals, Has.Count.EqualTo(1)); + var j = iCal.Journals.First(); + + Assert.That(j.Organizer.SentBy, Is.Null, "Expected Organizer's SENT-BY to be null, but it was not."); + } +} diff --git a/Ical.Net/DataTypes/Organizer.cs b/Ical.Net/DataTypes/Organizer.cs index 0eed39872..0e4e2b8d4 100644 --- a/Ical.Net/DataTypes/Organizer.cs +++ b/Ical.Net/DataTypes/Organizer.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable using System; using System.Diagnostics; using System.IO; @@ -16,9 +17,17 @@ namespace Ical.Net.DataTypes; [DebuggerDisplay("{Value}")] public class Organizer : EncodableDataType { - public virtual Uri SentBy + public virtual Uri? SentBy { - get => new Uri(Parameters.Get("SENT-BY")); + get + { + string sentBy = Parameters.Get("SENT-BY"); + if (!string.IsNullOrWhiteSpace(sentBy)) + { + return new Uri(sentBy); + } + return null; + } set { if (value != null) @@ -27,7 +36,7 @@ public virtual Uri SentBy } else { - Parameters.Set("SENT-BY", (string) null); + Parameters.Remove("SENT-BY"); } } } @@ -38,9 +47,18 @@ public virtual string CommonName set => Parameters.Set("CN", value); } - public virtual Uri DirectoryEntry + public virtual Uri? DirectoryEntry { - get => new Uri(Parameters.Get("DIR")); + get + { + string dir = Parameters.Get("DIR"); + if (!string.IsNullOrWhiteSpace(dir)) + { + return new Uri(dir); + } + + return null; + } set { if (value != null) @@ -49,12 +67,12 @@ public virtual Uri DirectoryEntry } else { - Parameters.Set("DIR", (string) null); + Parameters.Remove("DIR"); } } } - public virtual Uri Value { get; set; } + public virtual Uri? Value { get; set; } public Organizer() { } @@ -71,7 +89,7 @@ public Organizer(string value) : this() protected bool Equals(Organizer other) => Equals(Value, other.Value); - public override bool Equals(object obj) + public override bool Equals(object? obj) { if (ReferenceEquals(null, obj)) { @@ -100,4 +118,4 @@ public override void CopyFrom(ICopyable obj) Value = o.Value; } } -} \ No newline at end of file +}