Skip to content

A DocumentDbContext whose class name does not end in "Context" gets no generated code and no diagnostic; its collections stay null #300

Closed

Description

@CaffeinatedCoder

The source generator only looks at classes whose name ends in Context. A DocumentDbContext subclass named anything else (Shop, AppDb, Store, CatalogDb) gets no generated code, and the build reports nothing. Its collection properties stay null, and the first call throws NullReferenceException, far from the cause.

The README used such a name itself until #298 (new AppDb(...) in the master-key and multi-process samples), which is how this was found.

Repro

public class Customer { public ObjectId Id { get; set; } public string Name { get; set; } = ""; }

public partial class ShopContext : DocumentDbContext
{
    public DocumentCollection<ObjectId, Customer> Customers { get; set; } = null!;
    public ShopContext(string path) : base(path) { }
}

public partial class Shop : DocumentDbContext     // same body, different name
{
    public DocumentCollection<ObjectId, Customer> Customers { get; set; } = null!;
    public Shop(string path) : base(path) { }
}

await using var ok = new ShopContext("a.db");   // ok.Customers is set
await using var db = new Shop("b.db");          // db.Customers is null
await db.Customers.InsertAsync(new Customer { Name = "Ada" });   // NullReferenceException

Build with -p:EmitCompilerGeneratedFiles=true: the generator emits GenCheck.ShopContext_Program.Mappers.g.cs and nothing for Shop; no BLITE diagnostic.

Expected: a class that derives from DocumentDbContext is generated whatever its name, or the build says why it is not.

Actual: silently skipped.

Where in the code (0450b97)

  • MapperGenerator.IsPotentialDbContext (src/BLite.SourceGenerators/MapperGenerator.cs:492-499) filters on classDecl.Identifier.Text.EndsWith(BLiteConventions.DbContextClassSuffix), with DbContextClassSuffix = "Context" (src/BLite.SourceGenerators/Helpers/BLiteConventions.cs:13). The filter has been there since the generator's first version (219efaa, v1.0.0).
  • GetDbContextInfo (MapperGenerator.cs:805-813) then checks semantically that the class inherits from DocumentDbContext, so the name filter is only a cheap pre-filter, and it drops real contexts.
  • Neither the README nor the XML docs of DocumentDbContext mention the naming rule.

Suggested fix

  • Drop the name condition from the syntax predicate (ClassDeclarationSyntax with a BaseList is cheap enough; the semantic check already decides), or
  • keep it as a fast path, and report a warning (BLITE00x) for any class that derives from DocumentDbContext but was skipped, naming the rule.
  • Test: a context named without the suffix is generated (or the diagnostic is reported), in the generator tests.

Related: #152 (contexts in the global namespace, the same kind of silent skip), #298.

Activity

  1. moved this from Todo to In Progress in BLiteon Oct 18, 2026
  2. added
    severity/mediumSpace leak, inconsistency or failure in a narrow scenario
    and removed on Oct 18, 2026
  3. mrdevrobot commented on Oct 18, 2026

    @mrdevrobot
    Contributor

    Reproduced on 0450b97: a context named Shop builds with no diagnostic and its collections are null. Medium.

  4. moved this from In Progress to Done in BLiteon Oct 18, 2026
  5. added this to the 5.3.0 milestone on Oct 18, 2026
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

    bugSomething isn't workingpackage/generatorseverity/mediumSpace leak, inconsistency or failure in a narrow scenario

    Type

    No type

    Projects

    • Status
      Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions