Skip to content

Commit

Permalink
Merge pull request #473 from jikan-me/hotfix/empty_query_params
Browse files Browse the repository at this point in the history
🚑 Fixed issue with empty query params causing errors
  • Loading branch information
pushrbx authored Jan 20, 2024
2 parents 84fb57a + e2b024c commit 84da394
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions app/Dto/Concerns/PreparesData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
namespace App\Dto\Concerns;

use Illuminate\Support\Collection;
use Illuminate\Support\Env;
use Illuminate\Support\Facades\App;
use \ReflectionClass;
use Spatie\LaravelData\Support\DataConfig;
use Spatie\LaravelData\Support\DataProperty;

/**
* A trait for preparing the incoming data before passing it through the data pipeline.
Expand All @@ -29,22 +27,33 @@ public static function prepareForPipeline(Collection $properties): Collection
// collection, and if they are present have such a value, convert them.
$dataClass = app(DataConfig::class)->getDataClass(static::class);
foreach ($dataClass->properties as $property) {
if (!$property->type->acceptsType("bool")) {
continue;
}
/**
* @var DataProperty $property
*/
// the name can be different in the $properties variable, so let's check if there is an input name mapping
// for the property and use that instead if present.
$propertyRawName = $property->inputMappedName ?? $property->name;
if ($properties->has($propertyRawName)) {
$propertyVal = $properties->get($propertyRawName);
if ($propertyVal === "true") {
$propertyVal = true;
if ($property->type->acceptsType("bool")) {
if ($propertyVal === "true") {
$propertyVal = true;
}
if ($propertyVal === "false") {
$propertyVal = false;
}
}
if ($propertyVal === "false") {
$propertyVal = false;
// if the property is optional and the value is an empty string, we want to ignore it.
if ($property->type->isOptional && $propertyVal === "") {
$propertyVal = null;
}
$properties->put($propertyRawName, $propertyVal);
}

if (!is_null($propertyVal)) {
$properties->put($propertyRawName, $propertyVal);
} else {
$properties->forget($propertyRawName);
}
}
}

return $properties;
Expand Down

0 comments on commit 84da394

Please sign in to comment.