Conversation
WalkthroughChangeObjectType now converts non-string IFormattable inputs by first formatting them to string (with provided format and formatProvider) and attempting string-based conversion; adds direct DateTime → DateTimeOffset handling; removes the previous unconditional ToString fallback path. Two unit tests added for DateTime→DateTimeOffset conversions. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
324f8b3 to
ffae147
Compare
|
@CodeRabbit resume |
ffae147 to
a3ede2b
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/NLog/Config/PropertyTypeConverter.cs`:
- Around line 161-177: The fallback path in PropertyTypeConverter lets a raw
DateTime fall through to System.Convert.ChangeType causing InvalidCastException
when propertyType is DateTimeOffset; before the final Convert.ChangeType call,
add a check: if propertyValue is DateTime dt and propertyType ==
typeof(DateTimeOffset) (or nullable DateTimeOffset), convert by creating a
DateTimeOffset from dt (preserving Kind/UTC behavior as needed) and return it;
keep this check before calling Convert.ChangeType and after the existing
IConvertible/TryConvertToType branches so TryConvertToType still runs first.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: cd4c3854-17f8-4237-a5b0-b6bff29af74c
📒 Files selected for processing (2)
src/NLog/Config/PropertyTypeConverter.cstests/NLog.UnitTests/Config/PropertyTypeConverterTests.cs
✅ Actions performedReviews resumed. |
a3ede2b to
1b50176
Compare
|
@CodeRabbit Full Review |
✅ Actions performedFull review triggered. |
1b50176 to
4d07198
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/NLog.UnitTests/Config/PropertyTypeConverterTests.cs (1)
226-236: Cover the rawDateTime -> DateTimeOffsetfallback as well.This test only exercises the formatted
IFormattablepath. The new special-case on Line 164 insrc/NLog/Config/PropertyTypeConverter.csis a separate branch, so adding a no-format case here would keep that regression fix protected too.➕ Suggested test coverage
[Fact] public void Convert_DateTimeToDateTimeOffset() { // Act var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); var result = _sut.Convert(dateTime, typeof(DateTimeOffset), "o", null); // Assert var resultTyped = Assert.IsType<DateTimeOffset>(result); Assert.Equal(dateTime, resultTyped.DateTime); } + +[Fact] +public void Convert_DateTimeToDateTimeOffsetWithoutFormat() +{ + // Act + var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + var result = _sut.Convert(dateTime, typeof(DateTimeOffset), null, null); + + // Assert + var resultTyped = Assert.IsType<DateTimeOffset>(result); + Assert.Equal(dateTime, resultTyped.DateTime); +}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/NLog.UnitTests/Config/PropertyTypeConverterTests.cs` around lines 226 - 236, Add a second assertion in the Convert_DateTimeToDateTimeOffset test to exercise the non-formattable fallback path by calling _sut.Convert with the same DateTime and target type typeof(DateTimeOffset) but with format set to null (or an empty string), then assert the result is a DateTimeOffset and that its DateTime equals the original dateTime; this ensures the raw DateTime->DateTimeOffset branch in PropertyTypeConverter.Convert is covered in addition to the formatted IFormattable path.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@tests/NLog.UnitTests/Config/PropertyTypeConverterTests.cs`:
- Around line 226-236: Add a second assertion in the
Convert_DateTimeToDateTimeOffset test to exercise the non-formattable fallback
path by calling _sut.Convert with the same DateTime and target type
typeof(DateTimeOffset) but with format set to null (or an empty string), then
assert the result is a DateTimeOffset and that its DateTime equals the original
dateTime; this ensures the raw DateTime->DateTimeOffset branch in
PropertyTypeConverter.Convert is covered in addition to the formatted
IFormattable path.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: ab337b15-a7a4-4786-969a-81edcc475c37
📒 Files selected for processing (2)
src/NLog/Config/PropertyTypeConverter.cstests/NLog.UnitTests/Config/PropertyTypeConverterTests.cs
1c2b32e to
9ec1974
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/NLog.UnitTests/Config/PropertyTypeConverterTests.cs (1)
226-236: Add coverage for the no-format branch.This test only exercises the formatted
IFormattablepath. The new rawDateTime -> DateTimeOffsetfallback on Lines 162-163 insrc/NLog/Config/PropertyTypeConverter.csis still unpinned.🧪 Suggested companion test
[Fact] public void Convert_DateTimeToDateTimeOffset() { // Act var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); var result = _sut.Convert(dateTime, typeof(DateTimeOffset), "o", null); // Assert var resultTyped = Assert.IsType<DateTimeOffset>(result); Assert.Equal(dateTime, resultTyped.DateTime); } + + [Fact] + public void Convert_DateTimeToDateTimeOffsetWithoutFormat() + { + // Act + var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + var result = _sut.Convert(dateTime, typeof(DateTimeOffset), null, null); + + // Assert + var resultTyped = Assert.IsType<DateTimeOffset>(result); + Assert.Equal(dateTime, resultTyped.DateTime); + }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/NLog.UnitTests/Config/PropertyTypeConverterTests.cs` around lines 226 - 236, Add a companion unit test that exercises the "no-format" fallback in PropertyTypeConverter.Convert by calling _sut.Convert with a DateTime (same sample: new DateTime(1970,1,1,..., DateTimeKind.Utc)) and passing null for the format parameter (third arg) instead of "o"; assert the returned object is a DateTimeOffset (use Assert.IsType<DateTimeOffset>) and that its DateTime equals the original DateTime. Name the test clearly (e.g., Convert_DateTimeToDateTimeOffset_NoFormat) and mirror the existing assertions used in Convert_DateTimeToDateTimeOffset.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@tests/NLog.UnitTests/Config/PropertyTypeConverterTests.cs`:
- Around line 226-236: Add a companion unit test that exercises the "no-format"
fallback in PropertyTypeConverter.Convert by calling _sut.Convert with a
DateTime (same sample: new DateTime(1970,1,1,..., DateTimeKind.Utc)) and passing
null for the format parameter (third arg) instead of "o"; assert the returned
object is a DateTimeOffset (use Assert.IsType<DateTimeOffset>) and that its
DateTime equals the original DateTime. Name the test clearly (e.g.,
Convert_DateTimeToDateTimeOffset_NoFormat) and mirror the existing assertions
used in Convert_DateTimeToDateTimeOffset.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 9988e79e-c33b-47e3-9428-8753a555b01d
📒 Files selected for processing (2)
src/NLog/Config/PropertyTypeConverter.cstests/NLog.UnitTests/Config/PropertyTypeConverterTests.cs
9ec1974 to
82ec559
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/NLog.UnitTests/Config/PropertyTypeConverterTests.cs (1)
226-236: Consider a more explicit assertion for clarity.The assertion
Assert.Equal(dateTime.Date, resultTyped.DateTime)works but is semantically confusing—it compares a date-at-midnight with the full DateTime from DateTimeOffset. Since the "yyyy-MM-dd" format intentionally discards time, asserting against the expected concrete value makes the intent clearer:- Assert.Equal(dateTime.Date, resultTyped.DateTime); + Assert.Equal(new DateTime(1970, 1, 1), resultTyped.DateTime);Alternatively, consider adding an offset assertion if that's relevant to the bug fix (e.g.,
Assert.Equal(TimeSpan.Zero, resultTyped.Offset)if UTC is expected).🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/NLog.UnitTests/Config/PropertyTypeConverterTests.cs` around lines 226 - 236, The test Convert_DateTimeToDateTimeOffset is asserting dateTime.Date against resultTyped.DateTime which is confusing; update the assertion to compare against an explicit expected DateTimeOffset constructed from dateTime.Date (or compare resultTyped.DateTime to dateTime.Date and resultTyped.Offset to the expected offset) so the intent of the "yyyy-MM-dd" format is clear—modify the assertions in the Convert_DateTimeToDateTimeOffset test that inspect _sut.Convert(...) and the resultTyped variable to use an explicit expected DateTimeOffset (and optionally assert resultTyped.Offset, e.g., TimeSpan.Zero if UTC is expected).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@tests/NLog.UnitTests/Config/PropertyTypeConverterTests.cs`:
- Around line 226-236: The test Convert_DateTimeToDateTimeOffset is asserting
dateTime.Date against resultTyped.DateTime which is confusing; update the
assertion to compare against an explicit expected DateTimeOffset constructed
from dateTime.Date (or compare resultTyped.DateTime to dateTime.Date and
resultTyped.Offset to the expected offset) so the intent of the "yyyy-MM-dd"
format is clear—modify the assertions in the Convert_DateTimeToDateTimeOffset
test that inspect _sut.Convert(...) and the resultTyped variable to use an
explicit expected DateTimeOffset (and optionally assert resultTyped.Offset,
e.g., TimeSpan.Zero if UTC is expected).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 6f57ef57-39a9-4b78-a112-89eaff3d3d91
📒 Files selected for processing (2)
src/NLog/Config/PropertyTypeConverter.cstests/NLog.UnitTests/Config/PropertyTypeConverterTests.cs
🚧 Files skipped from review as they are similar to previous changes (1)
- src/NLog/Config/PropertyTypeConverter.cs
82ec559 to
1cdec99
Compare
|
@CodeRabbit Full Review |
✅ Actions performedFull review triggered. |
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/NLog.UnitTests/Config/PropertyTypeConverterTests.cs (1)
238-248: Consider adding an assertion for offset preservation.The test validates that the
DateTimevalue is preserved, but doesn't verify that theDateTimeKind.Utcis correctly converted to a zero UTC offset.DateTime.Equalscompares onlyTicks, notKind.🔧 Suggested enhancement
// Assert var resultTyped = Assert.IsType<DateTimeOffset>(result); Assert.Equal(dateTime, resultTyped.DateTime); + Assert.Equal(TimeSpan.Zero, resultTyped.Offset); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/NLog.UnitTests/Config/PropertyTypeConverterTests.cs` around lines 238 - 248, The test Convert_DateTimeToDateTimeOffset_NoFormat should also assert that the UTC kind is preserved as a zero offset; after calling _sut.Convert(dateTime, typeof(DateTimeOffset), null, null) and casting to resultTyped (DateTimeOffset), add an assertion that resultTyped.Offset == TimeSpan.Zero (or that resultTyped.ToUniversalTime().Ticks equals dateTime.ToUniversalTime().Ticks) so the conversion from DateTime with DateTimeKind.Utc yields a DateTimeOffset with zero UTC offset.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@tests/NLog.UnitTests/Config/PropertyTypeConverterTests.cs`:
- Around line 238-248: The test Convert_DateTimeToDateTimeOffset_NoFormat should
also assert that the UTC kind is preserved as a zero offset; after calling
_sut.Convert(dateTime, typeof(DateTimeOffset), null, null) and casting to
resultTyped (DateTimeOffset), add an assertion that resultTyped.Offset ==
TimeSpan.Zero (or that resultTyped.ToUniversalTime().Ticks equals
dateTime.ToUniversalTime().Ticks) so the conversion from DateTime with
DateTimeKind.Utc yields a DateTimeOffset with zero UTC offset.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 0dddfa5f-0c2d-401b-ad8b-8d3245b76459
📒 Files selected for processing (2)
src/NLog/Config/PropertyTypeConverter.cstests/NLog.UnitTests/Config/PropertyTypeConverterTests.cs
|



Trying to resolve #6115 (Prioritize Format-specifier, but also support conversion without having specified Format)