Skip to content

[bug]: SearchClient#browse_rules doesn't deserialize api response when a rule has a single condition #512

@ppatt

Description

@ppatt

Description

The rules search endpoint in case of a single condition being present returns a condition field:

$ curl --request POST \
  --url https://APP_ID.algolia.net/1/indexes/INDEX/rules/search \
  --header 'accept: application/json' \
  --header 'content-type: application/json' \
  --header 'x-algolia-api-key: API_KEY' \
  --header 'x-algolia-application-id: APP_ID' \
  --data '{"query":""}'

{"hits":[{"_metadata":{"lastUpdate":1599137775},"condition":{"pattern":"some name","anchoring":"is"},"consequence":{"params":{"filters":"name:'some other name'"}},"description":"","enabled":true,"objectID":"123","_highlightResult":{"condition":{"pattern":{"value":"some name","matchLevel":"none","matchedWords":[]}},"consequence":{"params":{"filters":{"value":"name:'some other name'","matchLevel":"none","matchedWords":[]}}},"description":{"value":"","matchLevel":"none","matchedWords":[]}}}],"nbHits":1,"page":0,"nbPages":1}

The Rule class always expects a conditions array:

:conditions => :"Array<Condition>",

This makes us retrieve conditionless rules in cases where there is a single condition.

We can monkeypatch it with:

module AlgoliaSearchRuleAttributesFix
  def self.prepended(base)
    base.singleton_class.prepend ClassMethods
  end

  module ClassMethods
    def build_from_hash(attributes)
      attributes = attributes.transform_keys(&:to_sym)
      if attributes.key?(:condition)
        (attributes[:conditions] ||= []) << attributes.delete(:condition)
      end
      super attributes
    end
  end
end

Algolia::Search::Rule.prepend AlgoliaSearchRuleAttributesFix

But the fix would be to make the api consistent in case of a single or multiple conditions

Client

Search

Version

3.34.4

Relevant log output

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions