Fix: Improve nullable operator handling for dictionary keys#523
Merged
axunonb merged 2 commits intoaxuno:mainfrom Mar 22, 2026
Merged
Fix: Improve nullable operator handling for dictionary keys#523axunonb merged 2 commits intoaxuno:mainfrom
axunonb merged 2 commits intoaxuno:mainfrom
Conversation
- 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
- 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.
There was a problem hiding this comment.
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
DictionarySourceto detect dictionary kinds (non-generic, generic/dynamic, read-only) and to returnnullfor missing keys when the nullable operator applies. - Updates
Source.HasNullableOperatorto only consider nullable operators on the current selector or any selector before it (up toISelectorInfo.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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DictionarySourceto detect dictionary types and handle missing keys gracefully, when the nullable operator is used in the selector of a format string.Source.HasNullableOperatorlogic: Returns true, if the currentSelectoror anySelectorbefore contains the nullable operator.Resolves #522