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

How to search for text within attribute list? #184

Open
bjr001 opened this issue May 5, 2021 · 2 comments
Open

How to search for text within attribute list? #184

bjr001 opened this issue May 5, 2021 · 2 comments

Comments

@bjr001
Copy link

bjr001 commented May 5, 2021

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
Copy link
Contributor

jenschude commented May 6, 2021

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
Copy link
Author

bjr001 commented May 6, 2021

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?

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

No branches or pull requests

2 participants