Skip to content

fix: use type default for optional DateTime constructor params (#954)#968

Closed
leno23 wants to merge 1 commit into
MapsterMapper:developmentfrom
leno23:fix/optional-datetime-ctor-default-954-dev
Closed

fix: use type default for optional DateTime constructor params (#954)#968
leno23 wants to merge 1 commit into
MapsterMapper:developmentfrom
leno23:fix/optional-datetime-ctor-default-954-dev

Conversation

@leno23
Copy link
Copy Markdown

@leno23 leno23 commented May 25, 2026

Summary

  • Extract CreateOptionalParameterDefault for optional constructor parameters.
  • When DefaultValue is null or DBNull, emit destinationType.CreateDefault() instead of Expression.Constant(null, valueType).
  • Adds regression test mapping a record with DateTime to a class with an optional DateTime constructor parameter.

Fixes #954

Test plan

  • Added WhenMappingRecordWithOptionalDateTimeConstructor regression test
  • CI green on development

…erMapper#954)

When an optional constructor parameter has a null/DBNull default value,
emit destinationType.CreateDefault() instead of Expression.Constant(null).

Co-authored-by: Cursor <cursoragent@cursor.com>
@DocSvartz
Copy link
Copy Markdown
Contributor

Hi, @leno23 Thanks, but it is fixed. and in net48 and perhaps in netcore version 6 or below, instead of the default value being null, the default value actually contains a FormatException.

static Expression CreateOptionalParameterDefault(ParameterInfo parameterInfo, Type destinationType)
{
var defaultValue = parameterInfo.DefaultValue;
if (defaultValue == null || defaultValue is DBNull)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@leno23 defaultValue is DBNull Please add a test for this case if it applies. There is already a test for the general case.

@leno23
Copy link
Copy Markdown
Author

leno23 commented May 27, 2026

Thanks @DocSvartz — confirmed the FormatException guard for optional DateTime ctor params is already on development (BaseClassAdapter, #if NETSTANDARD2_0). Closing as duplicate of the upstream fix.

Appreciate the note about net48 / net6-or-below default-value behavior vs null.

@leno23
Copy link
Copy Markdown
Author

leno23 commented May 27, 2026

Superseded by fix already on development branch.

@leno23 leno23 closed this May 27, 2026
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.

2 participants