Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable custom query validato into SelectExpandQueryValidator #800

Merged
merged 4 commits into from
Jan 18, 2023
Merged

Conversation

xuzhg
Copy link
Member

@xuzhg xuzhg commented Jan 10, 2023

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.

@@ -19,5 +27,7 @@ public interface ISelectExpandQueryValidator
/// <param name="selectExpandQueryOption">The $select and $expand query.</param>
/// <param name="validationSettings">The validation settings.</param>
void Validate(SelectExpandQueryOption selectExpandQueryOption, ODataValidationSettings validationSettings);

// Task ValidateAsync(SelectExpandClause selectExpandClause, ODataValidationSettings validationSettings);
Copy link
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
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.

}

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

@gathogojr gathogojr Jan 11, 2023

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
Member Author

Choose a reason for hiding this comment

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

Yes. Thanks.

/// <summary>
/// Validates $select. For example, ~/Customers?$select=Prop($select=SubProp;$top=2)
/// </summary>
/// <param name="pathSelectItem"></param>
Copy link
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
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
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
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
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
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
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
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
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
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).

{
if (filterClause != null)
if (orderByClause != null)
Copy link
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
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
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
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
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
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
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
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;}
}

Copy link
Contributor

@gathogojr gathogojr left a comment

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