Skip to content

Conversation

@eiriktsarpalis
Copy link
Member

Fix #102848

@dotnet-policy-service
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
@eiriktsarpalis
Copy link
Member Author

/backport to release/9.0-preview5

@github-actions
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
@jeffhandley
Copy link
Member

/backport to release/8.0-staging

@github-actions github-actions bot unlocked this conversation Aug 31, 2025
@github-actions
Copy link
Contributor

Started backporting to release/8.0-staging: https://github.com/dotnet/runtime/actions/runs/17358950692

@github-actions
Copy link
Contributor

@jeffhandley backporting to "release/8.0-staging" failed, the patch most likely resulted in conflicts:

$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: Fix STJ support for System.Reflection.NullabilityInfoContext.IsSupported = false
Using index info to reconstruct a base tree...
M	src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Helpers.cs
M	src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Helpers.cs
CONFLICT (content): Merge conflict in src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Helpers.cs
Auto-merging src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs
CONFLICT (content): Merge conflict in src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 Fix STJ support for System.Reflection.NullabilityInfoContext.IsSupported = false
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 31, 2025
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

6 participants