This is an automated email from the ASF dual-hosted git repository. nickva pushed a commit to branch fix-index-info-post-type in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit 63ab5f6f8907d1e6c904278b6627a3702765341e Author: Nick Vatamaniuc <[email protected]> AuthorDate: Fri Nov 13 13:51:37 2026 -0500 Fix types parameter for /_index_info For POSTs make sure to accept an array of strings in only in the body. As a query parameter it is still an array of strings. For consistency with other endpoints and parameters if both are provided, pick the query param. Fix: #6216 --- src/chttpd/src/chttpd_db.erl | 74 +++++++++++++++++++------- src/docs/src/api/database/index_info.rst | 10 +++- test/elixir/test/config/suite.elixir | 3 +- test/elixir/test/index_info_test.exs | 90 +++++++++++++++++++++++++++++--- 4 files changed, 150 insertions(+), 27 deletions(-) diff --git a/src/chttpd/src/chttpd_db.erl b/src/chttpd/src/chttpd_db.erl index 6c29c83f0..4332a9274 100644 --- a/src/chttpd/src/chttpd_db.erl +++ b/src/chttpd/src/chttpd_db.erl @@ -536,19 +536,21 @@ handle_design_info_req(Req, _Db, _DDoc) -> % _design_doc paging parameters: startkey, endkey, limit, etc. handle_index_info_req(#httpd{method = 'GET', path_parts = [_, _]} = Req, Db) -> Args = couch_mrview_http:parse_params(Req, undefined), - index_info_req(Req, Db, Args); + Types = parse_index_info_types(chttpd:qs(Req), []), + index_info_req(Req, Db, Types, Args); handle_index_info_req(#httpd{method = 'POST', path_parts = [_, _]} = Req, Db) -> chttpd:validate_ctype(Req, "application/json"), - Keys = couch_mrview_util:get_view_keys(chttpd:json_body_obj(Req)), - Args = couch_mrview_http:parse_body_and_query(Req, Keys), - index_info_req(Req, Db, Args); + {Props} = Body = chttpd:json_body_obj(Req), + Keys = couch_mrview_util:get_view_keys(Body), + Args = couch_mrview_http:parse_body_and_query(Req, Body, Keys), + Types = parse_index_info_types(chttpd:qs(Req), Props), + index_info_req(Req, Db, Types, Args); handle_index_info_req(#httpd{path_parts = [_, _]} = Req, _Db) -> send_method_not_allowed(Req, "GET,HEAD,POST"); handle_index_info_req(Req, _Db) -> chttpd:send_error(Req, not_found). -index_info_req(Req, Db, #mrargs{keys = Keys} = Args) -> - Types = parse_types(Req), +index_info_req(Req, Db, Types, #mrargs{keys = Keys} = Args) -> Default = ?MAX_DDOC_NUM_FOR_INDEX_INFO, Max = config:get_integer("chttpd", "max_ddoc_number_for_index_info_req", Default), case is_list(Keys) andalso length(Keys) > Max of @@ -575,18 +577,34 @@ meta_to_json(Meta) -> {bookmark, couch_util:get_value(bookmark, Meta, null)} ]. -% Types for _index_info. Must be one or more of view, search, nouveau -parse_types(Req) -> - TypeArgs = [V || {K, V} <- chttpd:qs(Req), K == "type"], - case TypeArgs of - [] -> - [view, search, nouveau]; - [_ | _] -> - Tokens = lists:flatmap(fun(V) -> string:tokens(V, ",") end, TypeArgs), - case lists:usort(lists:map(fun parse_type/1, Tokens)) of - [] -> throw({query_parse_error, ~"`type` must not be empty"}); - Types -> [T || T <- [view, search, nouveau], lists:member(T, Types)] - end +% Types for _index_info, view, search or nouveau. Specifying none, means all +parse_index_info_types(Qs, BodyProps) -> + BodyType = couch_util:get_value(~"type", BodyProps), + BodyTypes = to_types(body_types(BodyType), [view, search, nouveau]), + to_types(qs_types(Qs), BodyTypes). + +% Split query param types by comma +qs_types(Qs) -> + case [V || {K, V} <- Qs, K == "type"] of + [] -> undefined; + [_ | _] = Vals -> lists:flatmap(fun(V) -> string:tokens(V, ",") end, Vals) + end. + +% For POST request body, types should be a list +body_types(undefined) -> + undefined; +body_types(Types) -> + case is_list(Types) andalso lists:all(fun is_binary/1, Types) of + true -> lists:map(fun binary_to_list/1, Types); + false -> throw({query_parse_error, ~"`type` must be an array"}) + end. + +to_types(undefined, Default) -> + Default; +to_types(Tokens, _Default) -> + case lists:usort(lists:map(fun parse_type/1, Tokens)) of + [] -> throw({query_parse_error, ~"`type` must not be empty"}); + Types -> [T || T <- [view, search, nouveau], lists:member(T, Types)] end. parse_type("view") -> @@ -2917,6 +2935,26 @@ index_info_type_filter_test_() -> index_info_to_json(ByType) ). +index_info_parse_types_test_() -> + All = [view, search, nouveau], + % {Types, QueryString, BodyProps} + Cases = [ + {All, [], []}, + {All, [{"other", "1"}], [{~"other", 1}]}, + % query param + {[search], [{"type", "search"}], []}, + {[view, nouveau], [{"type", "nouveau,view"}], []}, + {[view, search], [{"type", "search"}, {"type", "view,search"}], []}, + % in the body + {[search], [], [{~"type", [~"search"]}]}, + {[view, nouveau], [], [{~"type", [~"nouveau", ~"view", ~"nouveau"]}]}, + {All, [], [{~"type", [~"search", ~"nouveau", ~"view"]}]}, + % query param wins (for consistency with the rest of the apis) + {[view], [{"type", "view"}], [{~"type", [~"search"]}]}, + {[search, nouveau], [{"type", "nouveau,search"}], [{~"type", [~"view"]}]} + ], + [?_assertEqual(Types, parse_index_info_types(Qs, Body)) || {Types, Qs, Body} <- Cases]. + parse_partitioned_opt_test_() -> { foreach, diff --git a/src/docs/src/api/database/index_info.rst b/src/docs/src/api/database/index_info.rst index 9d17abc04..53282e0db 100644 --- a/src/docs/src/api/database/index_info.rst +++ b/src/docs/src/api/database/index_info.rst @@ -261,7 +261,10 @@ :<header Content-Type: :mimetype:`application/json` :<json array keys: Doc IDs of the design docs to report, for example ``["_design/cookbook"]``. Other :get:`/{db}/_index_info` - parameters can provided in the body as well. *Optional* + parameters can be provided in the body as well. *Optional* + :<json array type: Filter by type. An array with one or more of: ``view``, + ``search`` and ``nouveau``, for example ``["view", + "search"]``. *Optional*, default: all types. :>header Content-Type: - :mimetype:`application/json` - :mimetype:`text/plain; charset=utf-8` :>json number total_rows: Number of design docs in the database. Same as @@ -282,7 +285,7 @@ .. code-block:: http - POST /recipes/_index_info?type=search HTTP/1.1 + POST /recipes/_index_info HTTP/1.1 Accept: application/json Content-Type: application/json Host: localhost:5984 @@ -290,6 +293,9 @@ { "keys": [ "_design/cookbook" + ], + "type": [ + "search" ] } diff --git a/test/elixir/test/config/suite.elixir b/test/elixir/test/config/suite.elixir index 135be31e4..d5b5c0d10 100644 --- a/test/elixir/test/config/suite.elixir +++ b/test/elixir/test/config/suite.elixir @@ -245,7 +245,8 @@ "index info lists every index with its per index info object", "index info pages design docs like _design_docs", "index info reports mango indexes by the ddoc and name _index uses", - "index info takes keys like _design_docs and caps the design docs" + "index info takes keys like _design_docs and caps the design docs", + "index info type in the body or query params" ], "InvalidDocIDsTest": [ "_local-prefixed ids are illegal", diff --git a/test/elixir/test/index_info_test.exs b/test/elixir/test/index_info_test.exs index 7c59b48d8..60a300636 100644 --- a/test/elixir/test/index_info_test.exs +++ b/test/elixir/test/index_info_test.exs @@ -35,6 +35,13 @@ defmodule IndexInfoTest do } } + # Settings to make search and nouveau unavailable. Their indexes are still + # listed, with an error instead of the info object. + @unavailable [ + %{section: "nouveau", key: "enable", value: "false"}, + %{section: "dreyfus", key: "name", value: "[email protected]"} + ] + defp index_info(db_name, query \\ []) do resp = Couch.get("/#{db_name}/_index_info", query: query) @@ -46,6 +53,12 @@ defmodule IndexInfoTest do defp view_group_ddocs(body), do: Enum.map(body["view_indexes"], & &1["ddoc"]) + # how many view, search and nouveau entries we got + defp counts(body) do + lists = ["view_indexes", "search_indexes", "nouveau_indexes"] + Enum.map(lists, &length(body[&1])) + end + defp view_group(body, ddoc) do [entry] = Enum.filter(body["view_indexes"], &(&1["ddoc"] == ddoc)) entry @@ -84,12 +97,7 @@ defmodule IndexInfoTest do copies = shard_copies(db_name) # These are not available, we want to test that scenario - unavailable = [ - %{section: "nouveau", key: "enable", value: "false"}, - %{section: "dreyfus", key: "name", value: "[email protected]"} - ] - - run_on_modified_server(unavailable, fn -> + run_on_modified_server(@unavailable, fn -> body = index_info(db_name) assert Enum.sort(Map.keys(body)) == @keys # two design docs, all returned, no next page => bookmark is null @@ -387,4 +395,74 @@ defmodule IndexInfoTest do assert view_group_ddocs(resp.body) == ["_design/a", "_design/c"] end) end + + @tag :with_db + test "index info type in the body or query params", context do + db_name = context[:db_name] + {:ok, _} = create_doc(db_name, @cookbook) + {:ok, _} = create_doc(db_name, ddoc_with_view("_design/zzz")) + + run_on_modified_server(@unavailable, fn -> + # 2 views, 1 search and 1 nouveau index + assert counts(index_info(db_name)) == [2, 1, 1] + assert counts(index_info(db_name, type: "search")) == [0, 1, 0] + assert counts(index_info(db_name, type: "view,nouveau")) == [2, 0, 1] + + # for bodies we expect a list + for {type, expected} <- [ + {["view"], [2, 0, 0]}, + {["nouveau", "view"], [2, 0, 1]}, + {["view", "search", "nouveau", "view"], [2, 1, 1]} + ] do + resp = Couch.post("/#{db_name}/_index_info", body: %{type: type}) + assert resp.status_code == 200, "type #{inspect(type)}: #{inspect(resp.body)}" + assert Enum.sort(Map.keys(resp.body)) == @keys + assert counts(resp.body) == expected, "type #{inspect(type)}" + end + + # along with the other parameters in the body + body = %{type: ["search"], keys: ["_design/cookbook", "_design/zzz"]} + resp = Couch.post("/#{db_name}/_index_info", body: body) + assert resp.status_code == 200 + assert counts(resp.body) == [0, 1, 0] + [search] = resp.body["search_indexes"] + assert {search["ddoc"], search["name"]} == {"_design/cookbook", "strings"} + + # query wins if we got both + query = [type: "view", limit: 1] + body = %{type: ["search"], limit: 2} + resp = Couch.post("/#{db_name}/_index_info", query: query, body: body) + assert resp.status_code == 200 + assert counts(resp.body) == [1, 0, 0] + assert view_group_ddocs(resp.body) == ["_design/cookbook"] + end) + + invalid = "Invalid type: foo. Must be view, search or nouveau" + not_a_list = "`type` must be an array" + + for {type, reason} <- [ + {["foo"], invalid}, + {["view", "foo"], invalid}, + {[], "`type` must not be empty"}, + # the comma separated string of the query parameter is not a list + {"search", not_a_list}, + {"view,search", not_a_list}, + {"", not_a_list}, + {1, not_a_list}, + {nil, not_a_list}, + {["view", 1], not_a_list}, + {%{view: true}, not_a_list} + ] do + resp = Couch.post("/#{db_name}/_index_info", body: %{type: type}) + assert resp.status_code == 400, "type #{inspect(type)}: #{inspect(resp.body)}" + assert resp.body["error"] == "query_parse_error" + assert resp.body["reason"] == reason + end + + # an invalid type in the body is an error even with a valid one in the query + body = %{type: ["foo"]} + resp = Couch.post("/#{db_name}/_index_info", query: [type: "view"], body: body) + assert resp.status_code == 400 + assert resp.body["reason"] == invalid + end end
