Skip to content

Enable custom query validato into SelectExpandQueryValidator - #800

Merged
xuzhg merged 4 commits into
mainfrom
issue767_3
Jan 18, 2023
Merged

Enable custom query validato into SelectExpandQueryValidator#800
xuzhg merged 4 commits into
mainfrom
issue767_3

Conversation

@xuzhg

@xuzhg xuzhg commented Jan 10, 2023

Copy link
Copy Markdown
Member

issue #767

Refactor SelectExpandQueryValidator by:

  1. Remove the private field
  2. Create/use the validator context
  3. Separate each part validation into a protected virtual, so the customer can customize each part.
  4. Remove "very bad" internal virtual method in FilterQueryValidator
  5. Other changes.

/// <param name="validationSettings">The validation settings.</param>
void Validate(SelectExpandQueryOption selectExpandQueryOption, ODataValidationSettings validationSettings);

// Task ValidateAsync(SelectExpandClause selectExpandClause, ODataValidationSettings validationSettings);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's the deal with the commented code? Shouldn't it be removed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh, Yes. Forgot it. Will update it.

remainDepth = maxDepth;
}

//if (expandConfiguration.MaxDepth > 0 && currentDepth >= expandConfiguration.MaxDepth)

@gathogojr gathogojr Jan 11, 2023

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this commented out code be removed instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes. Thanks.

Comment thread src/Microsoft.AspNetCore.OData/Query/Validator/SelectExpandQueryValidator.cs Outdated
/// <summary>
/// Validates $select. For example, ~/Customers?$select=Prop($select=SubProp;$top=2)
/// </summary>
/// <param name="pathSelectItem"></param>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Supply the parameter description

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

#Resolved.

ValidateSearch(pathSelectItem.SearchOption, subValidatorContext);

// Validate the nested $compute within $select
ValidateCompute(pathSelectItem.ComputeOption, subValidatorContext);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I assume it's not valid to have $levels and $apply nested in a $select expression?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No, They are not.

/// </summary>
/// <param name="filterClause">The nested $filter clause.</param>
/// <param name="validatorContext">The validator context.</param>
protected virtual void ValidateFilter(FilterClause filterClause, SelectExpandValidatorContext validatorContext)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For this method and the related ones, would it have been better to named them ValidateNestedXXX?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I original thought we don't need 'nested' since they are 'protected virtual' within SelectExpandValidator.

But, I am ok to rename them.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Given your explanation it would have been okay to retain them as they were...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

#Resolved. Never mind, I updated them.

OrderByModelLimitationsValidator orderByQueryValidator =
new OrderByModelLimitationsValidator(validatorContext.Context, validatorContext.Context.DefaultQuerySettings.EnableOrderBy);

orderByQueryValidator.TryValidate(validatorContext.Property, validatorContext.StructuredType, orderByClause, false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does TryValidate return a boolean value of true/false and should we be checking the return value?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's the old codes/existing codes that I will refactor later (next PR).

protected virtual void ValidateOrderby(OrderByClause orderByClause, SelectExpandValidatorContext validatorContext)
{
if (filterClause != null)
if (orderByClause != null)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just to observe that in some methods, you're doing:

if (xxxOption == null)
{
    return;
}

// Validation logic here

Here and in some other methods you're nesting the validation logic within the if block...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It depends on the "number of codes line". #won't fix.

xuzhg and others added 3 commits January 11, 2023 10:50
_structuredType = filterQueryOption.Context.TargetStructuredType;
}

_property = filterQueryOption.Context.TargetProperty;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't understand this change. Why was the if statement there in the first place?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think it's safe to check.
Actually, in the ODataQueryContext, we have the Path null check already.
Now, I added an internal constructor to set the TargetStructuredType and property, so, we should remove the null check here.


/// <summary>
/// The remaining depth on property.
/// It's weird logic in current implementation. Need to improve it later.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What implementation does this comment refer to? Why is it weird? If it needs to be implemented late, consider creating an issue to track it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's weird because the 'RemainingDepth' is working with the query setting configuration on the property. I can't understand such logic.
Maybe it's correct, but I do not fully understand this.
Let me update the description.

ValidationSettings = this.ValidationSettings,
Property = this.Property,
StructuredType = this.StructuredType,
RemainingDepth = this.RemainingDepth,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is RemainingDepth property value set by the customer or by the framework? At which point is it calculated?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In the SelectExpandQueryValidator.cs at line 192 at SelectExpandQueryValidator.cs

It's calculated at Line 170 - 176.
The codes read the query setting on property (Model query setting). Model query setting is something

public class Customer
{

       [Expand(MaxDepth=3)]
       public Order MyOrder {get;set;}
}

@gathogojr gathogojr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

:shipit:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants