Repository navigation
Fix STJ unsafe accessor detection with polyfilled attributes - #136463
Merged
eiriktsarpalis merged 1 commit intoOct 26, 2026
Merged
eiriktsarpalis merged 1 commit into
eiriktsarpalis merged 1 commit into
Conversation
Require core-library definitions for unsafe accessor capability markers in the System.Text.Json source generator. Co-authored-by: Copilot App <[email protected]>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-text-json |
AArnott
approved these changes
Oct 26, 2026
Member
Author
|
/backport to release/11.0 |
Contributor
|
Started backporting to |
2 of 4 tasks
ViveliDuCh
pushed a commit
that referenced
this pull request
Nov 14, 2026
…ibutes (#136473) Backport of #136463 to release/11.0 /cc @eiriktsarpalis ## Customer Impact - [x] Customer reported - [ ] Found internally In .NET 11, the STJ source generator added support for inaccessible members (supported historically in the reflection-based serializer via the `JsonIncludeAttribute` opt in) by way of unsafe accessors. The generator checks for the availability of unsafe accessors in a target framework by looking up the unsafe accessor attribute itself. It appears however that the popular polyfill library PolySharp (used among others by BenchmarkDotNet) is creating polyfills for targets that do not natively support unsafe accessors, resulting in code that that fails at runtime. This PR strengthens the unsafe accessor availability heuristic against such polyfills. ## Regression - [x] Yes - [ ] No ## Testing Added testing validating the scenario. ## Risk Low. Makes a straightforward update to the platform detection heuristic. Co-authored-by: Eirik Tsarpalis <[email protected]> Co-authored-by: Copilot App <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The System.Text.Json source generator treats polyfilled
UnsafeAccessorAttributedefinitions as evidence of runtime support. Referencing an assembly such as BenchmarkDotNet can therefore cause a .NET Framework project to emit unsupported unsafe accessors, even without using any APIs from that assembly. A polyfilledOverloadResolutionPriorityAttributecan similarly cause generic unsafe accessors to be emitted on .NET 8.Require both capability markers to come from the target compilation's core library, identified by the assembly defining
System.Object. Other type lookups are unchanged. Adds one generator unit-test theory covering source and referenced polyfills with public and internal visibility.Validation:
.\dotnet.cmd build src\libraries\System.Text.Json\tests\System.Text.Json.SourceGeneration.Unit.Tests\System.Text.Json.SourceGeneration.Roslyn4.4.Unit.Tests.csproj -t:Test -f net481 -p:RuntimeConfiguration=Release '-p:XunitMethodName=*UnsafeAccessorPolyfill*'passed all four cases. Standalone public API checks also passed for net472 and .NET 8 with source/referenced and public/internal marker polyfills.Resolves #136457
Note
This pull request was generated with GitHub Copilot.