Skip to content

Commit

Permalink
Verify the compute clause in compute query validator
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzhg committed Nov 30, 2022
1 parent 2052852 commit b3749a8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,21 @@ public class ComputeQueryValidator
/// <param name="validationSettings">The validation settings.</param>
public virtual void Validate(ComputeQueryOption computeQueryOption, ODataValidationSettings validationSettings)
{
if (computeQueryOption == null)
{
throw Error.ArgumentNull(nameof(computeQueryOption));
}

if (validationSettings == null)
{
throw Error.ArgumentNull(nameof(validationSettings));
}

// so far, we don't have validation rules here for $compute
// however, customer can use this to inject the validator to add his own rules
// because 'DefaultQuerySetting' doesn't have configuration for $compute
// we can only let ODL to parse and verify the compute clause,
// however, developer can override this method add his own rules
_ = computeQueryOption.ComputeClause;
}

internal static ComputeQueryValidator GetComputeQueryValidator(ODataQueryContext context)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//-----------------------------------------------------------------------------
// <copyright file="ComputeQueryValidatorTests.cs" company=".NET Foundation">
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
// See License.txt in the project root for license information.
// </copyright>
//------------------------------------------------------------------------------

using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Query.Validator;
using Microsoft.AspNetCore.OData.Tests.Commons;
using Microsoft.OData;
using Xunit;

namespace Microsoft.AspNetCore.OData.Tests.Query.Validator
{
public class ComputeQueryValidatorTests
{
private ComputeQueryValidator _validator = new ComputeQueryValidator();
private ODataQueryContext _context = ValidationTestHelper.CreateCustomerContext();

[Fact]
public void ValidateComputeQueryValidator_ThrowsOnNullOption()
{
// Arrange & Act & Assert
ExceptionAssert.ThrowsArgumentNull(
() => _validator.Validate(null, new ODataValidationSettings()), "computeQueryOption");
}

[Fact]
public void ValidateComputeQueryValidator_ThrowsOnNullSettings()
{
// Arrange & Act & Assert
ExceptionAssert.ThrowsArgumentNull(
() => _validator.Validate(new ComputeQueryOption("substring(Name, 0, 1) as FirstChar", _context), null), "validationSettings");
}

[Fact]
public void ValidateComputeQueryValidator_ThrowsIfWithoutAsInComputeClause()
{
// Arrange & Act & Assert
ExceptionAssert.Throws<ODataException>(() =>
_validator.Validate(
new ComputeQueryOption("test add p12m", _context),
new ODataValidationSettings()),
"'as' expected at position 13 in 'test add p12m'.");
}

[Fact]
public void ValidateComputeQueryValidator_ThrowsIfUnknownPropertyInComputeClause()
{
// Arrange & Act & Assert
ExceptionAssert.Throws<ODataException>(() =>
_validator.Validate(
new ComputeQueryOption("test add p12m as Any", _context),
new ODataValidationSettings()),
"Could not find a property named 'test' on type 'Microsoft.AspNetCore.OData.Tests.Query.Models.QueryCompositionCustomer'.");
}
}
}

0 comments on commit b3749a8

Please sign in to comment.