Fix System.Text.Json source generator constructor accessor for generic types with inaccessible constructors - #133406
Draft
svick wants to merge 1 commit into
Draft
Fix System.Text.Json source generator constructor accessor for generic types with inaccessible constructors#133406svick wants to merge 1 commit into
svick wants to merge 1 commit into
Conversation
The System.Text.Json source generator emitted an invalid [UnsafeAccessor(Constructor)] extern for a generic type with an inaccessible constructor: the extern named the closed generic type directly, which does not resolve the constructor and threw MissingMethodException at run time. Emit the constructor extern inside a generic wrapper class (as the member get/set/field accessors already do), using the open type parameters, and reference it through the closed type arguments at the call site. Non-generic types and the downlevel reflection fallback are unchanged. Fixes dotnet#133369. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Azure Pipelines: Successfully started running 4 pipeline(s). 12 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 |
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.
Summary
Fixes #133369.
The System.Text.Json source generator emitted an invalid
[UnsafeAccessor(UnsafeAccessorKind.Constructor)]extern for a generic type with an inaccessible constructor (e.g. aprivate [JsonConstructor]). The extern named the closed generic type directly:That compiles, but
UnsafeAccessordoes not resolve a constructor against a directly-named closed generic type, so deserialization threwSystem.MissingMethodException: Method not found: 'MyType`1..ctor'at run time. The member (get/set/field) accessors already handle generics correctly via a wrapper class; only the constructor accessor was missing that treatment.Fix
Emit the constructor extern inside a generic wrapper class whose type parameters flow into the signature, and reference it through the closed type arguments at the call site:
This mirrors the existing
__GenericAccessors_<T>member-accessor wrapper (distinct name, so a type with both inaccessible members and an inaccessible constructor emits both wrappers without clashing). Non-generic types and the downlevel reflection fallback (net462) are unchanged.Spec additions carrying the open-generic strings (incremental-pipeline safe):
TypeGenerationSpec.{TypeParameterNames, OpenTypeFQN, TypeParameterConstraintClauses}andParameterGenerationSpec.OpenParameterTypeFQN, populated only when the type is generic and generic UnsafeAccessors are supported (.NET 9+).Tests
UnsafeAccessors_GenericTypeInaccessibleConstructor(netcoreapp UnsafeAccessor wrapper + net462 reflection fallback).JsonSerializerContextTests.GenericTypeWithInaccessibleConstructor_IsSupportedthat round-trips a generic type with a private[JsonConstructor]; it throwsMissingMethodExceptionbefore the fix and passes after.Note
This PR description was generated by GitHub Copilot.