Skip to content

Fix ObjectToInferredTypesConverter stack overflow when serializing plain object #46893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 19, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 19, 2025

The ObjectToInferredTypesConverter in the JSON serialization documentation was causing a stack overflow when attempting to serialize a plain object instance (e.g., new object()).

Problem

The issue occurred in the Write method:

public override void Write(
    Utf8JsonWriter writer,
    object objectToWrite,
    JsonSerializerOptions options) =>
    JsonSerializer.Serialize(writer, objectToWrite, objectToWrite.GetType(), options);

When objectToWrite is a plain object instance, objectToWrite.GetType() returns typeof(object), which causes the converter to recursively call itself indefinitely, resulting in a stack overflow.

Solution

Added a check for typeof(object) in the Write method. When the object type is exactly object, the method now writes an empty JSON object {} instead of recursively calling JsonSerializer.Serialize:

public override void Write(
    Utf8JsonWriter writer,
    object objectToWrite,
    JsonSerializerOptions options)
{
    var valueType = objectToWrite.GetType();
    if (valueType == typeof(object))
    {
        writer.WriteStartObject();
        writer.WriteEndObject();
        return;
    }

    JsonSerializer.Serialize(writer, objectToWrite, valueType, options);
}

Testing

  • ✅ Fixed the stack overflow for new object() serialization (now produces {})
  • ✅ Verified original documentation example still works correctly
  • ✅ Confirmed type inference functionality is preserved (DateTime, numbers, strings, etc.)
  • ✅ Tested edge cases with object properties in complex scenarios

This is a minimal, surgical fix that addresses the specific issue without breaking any existing functionality.

Fixes #43831.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] The sample converter of [Deserialize inferred types to object properties] serialize issue Fix ObjectToInferredTypesConverter stack overflow when serializing plain object Jun 19, 2025
@Copilot Copilot AI requested a review from gewarren June 19, 2025 11:47
Copilot finished work on behalf of gewarren June 19, 2025 11:47
@gewarren gewarren requested a review from eiriktsarpalis June 19, 2025 11:58
@gewarren gewarren marked this pull request as ready for review June 19, 2025 11:58
@gewarren gewarren requested a review from a team as a code owner June 19, 2025 11:58
@gewarren gewarren enabled auto-merge (squash) June 19, 2025 11:58
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

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

This LGTM @gewarren

Once you and @eiriktsarpalis agree on the style suggestion, go ahead and :shipit:

auto-merge was automatically disabled June 19, 2025 16:02

Head branch was pushed to by a user without write access

Copilot finished work on behalf of BillWagner June 19, 2025 16:03
@Copilot Copilot AI requested a review from BillWagner June 19, 2025 16:03
@Copilot Copilot AI requested a review from eiriktsarpalis June 19, 2025 16:46
Copilot finished work on behalf of eiriktsarpalis June 19, 2025 16:46
@gewarren gewarren enabled auto-merge (squash) June 19, 2025 17:17
@gewarren gewarren merged commit 6f4b235 into main Jun 19, 2025
10 checks passed
@gewarren gewarren deleted the copilot/fix-43831 branch June 19, 2025 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The sample converter of [Deserialize inferred types to object properties] serialize issue
4 participants