Skip to content

Add nullability assertions for members (nullable reference type metadata) #330

@vbreuss

Description

@vbreuss

Why

With nullable reference types, teams often have conventions like "all properties of request/response DTOs must be nullable" (everything is optional on the wire) or "domain entities must not expose nullable members" (absence is modeled explicitly). These conventions are invisible to the compiler across a whole module and currently cannot be verified with aweXpect.Reflection at all, because nullability of reference types is only available via attribute metadata (NullableAttribute / NullableContextAttribute), which is tedious to read manually.

Example of the rule this would enable:

await Expect.That(In.AssemblyContaining<MyRequest>()
        .Types().WithName("*Request").AsWildcard())
    .OnlyHaveNullableMembers();

Proposal

  • Assertions on type collections: OnlyHaveNullableMembers() and OnlyHaveNonNullableMembers() (covering fields and properties; for value types this maps to Nullable<T>)
  • Optionally, member-level filters/assertions: WhichAreNullable() / IsNullable() on properties and fields

The implementation needs to combine Nullable<T> detection for value types with the NRT attribute metadata for reference types (NullabilityInfoContext can do this on supported targets; older targets may need a manual fallback or reduced support).

The failure message should list the members that violate the expected nullability.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions