Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Asp.Net Core

Asp.Net Core: Fix JSON Source Generation Skipping a Derived Type

- 18.11.26 - ErcanOPAK

⚙️ Asp.Net Core: Fix JSON Source Generation Skipping a Derived Type

System.Text.Json’s source generator only emits serialization code for the exact set of types it’s told about at compile time through a JsonSerializerContext, which means polymorphic serialization of a base type needs every concrete subtype declared explicitly via JsonDerivedType. Add a new subtype later without updating that attribute list, and serializing a collection of the base type doesn’t throw — it just serializes each new-subtype instance using only the base type’s own properties, silently dropping every field the subtype added.

🔎 The Problem

[JsonPolymorphic]
[JsonDerivedType(typeof(EmailNotification), "email")]
[JsonDerivedType(typeof(SmsNotification), "sms")]
public abstract class Notification { public string UserId { get; set; } }

// Added later, but the attribute list above was never updated:
public class PushNotification : Notification
{
    public string DeviceToken { get; set; }
}

// Serializing a List<Notification> containing a PushNotification
// drops DeviceToken entirely - no exception, no warning

✅ Fix: Keep the Derived-Type List and Tests in Lockstep With the Hierarchy

  • Add a [JsonDerivedType] entry for every concrete type the moment it’s created, as part of the same change that adds the class, rather than treating it as a separate follow-up step.
  • Write a round-trip test per derived type that serializes through the actual generated JsonSerializerContext and asserts every derived-only property survives, so a missing registration fails a test instead of shipping silently.
  • Where the type hierarchy changes often, consider a small analyzer or a reflection-based startup check that compares every non-abstract subtype against the registered JsonDerivedType list and fails fast if one is missing.

🚨 Why This Is Dangerous

  • Losing a field only during polymorphic serialization, with the type’s own direct serialization working fine, is exactly the kind of bug that passes unit tests on the concrete type and only shows up once that type is serialized as part of a mixed collection in production.

A source generator can only serialize the types it was told about — adding a new subtype to your hierarchy doesn’t add it to the generator’s list for free.

— Backend Architect

Related posts:

ASP.NET Core: Stop appsettings.json Secrets From Leaking Into Your Git History

ASP.NET Core: Stop appsettings.json Secrets From Leaking Into Your Git History

.NET Core: Master Logging with Serilog and NLog

.NET Core: Master Logging with Serilog and NLog

.NET Core — Kestrel Has Request Limits You Might Hit

.NET Core — Kestrel Has Request Limits You Might Hit

Views: 2

Post navigation

Kubernetes: Fix an Ingress Rule Matching the Wrong Backend Service
SQL Server: Fix a Temp Table Causing a Plan Recompile Every Call

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (658)
  • Add Constraint to SQL Table to ensure email contains @ (613)
  • Average of all values in a column that are not zero in SQL (571)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (549)
  • Find numbers with more than two decimal places in SQL (477)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK
  • Git: Fix a Worktree Locking a Branch Needed Somewhere Else
  • Asp.Net Core: Fix a Background Service Leaking Scoped Dependencies
  • SQL Server: Fix a Columnstore Index Fragmenting After Bulk Deletes
  • Docker: Fix a Container Losing Logs After a Restart Policy
  • Asp.Net Core: Fix Output Caching Serving a Stale Authorized Page

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com