Skip to content

Use STJ's polymorphism support instead of custom converter#70

Merged
SteveSandersonMS merged 3 commits intogithub:mainfrom
stephentoub:polymorphism
Jan 21, 2026
Merged

Use STJ's polymorphism support instead of custom converter#70
SteveSandersonMS merged 3 commits intogithub:mainfrom
stephentoub:polymorphism

Conversation

@stephentoub
Copy link
Contributor

Also:

  • Mark properties as required if they're required in the schema
  • Mark reference types as nullable if they're optional in the schema
  • Remove the suppression of CS8618, which is resulting in public types having incorrect nullable annotations
  • Annotate enums so the NativeAOT-unfriendly non-generic enum converter isn't needed

@stephentoub stephentoub requested a review from a team as a code owner January 21, 2026 14:28
Copilot AI review requested due to automatic review settings January 21, 2026 14:28
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request modernizes the JSON serialization for session events by replacing a custom polymorphic JSON converter with System.Text.Json's built-in polymorphism support. The changes improve NativeAOT compatibility, add proper nullable annotations to reference types, and mark properties as required when they are required by the schema.

Changes:

  • Replaced custom SessionEventConverter with [JsonPolymorphic] and [JsonDerivedType] attributes on the base SessionEvent class
  • Added nullable annotations (?) to optional reference types and array properties
  • Added required modifier to properties that are required in the schema
  • Updated enum declarations with [JsonConverter(typeof(JsonStringEnumConverter<T>))] and [JsonStringEnumMemberName] attributes for NativeAOT compatibility
  • Updated usage code to use pattern matching instead of string-based type checking
  • Removed CS8618 warning suppression

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.

File Description
nodejs/scripts/generate-csharp-session-types.ts Updated generator to add nullable annotations to optional types, add required modifier to required properties, generate JsonPolymorphic attributes instead of custom converter, and add NativeAOT-friendly enum attributes
dotnet/src/Generated/SessionEvents.cs Regenerated file reflecting all generator changes - uses JsonPolymorphic attributes, has proper nullable annotations, required modifiers on properties, and NativeAOT-compatible enum declarations
dotnet/src/Session.cs Updated implementation code to use pattern matching (case AssistantMessageEvent:) instead of string comparisons (evt.Type == "assistant.message") and updated documentation examples
dotnet/src/Client.cs Removed unused imports (Newtonsoft.Json.Linq, System.Text.Json.Nodes) and simplified serializer options by removing converter loop
Comments suppressed due to low confidence (2)

dotnet/src/Session.cs:206

  • The code uses pattern matching without capturing variables (case AssistantMessageEvent:), but then tries to access type-specific properties (evt.Data?.Content). Since evt is still of type SessionEvent, this will not compile. The code should use pattern matching with variable declarations, for example: case AssistantMessageEvent assistantMessage: Console.WriteLine($"Assistant: {assistantMessage.Data?.Content}");
    ///     switch (evt)
    ///     {
    ///         case AssistantMessageEvent:
    ///             Console.WriteLine($"Assistant: {evt.Data?.Content}");
    ///             break;
    ///         case SessionErrorEvent:
    ///             Console.WriteLine($"Error: {evt.Data?.Message}");
    ///             break;

dotnet/src/Session.cs:335

  • The code checks if evt is AssistantMessageEvent but then tries to access evt.Data?.Content without casting. Since evt is still of type SessionEvent, which doesn't have a Data property with a Content field, this will not compile. The code should use pattern matching with a variable declaration, for example: if (evt is AssistantMessageEvent assistantMessage) Console.WriteLine($"Assistant: {assistantMessage.Data?.Content}");
    ///     if (evt is AssistantMessageEvent)
    ///     {
    ///         Console.WriteLine($"Assistant: {evt.Data?.Content}");

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Also:
- Mark properties as required if they're required in the schema
- Mark reference types as nullable if they're optional in the schema
- Remove the suppression of CS8618, which is resulting in public types having incorrect nullable annotations
- Annotate enums so the NativeAOT-unfriendly non-generic enum converter isn't needed
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 4 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@SteveSandersonMS SteveSandersonMS left a comment

Choose a reason for hiding this comment

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

Thanks very much @stephentoub!

@SteveSandersonMS SteveSandersonMS added this pull request to the merge queue Jan 21, 2026
Merged via the queue into github:main with commit cb80f83 Jan 21, 2026
0 of 12 checks passed
@stephentoub stephentoub deleted the polymorphism branch January 21, 2026 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants