FEATURE: Return subcategories on categories endpoint (#14492)

* FEATURE: Return subcategories on categories endpoint

When using the API subcategories will now be returned nested inside of
each category response under the `subcategory_list` param. We already
return all the subcategory ids under the `subcategory_ids` param, but
you then would have to make multiple separate API calls to fetch each of
those subcategories. This way you can get **ALL** of the categories
along with their subcategories in a single API response.

The UI will not be affected by this change because you need to pass in
the `include_subcategories=true` param in order for subcategories to be
returned.

In a follow up PR I'll add the API scoping for fetching categories so
that a readonly API key can be used for the `/categories.json` endpoint. This
endpoint should be used instead of the `/site.json` endpoint for
fetching a sites categories and subcategories.

* Update PR based on feedback

- Have spec check for specific subcategory
- Move comparison check out of loop
- Only populate subcategory list if option present
- Remove empty array initialization
- Update api spec to allow null response

* More PR updates based on feedback

- Use a category serializer for the subcategory_list
- Don't include the subcategory_list param if empty
- For the spec check for the subcategory by id
- Fix spec to account for param not present when empty
This commit is contained in:
Blake Erickson
2021-10-05 12:12:31 -06:00
committed by GitHub
parent 766d337d42
commit fe676f334a
6 changed files with 83 additions and 6 deletions

View File

@ -68,6 +68,55 @@ describe CategoriesController do
)
end
it 'does not returns subcatgories without permission' do
subcategory = Fabricate(:category, user: admin, parent_category: category)
subcategory.set_permissions(admins: :full)
subcategory.save!
sign_in(user)
get "/categories.json?include_subcategories=true"
expect(response.status).to eq(200)
category_list = response.parsed_body["category_list"]
subcategories_for_category = category_list["categories"][1]["subcategory_list"]
expect(subcategories_for_category).to eq(nil)
end
it 'returns the right subcategory response with permission' do
subcategory = Fabricate(:category, user: admin, parent_category: category)
sign_in(user)
get "/categories.json?include_subcategories=true"
expect(response.status).to eq(200)
category_list = response.parsed_body["category_list"]
subcategories_for_category = category_list["categories"][1]["subcategory_list"]
expect(subcategories_for_category.count).to eq(1)
expect(subcategories_for_category.first["parent_category_id"]).to eq(category.id)
expect(subcategories_for_category.first["id"]).to eq(subcategory.id)
end
it 'does not return subcategories without query param' do
subcategory = Fabricate(:category, user: admin, parent_category: category)
sign_in(user)
get "/categories.json"
expect(response.status).to eq(200)
category_list = response.parsed_body["category_list"]
subcategories_for_category = category_list["categories"][1]["subcategory_list"]
expect(subcategories_for_category).to eq(nil)
end
it 'does not show uncategorized unless allow_uncategorized_topics' do
SiteSetting.desktop_category_page_style = "categories_boxes_with_topics"