BinarCode / laravel-restify

The fastest way to make a powerful JSON:API compatible Rest API with Laravel.

Home Page:https://restify.binarcode.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Boolean Match not triggered when falsy value is sent

arthurkirkosa opened this issue · comments

I have the following setup

// Repository
public static function matches(): array
{
    return [
        ...
        'has_direct_deposits' => HasDirectDepositsMatch::make()
                ->setType('boolean'),
        ...
    ];
}
// HavingDeductionRatesMatch
<?php

use Binaryk\LaravelRestify\Filters\MatchFilter;
use Illuminate\Http\Request;

class HasDirectDepositsMatch extends MatchFilter
{
    public function filter(Request $request, $query, $value)
    {
        dump('x', $request->all());
        if ($request->boolean('has_direct_deposits')) {
            $query->has('directDeposits');
        } else {
            $query->doesntHave('directDeposits');
        }
    }
}
// Repository Helper
public static function route(
    string|Model $path = null,
    Action $action = null,
    array $query = [],
): string {
    if ($path instanceof Model) {
        $path = $path->getKey();
    }

    if ($action) {
        $query['action'] = $action->uriKey();
    }

    $route = implode('/', array_filter([
        Restify::path(),
        static::uriKey(),
        $path,
        $action ? 'actions' : null,
    ]));

    if (empty($query)) {
        return $route;
    }

    return $route.'?'.http_build_query($query);
}

The query is generated using http_build_query method and converts bools to ints

Test

$this->getJson(EmployeeRepository::route(query: ['has_direct_deposits' => true]));
// Route is /api/restify/employees?has_direct_deposits=1
// Works as expected, the match is triggered

// Dump
^ "x"
^ array:1 [
  "has_direct_deposits" => "1"
]
$this->getJson(EmployeeRepository::route(query: ['has_direct_deposits' => false]));
// Route is /api/restify/employees?has_direct_deposits=0
// The match is not being triggered by Restify

// Nothing is dumped
$this->getJson(EmployeeRepository::route(query: ['has_direct_deposits' => 'false']));
// Route is /api/restify/employees?has_direct_deposits=false
// Works as expected, the match is triggered

// Dump
^ "x"
^ array:1 [
  "has_direct_deposits" => "false"
]

Considering this is a custom match class, you should be able to convert the request value to what you need, don't you?

The match class is not being instantiated at all (see case #2 from Tests above) which leads me to believe Restify is filtering it out some how