scribe icon indicating copy to clipboard operation
scribe copied to clipboard

Having a second key in the query parameters is not handled by the WritingUtils

Open minamassoud opened this issue 3 years ago • 2 comments

In Jsonapi we can have query parameters that look like filter[createdAt][lt]=2022-10-12 with

$rules = [
   'filter' => ['array'],
   'filter.createdAt' => ['array:lt,lte,gt,gte,bt'],
   'filter.createdAt.*' => ['date_format:Y-m-d']
];

the following lines are from the WritingUtils.php file https://github.com/knuckleswtf/scribe/blob/377eb64ff6d9c04bf1d19313c28a7ce78538c2df/src/Tools/WritingUtils.php#L117-L120

to handle the above scenario it can be changed to:

// Hash query param (eg filter[name]=john should become "filter[name]": "john")
                    foreach ($value as $item => $itemValue) {
                        $output .= str_repeat(" ", $spacesIndentation);
                        if(is_array($itemValue)) {
                            $key = array_key_first($itemValue);
                            $itemValue = $itemValue[$key];
                            $output .= "$startLinesWith$quote$parameter" . "[$item][$key]$quote$delimiter $quote$itemValue$quote$endLinesWith\n";
                        } else {
                            $output .= "$startLinesWith$quote$parameter" . "[$item]$quote$delimiter $quote$itemValue$quote$endLinesWith\n";
                        }
                    }

minamassoud avatar Oct 12 '22 10:10 minamassoud

its worth noting that I use formRequests for query parameters.

minamassoud avatar Oct 12 '22 10:10 minamassoud

I see your point, and it's doable, but I find it somewhat iffy, because I'm wondering what happens if we have another level of nesting? Can quickly turn into a recursive mess. I'll probably implement this, but I generally advise against using arrays in query parameters.

shalvah avatar Oct 15 '22 19:10 shalvah

I think this is fixed now, by https://github.com/knuckleswtf/scribe/pull/603

shalvah avatar Jan 15 '23 19:01 shalvah