From dce3e44b3412c89426c9b1e3ed01a74fc0a62ecb Mon Sep 17 00:00:00 2001 From: Vladislav Fitc Date: Mon, 29 Aug 2022 16:07:42 +0200 Subject: [PATCH] fix(query rules): Suppress rule condition pattern marshalling in case of empty anchoring (#689) --- algolia/search/rule_condition.go | 2 +- algolia/search/rule_condition_test.go | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/algolia/search/rule_condition.go b/algolia/search/rule_condition.go index 9cbe41d49..74cc399d5 100644 --- a/algolia/search/rule_condition.go +++ b/algolia/search/rule_condition.go @@ -12,7 +12,7 @@ type RuleCondition struct { func (c RuleCondition) MarshalJSON() ([]byte, error) { m := make(map[string]interface{}) - if c.Anchoring != "" || c.Pattern != "" { + if c.Anchoring != "" { m["anchoring"] = c.Anchoring m["pattern"] = c.Pattern } diff --git a/algolia/search/rule_condition_test.go b/algolia/search/rule_condition_test.go index 3fadd023b..eed73091d 100644 --- a/algolia/search/rule_condition_test.go +++ b/algolia/search/rule_condition_test.go @@ -44,7 +44,7 @@ func TestRuleCondition_MarshalJSON(t *testing.T) { Alternatives: nil, Filters: "", }, - `{"anchoring": "", "pattern": "Pattern"}`, + `{}`, }, { RuleCondition{ @@ -106,7 +106,11 @@ func TestRuleCondition_MarshalJSON(t *testing.T) { // Compare the two RuleConditions require.Equal(t, c.condition.Anchoring, condition.Anchoring) - require.Equal(t, c.condition.Pattern, condition.Pattern) + if c.condition.Anchoring == "" { + require.Equal(t, condition.Pattern, "") + } else { + require.Equal(t, c.condition.Pattern, condition.Pattern) + } require.Equal(t, c.condition.Context, condition.Context) if c.condition.Alternatives == nil { require.Nil(t, condition.Alternatives)