ReactiveUI icon indicating copy to clipboard operation
ReactiveUI copied to clipboard

[Bug]: Build fails when using `ReactiveWindow` with `System.Text.Json` source generation

Open database64128 opened this issue 3 years ago • 5 comments

Describe the bug 🐞

~~When a class with [Reactive] decorated properties is used with System.Text.Json source generation, build fails with the following errors:~~

Update: See https://github.com/reactiveui/ReactiveUI/issues/3255#issuecomment-1117245316 instead.

CS0534: 'SettingsJsonSerializerContext' does not implement inherited abstract member 'JsonSerializerContext.GeneratedSerializerOptions.get'
CS0534: 'SettingsJsonSerializerContext' does not implement inherited abstract member 'JsonSerializerContext.GetTypeInfo(Type)'

Step to reproduce

  1. Create a WPF project targeting .NET 6, add reference to RxUI and create MainWindowViewModel.
  2. Build succeeds.
  3. Replace WPF's Window with RxUI's ReactiveWindow.
  4. Build fails.
  5. Add package reference to STJ.
  6. Build succeeds.

Reproduction repository

https://github.com/database64128/WPFRxUISTJSourceGenerationMinimalReproduction

Expected behavior

Build succeeds.

ReactiveUI Version

18.0.10

database64128 avatar May 01 '22 08:05 database64128

I seen this when your context isn't a partial class and decorated correctly. Check also the output window to see if the build failed.

There's also a strong argument not to have view models be directly serialisable.

glennawatson avatar May 01 '22 08:05 glennawatson

I seen this when your context isn't a partial class and decorated correctly.

I believe it is correctly written and decorated. The whole change is in this commit: https://github.com/database64128/youtube-dl-wpf/commit/51947e6775689b1101514fd818d371a95816f454.

There's also a strong argument not to have view models be directly serialisable.

In this case it's not exactly a view model. The Settings class stores app settings. Inheriting ReactiveObject and decorating its properties with [Reactive] seems to be the easiest way to make certain parts of the app respond to setting changes. Maybe I should use a separate class for JSON serialization and deserialization?

database64128 avatar May 01 '22 08:05 database64128

Yeah, Can be best if you do that. I'd be concerned with a fody and source generator interop as well.

glennawatson avatar May 01 '22 08:05 glennawatson

Turns out it was unrelated to Fody. For some reason it can be fixed by referencing the STJ package: https://github.com/database64128/youtube-dl-wpf/commit/f201304be75674630eb2fbea1419cd1f37dacff4, even though my project was already targeting .NET 6.

database64128 avatar May 03 '22 10:05 database64128

dotnet/runtime#68810

Turns out the issue was caused by ReactiveWindow. I made a minimal reproduction repo: https://github.com/database64128/WPFRxUISTJSourceGenerationMinimalReproduction. There are 4 commits:

  1. database64128/WPFRxUISTJSourceGenerationMinimalReproduction@1cfa42a81dc3a17de085f10bafccf9f24dbdb8b4: Fresh WPF app with STJ source generation: no issues
  2. database64128/WPFRxUISTJSourceGenerationMinimalReproduction@561c4c16469558c1d32aa55ed3ae23f4c210ca34: Add RxUI and use ReactiveObject: no issues
  3. database64128/WPFRxUISTJSourceGenerationMinimalReproduction@743bea978e39695e12d07bb08a5f3875ec38ff75: Switch from WPF's Window to RxUI's ReactiveWindow: build fails.
  4. database64128/WPFRxUISTJSourceGenerationMinimalReproduction@b6705875106c04a5231243ac99da6fd05fa5d161: Add package reference to System.Text.Json: build succeeds.
GitHub
Minimal reproduction of a bug that can only be triggered when using RxWindow together with STJ source generation in a WPF app. - GitHub - database64128/WPFRxUISTJSourceGenerationMinimalReproduction...

database64128 avatar May 04 '22 12:05 database64128