autocomplete: fix accidental API breakage.

Fix /autocomplete.json returning results like this:

    [
      {
        "table": {
          "type": "tag-word",
          "label": "long hair",
          "value": "long_hair",
          "category": 0,
          "post_count": 2818211,
          "antecedent": null
        }
      },
    ]

instead of like this:

    [
      {
        "type": "tag-word",
        "label": "long hair",
        "value": "long_hair",
        "category": 0,
        "post_count": 2818211,
      },
    ]

Also change it so that optional attributes like `antecedent` aren't
returned if they're null. This could be a minor breaking change if users
rely on these attributes always being present.

Lastly change XML results to look like this:

    <autocomplete-service-results type="array">
      <autocomplete-service-result>
        <type>tag-word</type>
        <label>long hair</label>
        <value>long_hair</value>
        <category type="integer">0</category>
        <post-count type="integer">2801117</post-count>
      </autocomplete-service-result>
    </autocomplete-service-results>

instead of like this:

    <objects type="array">
      <object>
        <type>tag</type>
        <label>hair ornament</label>
        <value>hair_ornament</value>
        <category type="integer">0</category>
        <post-count type="integer">883340</post-count>
        <antecedent nil="true"/>
      </object>
    </objects>
This commit is contained in:
evazion
2022-09-03 18:45:09 -05:00
parent 0274dbde10
commit 03c02ef78e

View File

@@ -31,6 +31,17 @@ class AutocompleteService
attr_reader :query, :type, :limit, :current_user, :enabled
alias_method :enabled?, :enabled
# A Result represents a single autocomplete entry. `label` is the pretty name to
# display in the autocomplete menu and `value` is the actual string to insert.
class Result < Struct.new(:type, :label, :value, :category, :post_count, :id, :level, :antecedent, keyword_init: true)
include ActiveModel::Serializers::JSON
include ActiveModel::Serializers::Xml
def serializable_hash(...)
to_h.compact_blank
end
end
# Perform completion for the given search type and query.
# @param query [String] the string being completed
# @param type [String] the type of completion being performed
@@ -71,7 +82,7 @@ class AutocompleteService
autocomplete_saved_search_label(query)
else
[]
end.map { |result| OpenStruct.new(result) }
end.map { |result| Result.new(result) }
end
# Complete a tag search (a regular tag or a metatag)