Skip to content

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

Closed

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

Activity

  1. changed the title [-][bug]: SearchClient.browse_rules doesn't deserialize api response when a rule has a single condition[/-] [+][bug]: SearchClient#browse_rules doesn't deserialize api response when a rule has a single condition[/+] on Jan 30, 2026
  2. MarioAlexandruDan commented on Feb 10, 2026

    @MarioAlexandruDan
    Contributor

    Thank you for reporting this bug. I will review it and respond with an appropriate solution shortly.

  3. MarioAlexandruDan commented on Feb 12, 2026

    @MarioAlexandruDan
    Contributor

    The root cause has been identified. We will resolve this in an upcoming pull request.

  4. MarioAlexandruDan commented on Feb 26, 2026

    @MarioAlexandruDan
    Contributor

    The issue has been resolved and the fix will be included in the next release.

    Once the new version is available, please update and verify on your side. If the problem persists after upgrading, feel free to open a new issue and we’ll investigate further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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