RVN004 - AbstractIndexCreationTask subclass is missing a Map assignment
-
RVN004 reports a missing
Mapassignment in a RavenDB index class. -
Default severity: Info
Automated code fix: No -
In this article:
Description
Indexes that derive from AbstractIndexCreationTask<TDocument> or AbstractIndexCreationTask<TDocument, TReduceResult>
and use the standard CreateIndexDefinition() implementation must assign Map before the client builds the index definition.
Without a Map, the client throws InvalidOperationException ("Map is required to generate an index…") when it builds the index definition,
so the index is never deployed.
When indexes are deployed together through IndexCreation.CreateIndexes() or store.ExecuteIndexes(),
a missing Map prevents every index in that batch from being deployed.
Trigger condition
When is RVN004 reported?
RVN004 is reported when no Map assignment is reachable from any constructor of the index class or its base classes,
including a base class from a referenced project.
RVN004 is still reported when Map is assigned only in a method that no constructor calls,
even if that method runs before the client builds the index definition.
When is RVN004 not reported?
An assignment found in any constructor of the index class or its base classes is enough to suppress RVN004. Assignments in methods of those classes also count when a constructor calls the method, directly or through other methods.
RVN004 recognizes assignments written as Map = ..., this.Map = ..., or base.Map = ....
See Limitations for cases where an assignment counts even though it may not execute.
RVN004 is also not reported for the following index classes, even when no Map assignment is found:
- Abstract index classes. Their subclasses are checked instead.
- Indexes that override
CreateIndexDefinition(). - Multi-map indexes. See RVN005.
- JavaScript indexes (
AbstractJavaScriptIndexCreationTask), which define their maps as strings. - Index classes that are
private,protected, orprivate protected, or nested in a class with one of these access levels. - Indexes whose required base-class information cannot be obtained from generated metadata or available source. This can happen when a base class is in a compiled assembly built with a client version that does not include the RavenDB analyzers. See Distribution.
How to resolve
Add a constructor (or update the existing one) that assigns the Map property.
If the class is only a base for other indexes, declare it abstract.
Before (triggers RVN004)
// Triggers RVN004: Map is never assigned.
public class Orders_ByCompany : AbstractIndexCreationTask<Order>
{
}
After (correct)
public class Orders_ByCompany : AbstractIndexCreationTask<Order>
{
public Orders_ByCompany()
{
Map = orders => from o in orders
select new { o.Company, o.Employee };
}
}
Limitations
RVN004 does not verify that every constructor or execution path assigns Map.
RVN004 can be suppressed even when Map remains unset:
- An assignment in any constructor counts, including a base constructor that the index never calls.
- An assignment inside a condition counts even when the condition prevents it from executing.
- An assignment inside a lambda or local function nested in a checked constructor or method counts even if the lambda or local function is never invoked.
- Calls to methods are matched by name only. An assignment in another overload with the same name can therefore suppress RVN004 even when that overload is never called.
RVN004 can be reported even when Map is assigned at runtime:
- An assignment made only in a property setter is not detected, even if the constructor sets that property.
- An assignment made through another reference, such as
index.Map = ..., is not recognized. - A helper method defined in a base class from a referenced project is not followed when only a derived constructor calls it.
Mapassignments reachable from that base class's own constructors still count through its generated index metadata.