Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Handle more than one value for a url query parameter (#1889)
Olga Bulat92ea64c6d4ac
diff --git a/frontend/src/stores/search.ts b/frontend/src/stores/search.tsindex 40b587398..c812309ce 100644--- a/frontend/src/stores/search.ts+++ b/frontend/src/stores/search.ts@@ -21,7 +21,7 @@ import {import {ApiQueryParams,filtersToQueryData,- qToSearchTerm,+ queryDictionaryToQueryParams,queryStringToSearchType,queryToFilterData,} from "~/utils/search-query-transform"@@ -224,8 +224,8 @@ export const useSearchStore = defineStore("search", {* use the first one.* @param q - The URL `q` query parameter*/- setSearchTerm(q: string | (null | string)[] | null) {- const formattedTerm = qToSearchTerm(q)+ setSearchTerm(q: string | undefined | null) {+ const formattedTerm = q ? q.trim() : ""if (this.searchTerm === formattedTerm) returnthis.searchTerm = formattedTermthis.localSearchTerm = formattedTerm@@ -401,12 +401,13 @@ export const useSearchStore = defineStore("search", {path: stringurlQuery: Context["query"]}) {- this.setSearchTerm(urlQuery.q)+ const query = queryDictionaryToQueryParams(urlQuery)+ this.setSearchTerm(query.q)this.searchType = queryStringToSearchType(path)if (!isSearchTypeSupported(this.searchType)) return+// When setting filters from URL query, 'mature' has a value of 'true',// but we need the 'mature' code. Creating a local shallow copy to prevent mutation.- const query: Record<string, string> = { ...urlQuery, q: this.searchTerm }if (query.mature === "true") {query.mature = "mature"} else {diff --git a/frontend/src/utils/search-query-transform.ts b/frontend/src/utils/search-query-transform.tsindex 16ea5e255..b258e5bf7 100644--- a/frontend/src/utils/search-query-transform.ts+++ b/frontend/src/utils/search-query-transform.ts@@ -14,6 +14,9 @@ import {import { getParameterByName } from "~/utils/url-params"import { deepClone } from "~/utils/clone"+import type { Context } from "@nuxt/types"+import type { Dictionary } from "vue-router/types/router"+export interface ApiQueryParams {q?: stringlicense?: string@@ -166,7 +169,7 @@ export const queryToFilterData = ({searchType = "image",defaultFilters,}: {- query: Record<string, string>+ query: Dictionary<string>searchType: SupportedSearchTypedefaultFilters: Partial<Filters>}) => {@@ -262,11 +265,23 @@ export const areQueriesEqual = (}/**- * Converts the API query `q` parameter to a single string that can- * be used as a search term. Use the first item if `q` is an array.- * @param q - the URL `q` parameter+ * The URL query string can contain multiple values for the same parameter.+ * This function converts the query string to a dictionary where the values+ * are always strings. If the parameter has multiple values, the first value+ * is used.+ * * @param queryDictionary - the query param dictionary provided by Vue router*/-export const qToSearchTerm = (q: string | null | (string | null)[]) => {- const searchTerm = Array.isArray(q) ? q[0] : q- return searchTerm ? searchTerm.trim() : ""+export const queryDictionaryToQueryParams = (+ queryDictionary: Context["query"]+): Dictionary<string> => {+ const queryParams = {} as Dictionary<string>+ Object.keys(queryDictionary).forEach((key) => {+ const value = queryDictionary[key]+ // If the parameter is an array, use the first value.+ const parameter = Array.isArray(value) ? value[0] : value+ if (parameter) {+ queryParams[key] = parameter+ }+ })+ return queryParams}diff --git a/frontend/test/unit/specs/stores/search-store.spec.js b/frontend/test/unit/specs/stores/search-store.spec.jsindex d8d8d0c74..f69c6f2ac 100644--- a/frontend/test/unit/specs/stores/search-store.spec.js+++ b/frontend/test/unit/specs/stores/search-store.spec.js@@ -9,6 +9,7 @@ import {ALL_MEDIA,AUDIO,IMAGE,+ searchPath,supportedSearchTypes,VIDEO,} from "~/constants/media"@@ -124,40 +125,32 @@ describe("Search Store", () => {* - media specific filters that are unique (durations).* - media specific filters that have the same API param (extensions)* - no 'q' parameter in the query.+ * - more than one value for a parameter in the query (q=cat&q=dog).*/it.each`- query | searchType- ${{ q: "cat", license: "by", mature: "true" }} | ${IMAGE}- ${{ license: "by", mature: "true" }} | ${IMAGE}- ${{ license: "", mature: "" }} | ${IMAGE}- ${{ q: "cat", license: "by", searchBy: "creator" }} | ${ALL_MEDIA}- ${{ q: "cat", license: "pdm,cc0,by,by-nc" }} | ${ALL_MEDIA}- ${{ q: "cat", length: "medium" }} | ${AUDIO}- ${{ q: "cat", extension: "svg" }} | ${IMAGE}- ${{ q: "cat", extension: "svg" }} | ${AUDIO}- ${{ q: "cat", extension: "mp3" }} | ${AUDIO}+ query | expectedQueryParams | searchType+ ${{ q: "cat", license: "by", mature: "true" }} | ${{ q: "cat", license: "by", mature: "true" }} | ${IMAGE}+ ${{ license: "by", mature: "true" }} | ${{ q: "", license: "by", mature: "true" }} | ${IMAGE}+ ${{ license: "", mature: "" }} | ${{ q: "" }} | ${IMAGE}+ ${{ q: "cat", license: "by", searchBy: "creator" }} | ${{ q: "cat", license: "by", searchBy: "creator" }} | ${ALL_MEDIA}+ ${{ q: "cat", license: "pdm,cc0,by,by-nc" }} | ${{ q: "cat", license: "pdm,cc0,by,by-nc" }} | ${ALL_MEDIA}+ ${{ q: "cat", length: "medium" }} | ${{ q: "cat" }} | ${IMAGE}+ ${{ q: "cat", length: "medium" }} | ${{ q: "cat", length: "medium" }} | ${AUDIO}+ ${{ q: "cat", extension: "svg" }} | ${{ q: "cat", extension: "svg" }} | ${IMAGE}+ ${{ q: "cat", extension: "mp3" }} | ${{ q: "cat", extension: "mp3" }} | ${AUDIO}+ ${{ q: "cat", extension: "svg" }} | ${{ q: "cat" }} | ${AUDIO}+ ${{ q: ["cat", "dog"], license: ["by", "cc0"] }} | ${{ q: "cat", license: "by" }} | ${IMAGE}`("returns correct searchQueryParams and filter status for $query and searchType $searchType",- ({ query, searchType }) => {+ ({ query, expectedQueryParams, searchType }) => {const searchStore = useSearchStore()- const expectedQueryParams = query// It should discard the values that are not applicable for the search type:- if (searchType === AUDIO && query.extension === "svg") {- delete expectedQueryParams.extension- }+searchStore.setSearchStateFromUrl({- path: `/search/${searchType === ALL_MEDIA ? "" : searchType}`,- urlQuery: { ...expectedQueryParams },+ path: searchPath(searchType),+ urlQuery: query,})- // Edge-case: query parameter value is a blank string- for (let param in expectedQueryParams) {- if (expectedQueryParams[param] === "")- delete expectedQueryParams[param]- }- // Should add a blank string as `q` value if `q` is not in query- if (!("q" in expectedQueryParams)) {- expectedQueryParams.q = ""- }+expect(searchStore.searchQueryParams).toEqual(expectedQueryParams)})