Skip to content

Commit

Permalink
Fix when no "SENT-BY" defined, get SentBy will throw exception (ical-…
Browse files Browse the repository at this point in the history
…org#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 <fei.xu@infoflosolutions.com>
  • Loading branch information
idiotsky and Fei Xu authored Dec 15, 2024
1 parent 4ab2415 commit 3c5a596
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
24 changes: 24 additions & 0 deletions Ical.Net.Tests/Calendars/Journal/JOURNAL3.ics
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion Ical.Net.Tests/IcsFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -168,4 +169,4 @@ internal static string ReadStream(string manifestResource)

internal static string LibicalIcalrecurTest => ReadStream("Ical.Net.Tests.contrib.libical.icalrecur_test.out");

}
}
13 changes: 12 additions & 1 deletion Ical.Net.Tests/JournalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,15 @@ public void Journal2()
Assert.That(j.Start, Is.Null);
});
}
}

[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.");
}
}
36 changes: 27 additions & 9 deletions Ical.Net/DataTypes/Organizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the MIT license.
//

#nullable enable
using System;
using System.Diagnostics;
using System.IO;
Expand All @@ -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)
Expand All @@ -27,7 +36,7 @@ public virtual Uri SentBy
}
else
{
Parameters.Set("SENT-BY", (string) null);
Parameters.Remove("SENT-BY");
}
}
}
Expand All @@ -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)
Expand All @@ -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() { }

Expand All @@ -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))
{
Expand Down Expand Up @@ -100,4 +118,4 @@ public override void CopyFrom(ICopyable obj)
Value = o.Value;
}
}
}
}

0 comments on commit 3c5a596

Please sign in to comment.