Skip to content

Commit

Permalink
fix(query rules): Suppress rule condition pattern marshalling in case…
Browse files Browse the repository at this point in the history
… of empty anchoring (#689)
  • Loading branch information
VladislavFitz authored Aug 29, 2022
1 parent 3e8fd6d commit dce3e44
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion algolia/search/rule_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 6 additions & 2 deletions algolia/search/rule_condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestRuleCondition_MarshalJSON(t *testing.T) {
Alternatives: nil,
Filters: "",
},
`{"anchoring": "", "pattern": "Pattern"}`,
`{}`,
},
{
RuleCondition{
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit dce3e44

Please sign in to comment.