Skip to content

feat(Table): support navigation property null value #7296

Merged
ArgoZhang merged 3 commits intomainfrom
fix-table
Dec 11, 2025
Merged

feat(Table): support navigation property null value #7296
ArgoZhang merged 3 commits intomainfrom
fix-table

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented Dec 11, 2025

Link issues

fixes #7293

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Enhancements:

  • Refactor complex property expression building to use a reusable property-access helper that handles nullable navigation chains and dynamic objects.

Copilot AI review requested due to automatic review settings December 11, 2025 09:19
@bb-auto bb-auto Bot added the enhancement New feature or request label Dec 11, 2025
@bb-auto bb-auto Bot added this to the v10.1.0 milestone Dec 11, 2025
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Dec 11, 2025

Reviewer's Guide

Refactors complex property expression building in LambdaExtensions to be null-safe for navigation properties and support dynamic objects while simplifying the navigation traversal logic.

Class diagram for updated LambdaExtensions complex property handling

classDiagram
    class LambdaExtensions {
        +Expression~Func~TModel, TResult~~ GetComplexPropertyExpression~TModel, TResult~()
        -static Expression BuildPropertyAccess(Expression instance, Type instanceType, string propertyName)
    }

    class Expression
    class Type
    class IDynamicMetaObjectProvider
    class RuntimeBinder
    class CSharpArgumentInfo

    LambdaExtensions ..> Expression : uses
    LambdaExtensions ..> Type : uses
    LambdaExtensions ..> IDynamicMetaObjectProvider : checks assignable
    LambdaExtensions ..> RuntimeBinder : dynamic member access
    LambdaExtensions ..> CSharpArgumentInfo : dynamic binder args
Loading

File-Level Changes

Change Details Files
Refactor complex property expression building to handle null navigation properties and dynamic objects.
  • Replace per-step reflection and instance tracking with an expression-based navigation chain starting from the parameter converted to the root type
  • Introduce a reusable BuildPropertyAccess helper that builds property access expressions and returns null when the instance is null
  • Add conditional expression logic to guard property access with a null check on the parent instance
  • Add support for dynamic property access when the instance type implements IDynamicMetaObjectProvider, using C# runtime binder
  • Update GetComplexPropertyExpression to use BuildPropertyAccess for each segment of a dotted property path and return the final lambda expression
src/BootstrapBlazor/Extensions/LambdaExtensions.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#7293 Ensure Table/Blazor UI can bind and render navigation property chains (e.g., foo.Bar.Baz) where intermediate navigation entities may be null, without throwing or crashing.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

sourcery-ai[bot]
sourcery-ai Bot previously approved these changes Dec 11, 2025
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • The new BuildPropertyAccess null-guard logic assumes reference types: comparing instance to Expression.Constant(null, instanceType) and returning Expression.Constant(null, p.PropertyType) will throw or be invalid for non-nullable value types; consider restricting this path to reference/nullable types or using typeof(object) for the null constant and handling value types explicitly.
  • In GetComplexPropertyExpression, each iteration passes body (which becomes a conditional expression) as the instance to BuildPropertyAccess, but the conditional result may be a value type or object rather than the actual declaring type of the next property, so property chaining will fail for deeper navigation; you may need to track the actual runtime type (e.g., via reflection) instead of relying on body.Type after each step.
  • The dynamic-member branch in BuildPropertyAccess uses instanceType.IsAssignableTo(typeof(IDynamicMetaObjectProvider)), but after the first dynamic access instanceType will likely be object, so subsequent navigation on dynamic values will not take the dynamic path; consider carrying the original dynamic type or checking the expression node for being dynamic rather than relying solely on instanceType.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `BuildPropertyAccess` null-guard logic assumes reference types: comparing `instance` to `Expression.Constant(null, instanceType)` and returning `Expression.Constant(null, p.PropertyType)` will throw or be invalid for non-nullable value types; consider restricting this path to reference/nullable types or using `typeof(object)` for the null constant and handling value types explicitly.
- In `GetComplexPropertyExpression`, each iteration passes `body` (which becomes a conditional expression) as the `instance` to `BuildPropertyAccess`, but the conditional result may be a value type or `object` rather than the actual declaring type of the next property, so property chaining will fail for deeper navigation; you may need to track the actual runtime type (e.g., via reflection) instead of relying on `body.Type` after each step.
- The dynamic-member branch in `BuildPropertyAccess` uses `instanceType.IsAssignableTo(typeof(IDynamicMetaObjectProvider))`, but after the first dynamic access `instanceType` will likely be `object`, so subsequent navigation on dynamic values will not take the dynamic path; consider carrying the original dynamic type or checking the expression node for being dynamic rather than relying solely on `instanceType`.

## Individual Comments

### Comment 1
<location> `src/BootstrapBlazor/Extensions/LambdaExtensions.cs:681-682` </location>
<code_context>
+        {
+            var propertyAccess = Expression.Property(instance, p);
+            return Expression.Condition(
+                test: Expression.Equal(instance, Expression.Constant(null, instanceType)),
+                ifTrue: Expression.Constant(null, p.PropertyType),
+                ifFalse: propertyAccess
+            );
</code_context>

<issue_to_address>
**issue (bug_risk):** Null checks and null constants will break when `instanceType` or `p.PropertyType` are non-nullable value types.

This logic only works when both `instance` and `p.PropertyType` are nullable/comparable to `null`. For a non-nullable struct `instanceType`, `Expression.Equal(instance, Constant(null, instanceType))` will throw, and `Expression.Constant(null, p.PropertyType)` is also invalid for non-nullable value types. Please either restrict this path to reference types or perform the comparison via `object`/nullable types and then convert back to the target type.
</issue_to_address>

### Comment 2
<location> `src/BootstrapBlazor/Extensions/LambdaExtensions.cs:686-693` </location>
<code_context>
+                ifFalse: propertyAccess
+            );
+        }
+        else if (instanceType.IsAssignableTo(typeof(IDynamicMetaObjectProvider)))
+        {
+            var binder = Microsoft.CSharp.RuntimeBinder.Binder.GetMember(
+                CSharpBinderFlags.None,
+                propertyName,
+                instanceType,
+                new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) });
+            return Expression.Dynamic(binder, typeof(object), instance);
+        }
+
</code_context>

<issue_to_address>
**issue (bug_risk):** Dynamic member access support is limited to the first segment in a nested property path.

Because the dynamic branch returns an `Expression` of type `object`, the next iteration sees `instanceType == typeof(object)`, which no longer matches `IDynamicMetaObjectProvider`. Any subsequent segment after a dynamic one (e.g., `dynamicObj.Prop1.Prop2`) will therefore drop into the reflection path and fail. If chained dynamic access is needed, you’ll need to either carry forward the original dynamic type for `instanceType` or add a dedicated handling path for `instanceType == typeof(object)` that continues using dynamic binders.
</issue_to_address>

### Comment 3
<location> `src/BootstrapBlazor/Extensions/LambdaExtensions.cs:674` </location>
<code_context>
         }
     }

+    private static Expression BuildPropertyAccess(Expression instance, Type instanceType, string propertyName)
+    {
+        var p = instanceType.GetPropertyByName(propertyName);
</code_context>

<issue_to_address>
**issue (complexity):** Consider splitting property access, null-safety, and dynamic handling into separate helpers so the complex property expression is built through a clearer, linear chain.

You can keep the new features (null-safety and dynamic support) while reducing complexity by splitting responsibilities and making the chain construction more linear.

### 1. Split `BuildPropertyAccess` into focused helpers

Right now `BuildPropertyAccess` handles:
- static reflection
- null checks
- dynamic binder

You can isolate these concerns:

```csharp
private static Expression BuildPropertyAccessCore(Expression instance, Type instanceType, string propertyName)
{
    var p = instanceType.GetPropertyByName(propertyName);
    if (p != null)
    {
        // pure static property access
        return Expression.Property(instance, p);
    }

    if (typeof(IDynamicMetaObjectProvider).IsAssignableFrom(instanceType))
    {
        return BuildDynamicPropertyAccess(instance, instanceType, propertyName);
    }

    throw new InvalidOperationException($"类型 {instanceType.Name} 未找到 {propertyName} 属性,无法获取其值");
}

private static Expression BuildDynamicPropertyAccess(Expression instance, Type instanceType, string propertyName)
{
    var binder = Microsoft.CSharp.RuntimeBinder.Binder.GetMember(
        CSharpBinderFlags.None,
        propertyName,
        instanceType,
        new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) });

    return Expression.Dynamic(binder, typeof(object), instance);
}
```

### 2. Move null-safety into a small wrapper

Instead of mixing null checks into property resolution, add a single-purpose helper that wraps any access expression with a per-step null guard:

```csharp
private static Expression BuildNullSafeAccess(Expression instance, Func<Expression, Expression> accessFactory)
{
    var access = accessFactory(instance);

    return Expression.Condition(
        test: Expression.Equal(instance, Expression.Constant(null, instance.Type)),
        ifTrue: Expression.Constant(null, access.Type),
        ifFalse: access);
}
```

### 3. Make `GetComplexPropertyExpression` linear and easy to follow

Now `GetComplexPropertyExpression` reads as “build a chain, applying null-safe access at each step” with the actual concerns separated:

```csharp
Expression<Func<TModel, TResult>> GetComplexPropertyExpression()
{
    var propertyNames = propertyName.Split(".");
    Expression body = Expression.Convert(parameter, type);

    foreach (var name in propertyNames)
    {
        body = BuildNullSafeAccess(
            body,
            instance => BuildPropertyAccessCore(instance, instance.Type, name));
    }

    return Expression.Lambda<Func<TModel, TResult>>(
        Expression.Convert(body, typeof(TResult)),
        parameter);
}
```

This keeps:
- per-segment null-propagation
- dynamic member support
- existing functionality

while reducing cognitive load by:
- separating static vs dynamic access
- separating null-safety from member resolution
- keeping the property-chain construction readable and linear.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/BootstrapBlazor/Extensions/LambdaExtensions.cs Outdated
Comment thread src/BootstrapBlazor/Extensions/LambdaExtensions.cs Outdated
Comment thread src/BootstrapBlazor/Extensions/LambdaExtensions.cs Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 11, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (24729ef) to head (8132eef).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #7296   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          745       745           
  Lines        32628     32629    +1     
  Branches      4522      4520    -2     
=========================================
+ Hits         32628     32629    +1     
Flag Coverage Δ
BB 100.00% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ArgoZhang ArgoZhang merged commit 604e468 into main Dec 11, 2025
6 checks passed
@ArgoZhang ArgoZhang deleted the fix-table branch December 11, 2025 09:28
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds null navigation property support for Table components by refactoring the complex property expression building logic to handle null intermediate properties gracefully. The change addresses issue #7293 where accessing nested properties (e.g., Foo.Name) would throw NullReferenceException when intermediate properties were null.

Key Changes:

  • Refactored GetComplexPropertyExpression() to use a new helper method for property access
  • Introduced BuildPropertyAccess() method that wraps property access with null-checking conditional expressions
  • Version bumped to 10.1.4-beta01

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/BootstrapBlazor/Extensions/LambdaExtensions.cs Refactored complex property expression building to add null-safe navigation; extracted property access logic into BuildPropertyAccess helper method
src/BootstrapBlazor/BootstrapBlazor.csproj Version bumped to 10.1.4-beta01 for beta release
Comments suppressed due to low confidence (2)

src/BootstrapBlazor/Extensions/LambdaExtensions.cs:697

  • The new BuildPropertyAccess method lacks test coverage for null navigation properties. While existing tests in LambadaExtensionsTest.cs cover GetPropertyValueLambda with nested properties (e.g., "Foo.Name"), there are no tests that verify behavior when intermediate properties are null (e.g., when Foo is null in "Foo.Name"). Tests should be added to verify that null navigation properties return null instead of throwing NullReferenceException.
    private static ConditionalExpression BuildPropertyAccess(Expression instance, Type instanceType, string propertyName)
    {
        var p = instanceType.GetPropertyByName(propertyName) ?? throw new InvalidOperationException($"类型 {instanceType.Name} 未找到 {propertyName} 属性,无法获取其值");

        var propertyAccess = Expression.Property(instance, p);
        return Expression.Condition(
            test: Expression.Equal(instance, Expression.Constant(null, instanceType)),
            ifTrue: Expression.Constant(null, p.PropertyType),
            ifFalse: propertyAccess
        );
    }

    /// <summary>
    /// 给指定模型属性赋值 Lambda 表达式
    /// </summary>
    /// <typeparam name="TModel"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    /// <param name="model"></param>
    /// <param name="propertyName"></param>
    /// <returns></returns>
    public static Expression<Action<TModel, TValue>> SetPropertyValueLambda<TModel, TValue>(TModel model, string propertyName)
    {
        if (model == null)
        {

src/BootstrapBlazor/Extensions/LambdaExtensions.cs:693

  • Dynamic property access doesn't include null checking like the static property case. If the instance is null, the dynamic invocation will throw a NullReferenceException at runtime. Consider wrapping the dynamic expression with a similar null-check conditional to provide consistent behavior between static and dynamic property access.
    /// <summary>
    /// 给指定模型属性赋值 Lambda 表达式
    /// </summary>
    /// <typeparam name="TModel"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    /// <param name="model"></param>
    /// <param name="propertyName"></param>
    /// <returns></returns>

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +674 to +683
private static ConditionalExpression BuildPropertyAccess(Expression instance, Type instanceType, string propertyName)
{
var p = instanceType.GetPropertyByName(propertyName) ?? throw new InvalidOperationException($"类型 {instanceType.Name} 未找到 {propertyName} 属性,无法获取其值");

var propertyAccess = Expression.Property(instance, p);
return Expression.Condition(
test: Expression.Equal(instance, Expression.Constant(null, instanceType)),
ifTrue: Expression.Constant(null, p.PropertyType),
ifFalse: propertyAccess
);
Copy link

Copilot AI Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The null-checking logic has several issues: (1) Expression.Equal cannot properly handle value types - if instanceType is a value type (struct), comparing it to null will cause compilation errors. (2) Expression.Constant(null, p.PropertyType) will throw ArgumentException if p.PropertyType is a non-nullable value type. The code should check if instanceType is a reference type or nullable value type before adding the null check, and ensure the return type is properly nullable.

Suggested change
private static ConditionalExpression BuildPropertyAccess(Expression instance, Type instanceType, string propertyName)
{
var p = instanceType.GetPropertyByName(propertyName) ?? throw new InvalidOperationException($"类型 {instanceType.Name} 未找到 {propertyName} 属性,无法获取其值");
var propertyAccess = Expression.Property(instance, p);
return Expression.Condition(
test: Expression.Equal(instance, Expression.Constant(null, instanceType)),
ifTrue: Expression.Constant(null, p.PropertyType),
ifFalse: propertyAccess
);
private static Expression BuildPropertyAccess(Expression instance, Type instanceType, string propertyName)
{
var p = instanceType.GetPropertyByName(propertyName) ?? throw new InvalidOperationException($"类型 {instanceType.Name} 未找到 {propertyName} 属性,无法获取其值");
var propertyAccess = Expression.Property(instance, p);
// Only add null check if instanceType is a reference type or nullable value type
if (!instanceType.IsValueType || Nullable.GetUnderlyingType(instanceType) != null)
{
// Only use null constant if property type is reference or nullable value type
if (!p.PropertyType.IsValueType || Nullable.GetUnderlyingType(p.PropertyType) != null)
{
return Expression.Condition(
test: Expression.Equal(instance, Expression.Constant(null, instanceType)),
ifTrue: Expression.Constant(null, p.PropertyType),
ifFalse: propertyAccess
);
}
else
{
return Expression.Condition(
test: Expression.Equal(instance, Expression.Constant(null, instanceType)),
ifTrue: Expression.Default(p.PropertyType),
ifFalse: propertyAccess
);
}
}
else
{
// For non-nullable value types, just return property access
return propertyAccess;
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(Table): sqlsugar 导航查询值为 null 时报错

2 participants