Skip to content

Fix/issue 441#471

Open
gyurebalint wants to merge 5 commits intoserilog:devfrom
gyurebalint:fix/issue-441
Open

Fix/issue 441#471
gyurebalint wants to merge 5 commits intoserilog:devfrom
gyurebalint:fix/issue-441

Conversation

@gyurebalint
Copy link
Copy Markdown
Contributor

Fixes #441

Problem

When a configuration method has a params array parameter (e.g. params string[] ids) and the Args section is omitted entirely from config, the library fails to invoke the method at startup with:
System.ArgumentException: Object of type 'System.DBNull' cannot be converted to type 'System.String[]'.

This happens because:

  1. HasImplicitValueWhenNotSpecified does not recognise params parameters as optional — so SelectConfigurationMethod rejects the method entirely when no args are supplied
  2. Even if the method were selected, GetImplicitValueForNotSpecifiedKey falls through to return parameter.DefaultValue which is DBNull for params parameters on .NET 10 — causing a crash at invocation

Changes

ConfigurationReader.cs

  • Added ParamArrayAttribute check to HasImplicitValueWhenNotSpecified so params parameters are treated as optional during method selection
  • Added ParamArrayAttribute check to GetImplicitValueForNotSpecifiedKey to return an empty array of the correct element type instead of DBNull
  • Made GetImplicitValueForNotSpecifiedKey
    internal to allow direct unit testing

ConfigurationReaderTests.cs

  • Added test asserting that a method with a params string[] parameter is selected when no args are supplied
  • Added test asserting that the implicit value for a params string[] parameter is an empty array, not DBNull

Root cause

In .NET 10, params parameters report HasDefaultValue = true but DefaultValue is DBNull — not a valid value for an array type. The fix intercepts params parameters before falling through to return parameter.DefaultValue.

@gyurebalint
Copy link
Copy Markdown
Contributor Author

Hi everyone!
Note: I changed GetImplicitValueForNotSpecifiedKey to internal to allow for isolated unit testing of the params logic.
If you'd prefer to keep it private and have this tested strictly through the public API, let me know and I'll adjust the tests.
Any feedback is appreciated.

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.

params arguments should be regarded as optional

1 participant