Skip to content

[release/11.0] Fix STJ unsafe accessor detection with polyfilled attributes - #136473

Merged
ViveliDuCh merged 1 commit into
release/11.0from
backport/pr-136463-to-release/11.0
Nov 14, 2026
Merged

ViveliDuCh merged 1 commit into
release/11.0from
backport/pr-136463-to-release/11.0

Conversation

@github-actions

@github-actions github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Backport of #136463 to release/11.0

/cc @eiriktsarpalis

Customer Impact

  • 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

  • Yes
  • No

Testing

Added testing validating the scenario.

Risk

Low. Makes a straightforward update to the platform detection heuristic.

<!-- -->

The System.Text.Json source generator treats polyfilled
`UnsafeAccessorAttribute` definitions 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 polyfilled
`OverloadResolutionPriorityAttribute` can 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.

Co-authored-by: Copilot App <[email protected]>
@azure-pipelines

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@eiriktsarpalis eiriktsarpalis added the Servicing-consider Issue for next servicing release review label Oct 26, 2026
@eiriktsarpalis eiriktsarpalis self-assigned this Oct 26, 2026
@eiriktsarpalis eiriktsarpalis added Servicing-approved Approved for servicing release and removed Servicing-consider Issue for next servicing release review labels Oct 26, 2026
@eiriktsarpalis

Copy link
Copy Markdown
Member

Approved for December servicing.

@eiriktsarpalis
eiriktsarpalis requested a review from artl93 October 26, 2026 19:15
@eiriktsarpalis

Copy link
Copy Markdown
Member

/ba-g test failure is unrelated.

@artl93 artl93 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved for me too.

@ViveliDuCh
ViveliDuCh merged commit af81de0 into release/11.0 Nov 14, 2026
99 of 104 checks passed
@ViveliDuCh
ViveliDuCh deleted the backport/pr-136463-to-release/11.0 branch November 14, 2026 06:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Text.Json Servicing-approved Approved for servicing release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants