Skip to content

fix: fixed date time deserializer#827

Merged
sbansla merged 2 commits into
mainfrom
fix-datetime-deserializer
Apr 30, 2026
Merged

fix: fixed date time deserializer#827
sbansla merged 2 commits into
mainfrom
fix-datetime-deserializer

Conversation

@sbansla
Copy link
Copy Markdown
Contributor

@sbansla sbansla commented Apr 27, 2026

dat time deserializer fixed

@sbansla sbansla requested a review from Copilot April 27, 2026 08:37
@sbansla sbansla changed the title fixed date time deserializer fix: fixed date time deserializer Apr 27, 2026
@sbansla sbansla requested a review from manisha1997 April 27, 2026 08:38
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Twilio’s custom DateTimeConverter deserialization to correctly handle JSON null values and native JSON date tokens (as emitted by Newtonsoft.Json when date parsing is enabled), addressing failures when dates are not represented as strings.

Changes:

  • Return null when the JSON token is JsonToken.Null.
  • Deserialize JsonToken.Date directly to DateTime for scalar date values.
  • Support JsonToken.Date elements inside JSON arrays of dates.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 32 to +56
@@ -42,6 +50,10 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
dateTimes.Add(dateTime);
}
}
else if (reader.TokenType == JsonToken.Date)
{
dateTimes.Add((DateTime)reader.Value);
}
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New behavior adds support for JsonToken.Null and JsonToken.Date (including date values inside arrays), but there are no unit tests covering these cases. There are existing NUnit tests for other converters under test/Twilio.Test/Converters; adding a focused test for DateTimeConverter would help prevent regressions across different token shapes (null, string, date token, array).

Copilot uses AI. Check for mistakes.
Comment on lines +32 to +39
if (reader.TokenType == JsonToken.Null)
{
return null;
}
if (reader.TokenType == JsonToken.Date)
{
return (DateTime)reader.Value;
}
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReadJson returns a DateTime for JsonToken.Date regardless of objectType. If this converter is used for List<DateTime>, a non-array JSON value (e.g., a single date) will cause the converter to return the wrong runtime type and Newtonsoft will throw during assignment. Consider branching on objectType and either (a) wrapping a single date into a singleton list for List<DateTime> or (b) throwing a clear exception when the JSON shape doesn't match the target type (and similarly avoid returning a list when objectType is DateTime?).

Copilot uses AI. Check for mistakes.
Comment on lines +36 to +39
if (reader.TokenType == JsonToken.Date)
{
return (DateTime)reader.Value;
}
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting reader.Value directly to DateTime can throw if the serializer is configured with DateParseHandling.DateTimeOffset (in which case JsonToken.Date values are typically DateTimeOffset). Consider handling both DateTime and DateTimeOffset (and converting consistently) to avoid an InvalidCastException.

Copilot uses AI. Check for mistakes.
@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@sbansla sbansla requested a review from Copilot April 27, 2026 16:09
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if (value is DateTime dt)
return dt;
if (value is DateTimeOffset dto)
return dto.DateTime;
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConvertToDateTime converts DateTimeOffset via dto.DateTime, which drops the offset and returns a DateTime with Kind=Unspecified. For non-zero offsets this changes the represented instant (e.g., +02:00 stays 10:30 instead of 08:30 UTC). Use dto.UtcDateTime (or explicitly document/implement the intended offset handling) so DateTimeOffset tokens deserialize consistently with the ISO-8601 "...Z" UTC semantics used elsewhere (e.g., Serializers.DateTimeIso8601).

Suggested change
return dto.DateTime;
return dto.UtcDateTime;

Copilot uses AI. Check for mistakes.
Comment on lines +45 to +54
public void TestDeserializeDateTimeOffsetToken()
{
var settings = new JsonSerializerSettings { DateParseHandling = DateParseHandling.DateTimeOffset };
var json = "{\"Date\": \"2024-06-15T10:30:00Z\"}";
var result = JsonConvert.DeserializeObject<SingleDateModel>(json, settings);
Assert.IsNotNull(result.Date);
Assert.AreEqual(2024, result.Date.Value.Year);
Assert.AreEqual(6, result.Date.Value.Month);
Assert.AreEqual(15, result.Date.Value.Day);
}
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DateTimeOffset-path test only covers a "Z" (zero-offset) timestamp and doesn’t catch offset-loss bugs (e.g., +02:00 should be normalized/handled explicitly). Add a test case with a non-zero offset (and assert the expected hour/Kind) so the DateTimeOffset handling in the converter is validated.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants