commercetools / commercetools-dotnet-core-sdk

The e-commerce SDK from commercetools running on the .NET Core platform
https://docs.commercetools.com/sdk/dotnet-sdk#net-core-sdk
Apache License 2.0
10 stars 5 forks source link

How to search for text within attribute list? #184

Open bjr001 opened 3 years ago

bjr001 commented 3 years ago

What's the proper way to filter for value within a (custom) attribute list?

Currently I'm trying to do as following...

var queryCommand = new QueryCommand<ProductProjection>();
queryCommand .Where(p => p.Variants.Any(variant => variant.Attributes.Any(attribute =>
                    attribute.Name == "MyCustomAttribute" &&
                    attribute.ToSetTextAttribute().Value.ContainsAny("MyCustomValue"))));

...which doesn't work out since API returns BadRequest:

Response: {"statusCode":400,"message":"Malformed parameter: where: The field 'value' does not support this expression.","errors":[{"code":"InvalidInput","message":"Malformed parameter: where: The field 'value' does not support this expression."}]}

Any ideas?

Using version 1.1.3 of commercetools.Sdk.All.

jenschude commented 3 years ago

From the documentation itself I would assume it's correct as this produces the correct predicate:

        [Fact]
        public void ExpressionPropertySetTextAttributeValueEqual()
        {
            Expression<Func<ProductProjection, bool>> expression = p => p.Variants.Any(variant => variant.Attributes.Any(a => a.ToTextAttribute().Name == "text-name" && a.ToSetTextAttribute().Value.ContainsAny("text-value")));
            IQueryPredicateExpressionVisitor queryPredicateExpressionVisitor = this.linqFixture.GetService<IQueryPredicateExpressionVisitor>();
            string result = queryPredicateExpressionVisitor.Render(expression);
            Assert.Equal("variants(attributes(name = \"text-name\" and value contains any (\"text-value\")))", result);
        }

And as you can see the message comes back from the API that it doesn't support this option. This means it's not an issue of the SDK itself.

Could you please forward this to our support at https://support.commercetools.com

bjr001 commented 3 years ago

Okay, so I get in contact with CT support and according to them the "in" operator should be used:

We used the 'in' operator based on the documentation provided here where it offers query predicate examples:

https://docs.commercetools.com/api/predicates/query#query-predicates-by-example

// Check whether a field's value is or is not contained in // a specified set of values. age in (42, 43, 44) age not in (42, 43, 44)

So I tried as following....

var queryCommand = new QueryCommand<ProductProjection>();
queryCommand .Where(p => p.Variants.Any(variant => variant.Attributes.Any(attribute =>
                    attribute.Name == "MyCustomAttribute" &&
                    attribute.ToTextAttribute().Value.In("MyCustomValue"))));

...which does return expected result although "MyCustomAttribute" is set of text.

Does this mean there will be an upcoming change to .NET Sdk?