Skip to content

[Bug]: Named parameter ReadOnly of ObservableAsProperty attribute gets ignored when used on partial property #185

@DarkCloud14

Description

@DarkCloud14

Describe the bug 🐞

It looks like the named parameter ReadOnly of the ObservableAsProperty attribute is ignored when you use it at a partial property.

When you explicitly set ReadOnly to true the generated OAPH field isn't readonly and the same is true if you don't supply the parameter at all, this then also generates an OAPH field which isn't readonly and so in this regard is behaving different to specifying OAP attribute at a field without ReadOnly parameter which results in a readonly OAPH field.

I discovered that issue when looking into another issue which I reported here

Step to reproduce

  1. Create a new .NET9 class library project
  2. Install the ReactiveUI and ReactiveUI.SourceGenerators packages
  3. Write a simple partial class which is a subclass of ReactiveObject
  4. Add a partial get-only property with [ObservableAsProperty] attribute and a field with [ObservableAsProperty] attribute
  5. Add another partial get-only property with [ObservableAsProperty(ReadOnly = true)] attribute and a field with [ObservableAsProperty(ReadOnly = true)] attribute
public partial class TestClass : ReactiveObject
{
    [ObservableAsProperty]
    private string _observableTestFieldNotSet;

    [ObservableAsProperty(ReadOnly = true)]
    private string _observableTestFieldReadOnlyTrue;

    [ObservableAsProperty]
    public partial string ObservableTestPropertyNotSet { get; }

    [ObservableAsProperty(ReadOnly = true)]
    public partial string ObservableTestPropertyReadOnlyTrue { get; }
}
  1. Have a look at the generated source code and see that both helper fields of the partial properties ObservableTestPropertyNotSet and ObservableTestPropertyReadOnlyTrue are not readonly. When looking at the helper fields generated for the fields _observableTestFieldNotSet and _observableTestFieldReadOnlyTrue this is the case meaning both are readonly.

Reproduction repository

https://github.com/DarkCloud14/ReactiveUI_SourceGen_ObservableAsProperty_ReadOnly_Issue

Expected behavior

I would expect that only if you explicitly set the named parameter ReadOnly of ObservableAsProperty attribute to false at a partial property the generated OAPH field isn't readonly and in the other two cases (not supplied at all or explicitly set to true) it is readonly.

The reason I would expect the above behaviour is because that's the result if you do that at a field instead of a partial property.

Or is the current behaviour expected and I'm missing or missunderstanding something?

Screenshots 🖼️

No response

IDE

Rider macOS

Operating system

macOS Sequoia 15.3.1

Version

No response

Device

No response

ReactiveUI Version

ReactiveUI 20.1.63, ReactiveUI.SourceGenerators 2.1.1

Additional information ℹ️

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions