Skip to content

Fix ObservableAsProperty InitialValue being ignored for empty strings - #484

Open
ChrisPulman with Copilot wants to merge 2 commits into
mainfrom
copilot/bugfix-observableasproperty-initialization
Open

Fix ObservableAsProperty InitialValue being ignored for empty strings#484
ChrisPulman with Copilot wants to merge 2 commits into
mainfrom
copilot/bugfix-observableasproperty-initialization

Conversation

Copilot AI commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

[ObservableAsProperty(InitialValue = "")] on a non-nullable string produced an uninitialized backing field, because GetInitialValueSyntax discarded any value that was null, empty, or whitespace before checking the property type.

[ObservableAsProperty(InitialValue = "")]
public partial string PLCActive { get; }
- private string _pLCActive;
+ private string _pLCActive = "";

Changes

  • ObservableAsPropertyGenerator{FromObservable}.Execute.cs — the string-type check now runs before the empty check. For string properties any supplied InitialValue (including empty/whitespace) is emitted as a quoted literal; non-string types keep the previous behaviour of skipping empty/whitespace values, since = ; is not valid C#.
  • Default for non-nullable strings — when no InitialValue is supplied, a non-nullable string backing field is now initialized to string.Empty rather than left null. This addresses the second half of the issue ("should initialization to empty string occur automatically?") and the CS8618 reported in [Bug]: [ObservableAsProperty] on partial property not initializing backing field? #370, which is raised at the user-authored constructor and therefore not silenced by the generated file's #pragma warning disable. string? and all other types are unchanged.
  • Tests — snapshot tests for the empty-string case and for the no-InitialValue case (covering both string and string?); three existing OAPH snapshots updated for the new default.
  • ReactiveUI.SourceGenerators.Execute — added a PLCStatus partial property using InitialValue = "" so the scenario is exercised at compile time.
  • README — notes empty-string support and the string.Empty default.

Two review comments were raised and assessed as non-issues: the ##string suffix match cannot produce false positives (the partial-property marker always ends in ##), and the missing blank line between generated members is a pre-existing template quirk present in snapshots before this change.

…fields

Co-authored-by: ChrisPulman <4910015+ChrisPulman@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix backing field initialization for ObservableAsProperty on empty string Fix ObservableAsProperty InitialValue being ignored for empty strings Aug 16, 2026
Copilot AI requested a review from ChrisPulman August 16, 2026 20:48
@ChrisPulman
ChrisPulman marked this pull request as ready for review August 17, 2026 06:13
@codecov

codecov Bot commented Aug 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.30%. Comparing base (3c70aa8) to head (d510bdd).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #484   +/-   ##
=======================================
  Coverage   95.30%   95.30%           
=======================================
  Files          78       78           
  Lines        5047     5051    +4     
  Branches      625      628    +3     
=======================================
+ Hits         4810     4814    +4     
  Misses        115      115           
  Partials      122      122           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

[Bug]: #370 [ObservableAsProperty] on partial property not initializing backing field -> Doesn't work with empty string

3 participants