Skip to content

Reading a polymorphic Mongo collection without .variant(...) returns variant-only fields undecoded #30471

Open

Description

@wmadden-electric

What a user sees. For a model with @@discriminator and variant models declared with @@base, a read through the base collection (db.assets.all(), without .variant('Photo')) returns each document's variant-only fields exactly as the driver read them. A variant ObjectId field comes back as an ObjectId instance rather than a hex string, a Decimal128 as the driver's class rather than decimal text, and a Json field is not checked. Reads and writes through .variant(...) use the variant's codecs.

Where. packages/2-mongo-family/5-query-builders/orm/src/collection.ts: without a variant, the result shape is built from the base model's fields only (contractModelToMongoResultShape in packages/2-mongo-family/5-query-builders/query-builder/src/result-shape.ts), and the row decoder copies fields that are not in the shape unchanged (packages/2-mongo-family/7-runtime/src/codecs/decoding.ts).

Likely fix. Decode each row by its discriminator: build one result shape per variant (base fields plus that variant's fields) and pick the shape from the row's discriminator value, falling back to the base shape for an unknown value. Add an integration test that reads a mixed collection without .variant(...).

Where it came from. #30439, whose review found and fixed the .variant(...) case (the ORM skipped codecs on variant-only fields entirely) and recorded this one.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions