Skip to content

Bring back the Derived Type Discriminator Convention for MongoDB#1869

Merged
einari merged 2 commits intomainfrom
copilot/bring-back-derived-type-serializer
Mar 16, 2026
Merged

Bring back the Derived Type Discriminator Convention for MongoDB#1869
einari merged 2 commits intomainfrom
copilot/bring-back-derived-type-serializer

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 8, 2026

Re-introduces the DerivedTypeDiscriminatorConvention that was accidentally dropped during repository migration. This convention enables MongoDB to correctly serialize and deserialize polymorphic types decorated with DerivedTypeAttribute using a _derivedTypeId GUID discriminator field.

Added

  • DerivedTypeDiscriminatorConvention — implements IDiscriminatorConvention using _derivedTypeId as the BSON discriminator field; resolves derived types via IDerivedTypes.GetDerivedTypeFor and falls back to the nominal type when no discriminator is present
  • Auto-registration in MongoDBDefaults.Initialize — iterates IDerivedTypes.TypesWithDerivatives and registers the convention per base type via BsonSerializer.RegisterDiscriminatorConvention; no-ops gracefully if IDerivedTypes is not configured
// Types decorated with [DerivedType] will be stored with a GUID discriminator
[DerivedType("b91c21cd-56db-4edc-805b-b1d1ff9aa772", typeof(BaseDocument))]
public class SpecificDocument : BaseDocument { ... }

// MongoDB document stored as:
// { "_derivedTypeId": "b91c21cd-56db-4edc-805b-b1d1ff9aa772", ... }
Original prompt

This section details on the original issue you should resolve

<issue_title>Bring back the Derived Type serializer for MongoDB</issue_title>
<issue_description>We used to have a Derived Type serializer, but it got accidently removed during transitioning of repositories.
Bring this back from this fork:

https://github.com/einari/MongoDB/blob/main/Source/DerivedTypeDiscriminatorConvention.cs</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: einari <134365+einari@users.noreply.github.com>
Copilot AI changed the title [WIP] Restore derived type serializer for MongoDB Bring back the Derived Type Discriminator Convention for MongoDB Mar 8, 2026
einari added a commit that referenced this pull request Mar 16, 2026
19.10.0 release branch: merge PRs #1869, #1870, #1888 with build and test fixes
@einari einari merged commit 16707ea into main Mar 16, 2026
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.

Bring back the Derived Type serializer for MongoDB

2 participants