Skip to content

Fix trim/AOT safety for custom event argument deserialization in Blazor - #68113

Draft
NanthiniMahalingam wants to merge 2 commits into
dotnet:mainfrom
NanthiniMahalingam:fix-45851
Draft

Fix trim/AOT safety for custom event argument deserialization in Blazor#68113
NanthiniMahalingam wants to merge 2 commits into
dotnet:mainfrom
NanthiniMahalingam:fix-45851

Conversation

@NanthiniMahalingam

@NanthiniMahalingam NanthiniMahalingam commented Jul 30, 2026

Copy link
Copy Markdown

Bug description

  • When a Blazor component uses a custom event (registered via [EventHandler]) whose event-args type is deserialized from JSON, the members of that type could be trimmed away in trimmed/AOT-published apps. The deserialization path in WebEventData relied on JsonSerializer.Deserialize(string, Type, options), which is annotated RequiresUnreferencedCode.
  • That warning was being hidden with an UnconditionalSuppressMessage rather than actually being made trim-safe, so custom event args could fail to deserialize at runtime after trimming.

Root cause

  • The custom event-args Type flows from [EventHandler(..., Type eventArgsType)] → Renderer.GetEventArgsType(...) → JSON deserialization, but the Type was not annotated with [DynamicallyAccessedMembers]. The trimmer therefore had no signal to preserve the JSON-required members.
  • WebEventData.ParseEventArgsJson used the reflection-based JsonSerializer.Deserialize overload (RequiresUnreferencedCode) and merely suppressed the IL2026 warning, masking the real trimming hole instead of closing it.

Description of code changes

  • Annotated the eventArgsType constructor parameters and the EventArgsType property on EventHandlerAttribute.cs with DynamicallyAccessedMembers(LinkerFlags.JsonSerialized), so the members needed for JSON (de)serialization are preserved through trimming.
  • In WebEventData.cs, switched to the trim-safe API: resolve JsonTypeInfo via jsonSerializerOptions.GetTypeInfo(eventArgsType) and deserialize with that, removing the now-unnecessary UnconditionalSuppressMessage suppression.

Fixes #45851

Output

Before changes

beforefix-45851.mp4

After changes

afterfix-45851.mp4

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jul 30, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @NanthiniMahalingam. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WebEventData.ParseEventArgsJson has invalid UnconditionalSuppressMessage for JsonSerializer.Deserialize

1 participant