Skip to content

Fix: Improve nullable operator handling for dictionary keys#523

Merged
axunonb merged 2 commits intoaxuno:mainfrom
axunonb:pr/issue-522
Mar 22, 2026
Merged

Fix: Improve nullable operator handling for dictionary keys#523
axunonb merged 2 commits intoaxuno:mainfrom
axunonb:pr/issue-522

Conversation

@axunonb
Copy link
Copy Markdown
Member

@axunonb axunonb commented Mar 19, 2026

  • Enhances SmartFormat's support for nullable operators ('?') in dictionary formatting.
  • Refactors DictionarySource to detect dictionary types and handle missing keys gracefully, when the nullable operator is used in the selector of a format string.
  • Adds tests to verify correct behavior for missing keys and improper nullable operator placement.
  • Updates Source.HasNullableOperator logic: Returns true, if the current Selector or any Selector before contains the nullable operator.

Resolves #522

- Enhances SmartFormat's support for nullable operators ('?') in dictionary formatting.
- Refactors `DictionarySource` to detect dictionary types and handle missing keys gracefully, when the nullable operator is used in the selector of a format string.
- Adds tests to verify correct behavior for missing keys and improper nullable operator placement.
- Updates `Source.HasNullableOperator` logic: Returns true, if the current `Selector` or any `Selector` before contains the nullable operator.

Resolves axuno#522
@axunonb axunonb requested a review from Copilot March 19, 2026 16:25

This comment was marked as resolved.

- Introduced a thread-safe cache to IsIReadOnlyDictionary for improved performance and reduced allocations.
- Added tests for nullable operator with enum-keyed dictionaries to ensure missing keys return empty values.
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 improves SmartFormat’s handling of the nullable operator (?) when formatting dictionary-like sources, so missing dictionary keys can be treated as null (and therefore formatted as an empty string) when ? is used appropriately in the selector chain.

Changes:

  • Refactors DictionarySource to detect dictionary kinds (non-generic, generic/dynamic, read-only) and to return null for missing keys when the nullable operator applies.
  • Updates Source.HasNullableOperator to only consider nullable operators on the current selector or any selector before it (up to ISelectorInfo.SelectorIndex).
  • Adds tests covering missing keys (string + enum key dictionaries) and a negative test for incorrect nullable-operator placement.

Reviewed changes

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

File Description
src/SmartFormat/Extensions/DictionarySource.cs Adds dictionary type detection and nullable-operator-based missing-key behavior; improves IReadOnlyDictionary type-check caching.
src/SmartFormat/Core/Extensions/Source.cs Refines nullable-operator detection scope (only selectors up to the currently evaluated selector).
src/SmartFormat.Tests/Extensions/DictionarySourceTests.cs Adds regression tests for missing dictionary keys with nullable operator, including enum keys and incorrect operator placement.

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

@axunonb axunonb changed the title Improve nullable operator handling for dictionary keys Fix: Improve nullable operator handling for dictionary keys Mar 22, 2026
@axunonb axunonb merged commit 523705a into axuno:main Mar 22, 2026
7 checks passed
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.

DictionarySource throws on missing key instead of returning null

2 participants