Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix STJ support for System.Reflection.NullabilityInfoContext.IsSupported = false #102852

Merged

Conversation

eiriktsarpalis
Copy link
Member

Fix #102848

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

@jkotas
Copy link
Member

jkotas commented May 30, 2024

I do not think that this is the right fix.

I think the right fix is to throw exception when RespectNullableAnnotations = true and the nullability annotations are trimmed.

@MichalStrehovsky
Copy link
Member

I think I know the answer, but just to double check - when using the source generator, this runtime reflection doesn't happen, right? (I.e. wasm is only running into this because it's using trimming in a way that we do not recommend to anyone.)

@eiriktsarpalis
Copy link
Member Author

I do not think that this is the right fix.

I think the right fix is to throw exception when RespectNullableAnnotations = true and the nullability annotations are trimmed.

It's something I considered, although ultimately I settled on the conclusion that it's common for reflection to misreport metadata in the context of trimmed applications. If anything, the current throwing behavior of NullabilityInfoContext is inconsistent with how the rest of reflection works in trimmed apps.

I think I know the answer, but just to double check - when using the source generator, this runtime reflection doesn't happen, right?

That is correct, which is why I'm not particularly concerned about this corner case. There will have been warnings.

@MichalStrehovsky
Copy link
Member

It's something I considered, although ultimately I settled on the conclusion that it's common for reflection to misreport metadata in the context of trimmed applications. If anything, the current throwing behavior of NullabilityInfoContext is inconsistent with how the rest of reflection works in trimmed apps.

The current NullabilityInfoContaxt feature switch is simply a bad design. We knew that it would be bad when it was introduced (#55860 (comment)). I was not involved in any of the discussions besides what I posted so I don't know what tradeoff forced going with it anyway, despite being bad.

That is correct, which is why I'm not particularly concerned about this corner case. There will have been warnings.

There will be no warnings: Blazor WASM that hit this turns them off (that's why people are not guided towards using the source generator in the first place). If we are choosing between an exception and silent different behavior, we should choose the exception be default and only very exceptionally silent different behaviors.

@eiriktsarpalis
Copy link
Member Author

OK, I've pushed a change that reinstates the exception when the user has opted into enforcement.

…ion/Metadata/DefaultJsonTypeInfoResolver.Helpers.cs
@eiriktsarpalis eiriktsarpalis merged commit 9a3a278 into dotnet:main May 30, 2024
83 checks passed
@eiriktsarpalis
Copy link
Member Author

/backport to release/9.0-preview5

Copy link
Contributor

Started backporting to release/9.0-preview5: https://github.com/dotnet/runtime/actions/runs/9302780301

Ruihan-Yin pushed a commit to Ruihan-Yin/runtime that referenced this pull request May 30, 2024
…ted = false (dotnet#102852)

* Fix STJ support for System.Reflection.NullabilityInfoContext.IsSupported = false

* Update src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs

* Bring back exception in cases where RespectNullability has been turned on.

* Update src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Helpers.cs

---------

Co-authored-by: David Cantú <dacantu@microsoft.com>
@github-actions github-actions bot locked and limited conversation to collaborators Jun 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exception in BlazorWasm applications due to NullabilityInfoContext usage
5 participants