ModelBuilder icon indicating copy to clipboard operation
ModelBuilder copied to clipboard

Nullability of property when property is a collection of enum type is not correct

Open dotnet-enthusiast opened this issue 1 month ago • 2 comments

Using ODataConventionModelBuilder, I generate a complex type with the following property.

public class RecurrencePattern
{
    public List<DayOfWeek>? DaysOfWeek { get; set; }
}

where DayOfWeek is defined as an enumeration.

public enum DayOfWeek
{
    Sunday = 0,
    Monday = 1,
    Tuesday = 2,
    Wednesday = 3,
    Thursday = 4,
    Friday = 5,
    Saturday = 6
}

Wrote a simple unit test to compare the IEdmModel generated from my code vs one generated from an csdl file, to ensure that my api workload doesn't introduce any breaking changes.

The IEdmModel generated from the schema has the following configuration.

<ComplexType Name="recurrencePattern">
  <Property Name="type" Type="microsoft.graph.recurrencePatternType" />
  <Property Name="interval" Type="Edm.Int32" Nullable="false" />
  <Property Name="month" Type="Edm.Int32" Nullable="false" />
  <Property Name="dayOfMonth" Type="Edm.Int32" Nullable="false" />
  <Property Name="daysOfWeek" Type="Collection(microsoft.graph.dayOfWeek)" />
  <Property Name="firstDayOfWeek" Type="microsoft.graph.dayOfWeek" />
  <Property Name="index" Type="microsoft.graph.weekIndex" />
</ComplexType>
<EnumType Name="dayOfWeek">
  <Member Name="sunday" Value="0" />
  <Member Name="monday" Value="1" />
  <Member Name="tuesday" Value="2" />
  <Member Name="wednesday" Value="3" />
  <Member Name="thursday" Value="4" />
  <Member Name="friday" Value="5" />
  <Member Name="saturday" Value="6" />
</EnumType>

When I compare the property daysOfWeek on the recurencePattern type generated from the ODataConventionModelBuilder vs the one generated from the csdl, the property has Nullable=false on the ODataConvetionModelBuilder and Nullable=true on the one generated from the csdl.

Expection:

I would expect the property to be Nullable=true when using the ODataConventionModelBuilder. I have confirmed that is the case when the collection is an entity type or complex type, but not when it is a collection of enumerations as shown above.

dotnet-enthusiast avatar Oct 27 '25 19:10 dotnet-enthusiast

Just FYI the API guide for Enum:

https://github.com/microsoft/api-guidelines/blob/vNext/graph/patterns/enums.md#flag-enums-or-collection-of-enums

xuzhg avatar Nov 04 '25 17:11 xuzhg

It appears that ODataConventionModelBuilder is operating correctly in this case.

Note that Nullable='true' on a collection does not mean that the collection property can be null (i.e., List<DayOfWeek>?); it means that the collection can contain null values (i.e., List<DayOfWeek?>.)

In OData, collection-valued properties may be empty, but are never null. The Nullable='true' means that it may contain null values, but this is rare, so Nullable='true' is almost never applied to collection-valued properties in OData.

mikepizzo avatar Nov 04 '25 17:11 mikepizzo