Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .claude/skills/migrate-from-v2-to-v3/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ the table in `UPGRADE-3.0.md`. Look at where the data comes from:
second becomes `false` (it was `true`).
- **JSON APIs:** `1.0` into `int` still works; `1.5` throws.
- **Database rows / internal arrays:** usually already typed.
- **Union types** (`int|string`, `float|int`): no longer coerced at all;
`true` or `1.5` into `int|string` now throws a `TypeError`.

If a property legitimately receives lossy input, **STOP AND ASK** before
choosing between retyping it (`float`), adding a cast (`IntegerCast`,
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ and the project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.
validation error under `'$pipeline'` (class pipe) or the property's key.
`#[RejectUnknownKeys]` still checks the raw input; `from()` is unchanged
(#23).
- A property typed `int`, `float`, `string` or `bool` without a cast takes
only values that convert without loss (`"3"` and `3.0` into `int`,
`"false"` into `bool` as `false`); anything else throws
`DataHydrationException` naming the field. Union types are checked in
strict mode. In 2.x PHP's weak coercion applied (`"1.9"` became `1`,
`"false"` became `true`) (#24).
- A non-null value that matches no case of a nullable enum property throws
`DataHydrationException` (`tryFrom()` returns `null`, `fromResult()`
reports the field) instead of becoming `null`. `#[Cast(new EnumCast(...))]`
Expand Down
14 changes: 10 additions & 4 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,20 @@ takes only values that convert without loss:
|---|---|---|
| `int` | int, a float with no fraction, a string of digits with an optional `-` within range | `"1.9"`, `1.9`, `true`, `"abc"` |
| `float` | int, float, a numeric string | `true`, `"abc"` |
| `string` | string, int, float | `true`, arrays, objects |
| `string` | string, int, float, `Stringable` | `true`, arrays, other objects |
| `bool` | bool, and `"1"`, `"0"`, `"true"`, `"false"`, `"on"`, `"off"`, `"yes"`, `"no"`, `""`, `1`, `0` | `"maybe"`, `2` |

Anything else throws `DataHydrationException` naming the field; `tryFrom()`
returns `null` and `fromResult()` reports it under the field's key.
Anything else throws `DataHydrationException` naming the field ("Invalid
value for field 'age': Cannot convert string to int without loss."); `tryFrom()`
returns `null` and `fromResult()` reports it under the field's key. `with()`
applies the same rules.

Union types (`int|string`) are not coerced: PHP checks them in strict mode,
so `int|string` given `1.5` or `true` throws a `TypeError`.

**Affected:** inputs that relied on lossy coercion: decimals into `int`,
`"false"`/`"no"` strings into `bool` (now `false`, previously `true`).
`"false"`/`"no"` strings into `bool` (now `false`, previously `true`), and
values PHP coerced into a union type.

**What to do:** type the property as `float`, add a cast (`IntegerCast`,
`BooleanCast`), or fix the input.
Expand Down
20 changes: 20 additions & 0 deletions docs/features/hydration.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,26 @@ UserData::from(['name' => 'Alice', 'email' => 'alice@example.com']);
// phone = null, age = 0
```

## Scalar Types

A property typed `int`, `float`, `string` or `bool` (nullable or not) without a cast takes a value of another scalar type only when it converts without loss:

```php
ProductData::from(['quantity' => '3']); // 3
ProductData::from(['quantity' => 3.0]); // 3
ProductData::from(['quantity' => '3.5']); // DataHydrationException:
// Invalid value for field 'quantity': Cannot convert string to int without loss.
```

| Type | Accepted | Rejected |
|---|---|---|
| `int` | int, a float with no fraction, a string of digits with an optional `-` within range | `"1.9"`, `1.9`, `true`, `"abc"` |
| `float` | int, float, a finite numeric string | `true`, `"abc"` |
| `string` | string, int, float, `Stringable` | `true`, arrays, other objects |
| `bool` | bool, and `"1"`, `"0"`, `"true"`, `"false"`, `"on"`, `"off"`, `"yes"`, `"no"`, `""`, `1`, `0` | `"maybe"`, `2` |

Values of the declared type pass straight to the constructor; conversion runs only when PHP rejects one. Union types (`int|string`) are checked by PHP in strict mode and not converted. For any other conversion, use a [cast](../attributes/cast.md) such as `IntegerCast` or `BooleanCast`.

## Missing Required Fields

If a required field (non-nullable, no default) is missing, `DataHydrationException` is thrown:
Expand Down
2 changes: 1 addition & 1 deletion src/BaseData.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*/
public static function from(#[\SensitiveParameter] mixed $data): static
{
$hydrate = HydratorCompiler::$hydrators[static::class] ?? HydratorCompiler::compile(static::class);

Check warning on line 45 in src/BaseData.php

View workflow job for this annotation

GitHub Actions / Mutation testing

Escaped Mutant for Mutator "Coalesce": @@ @@ */ public static function from(#[\SensitiveParameter] mixed $data): static { - $hydrate = HydratorCompiler::$hydrators[static::class] ?? HydratorCompiler::compile(static::class); + $hydrate = HydratorCompiler::compile(static::class) ?? HydratorCompiler::$hydrators[static::class]; if (is_array($data)) { /** @var static */

if (is_array($data)) {
/** @var static */
Expand All @@ -66,7 +66,7 @@
public static function fromLazy(#[\SensitiveParameter] mixed $data): static
{
$class = static::class;
$reflector = self::$reflectors[$class] ??= new \ReflectionClass($class);

Check warning on line 69 in src/BaseData.php

View workflow job for this annotation

GitHub Actions / Mutation testing

Escaped Mutant for Mutator "AssignCoalesce": @@ @@ public static function fromLazy(#[\SensitiveParameter] mixed $data): static { $class = static::class; - $reflector = self::$reflectors[$class] ??= new \ReflectionClass($class); + $reflector = (self::$reflectors[$class] = new \ReflectionClass($class)); // A ghost's class is fixed at creation, so a #[Discriminator] class // must resolve the concrete class eagerly; hydration stays deferred.

// A ghost's class is fixed at creation, so a #[Discriminator] class
// must resolve the concrete class eagerly; hydration stays deferred.
Expand All @@ -89,12 +89,12 @@
$meta = MetadataRegistry::get($class);

if (! $meta->hasConstructor) {
(HydratorCompiler::$populators[$class] ?? HydratorCompiler::compilePopulate($class))($normalized, $ghost);

Check warning on line 92 in src/BaseData.php

View workflow job for this annotation

GitHub Actions / Mutation testing

Escaped Mutant for Mutator "Coalesce": @@ @@ $meta = MetadataRegistry::get($class); if (! $meta->hasConstructor) { - (HydratorCompiler::$populators[$class] ?? HydratorCompiler::compilePopulate($class))($normalized, $ghost); + (HydratorCompiler::compilePopulate($class) ?? HydratorCompiler::$populators[$class])($normalized, $ghost); return; }

return;
}

$args = (HydratorCompiler::$argResolvers[$class] ?? HydratorCompiler::compileArgs($class))($normalized);

Check warning on line 97 in src/BaseData.php

View workflow job for this annotation

GitHub Actions / Mutation testing

Escaped Mutant for Mutator "Coalesce": @@ @@ return; } - $args = (HydratorCompiler::$argResolvers[$class] ?? HydratorCompiler::compileArgs($class))($normalized); + $args = (HydratorCompiler::compileArgs($class) ?? HydratorCompiler::$argResolvers[$class])($normalized); try { $ghost->__construct(...$args);

try {
$ghost->__construct(...$args);
Expand All @@ -106,7 +106,7 @@
// extra properties still need populating, same mechanism as the
// constructor-less path above.
if ($meta->hasExtraProperties) {
(HydratorCompiler::$populators[$class] ?? HydratorCompiler::compilePopulate($class))($normalized, $ghost);

Check warning on line 109 in src/BaseData.php

View workflow job for this annotation

GitHub Actions / Mutation testing

Escaped Mutant for Mutator "Coalesce": @@ @@ // extra properties still need populating, same mechanism as the // constructor-less path above. if ($meta->hasExtraProperties) { - (HydratorCompiler::$populators[$class] ?? HydratorCompiler::compilePopulate($class))($normalized, $ghost); + (HydratorCompiler::compilePopulate($class) ?? HydratorCompiler::$populators[$class])($normalized, $ghost); } }); }
}
});
}
Expand All @@ -128,7 +128,7 @@
*/
public static function fromResult(#[\SensitiveParameter] mixed $data): HydrationResult
{
$collect = HydratorCompiler::$collectingHydrators[static::class] ?? HydratorCompiler::compileCollecting(static::class);

Check warning on line 131 in src/BaseData.php

View workflow job for this annotation

GitHub Actions / Mutation testing

Escaped Mutant for Mutator "Coalesce": @@ @@ */ public static function fromResult(#[\SensitiveParameter] mixed $data): HydrationResult { - $collect = HydratorCompiler::$collectingHydrators[static::class] ?? HydratorCompiler::compileCollecting(static::class); + $collect = HydratorCompiler::compileCollecting(static::class) ?? HydratorCompiler::$collectingHydrators[static::class]; if (is_array($data)) { /** @var HydrationResult<static> */

if (is_array($data)) {
/** @var HydrationResult<static> */
Expand All @@ -139,7 +139,7 @@
return HydrationResult::success($data);
}

try {

Check warning on line 142 in src/BaseData.php

View workflow job for this annotation

GitHub Actions / Mutation testing

Escaped Mutant for Mutator "CatchBlockRemoval": @@ @@ try { $data = InputNormalizer::normalize(static::class, $data); - } catch (DataHydrationException $e) { - return HydrationResult::failure(['$input' => $e->getMessage()]); } catch (\Throwable) { return HydrationResult::failure(['$input' => 'Input could not be normalized.']); }
$data = InputNormalizer::normalize(static::class, $data);
} catch (DataHydrationException $e) {
return HydrationResult::failure(['$input' => $e->getMessage()]);
Expand Down Expand Up @@ -180,7 +180,7 @@
$class = static::class;

return LazyCollection::make(static function () use ($items, $class): \Generator {
$hydrate = HydratorCompiler::$hydrators[$class] ?? HydratorCompiler::compile($class);

Check warning on line 183 in src/BaseData.php

View workflow job for this annotation

GitHub Actions / Mutation testing

Escaped Mutant for Mutator "Coalesce": @@ @@ $class = static::class; return LazyCollection::make(static function () use ($items, $class): \Generator { - $hydrate = HydratorCompiler::$hydrators[$class] ?? HydratorCompiler::compile($class); + $hydrate = HydratorCompiler::compile($class) ?? HydratorCompiler::$hydrators[$class]; foreach ($items as $item) { yield $item instanceof $class

foreach ($items as $item) {
yield $item instanceof $class
Expand All @@ -201,7 +201,7 @@
$class = static::class;

return LazyCollection::make(static function () use ($class, $uri, $path): \Generator {
$hydrate = HydratorCompiler::$hydrators[$class] ?? HydratorCompiler::compile($class);

Check warning on line 204 in src/BaseData.php

View workflow job for this annotation

GitHub Actions / Mutation testing

Escaped Mutant for Mutator "Coalesce": @@ @@ $class = static::class; return LazyCollection::make(static function () use ($class, $uri, $path): \Generator { - $hydrate = HydratorCompiler::$hydrators[$class] ?? HydratorCompiler::compile($class); + $hydrate = HydratorCompiler::compile($class) ?? HydratorCompiler::$hydrators[$class]; foreach (XmlStream::read($class, $uri, $path) as $row) { yield $hydrate($row);

foreach (XmlStream::read($class, $uri, $path) as $row) {
yield $hydrate($row);
Expand All @@ -226,7 +226,7 @@
if (array_key_exists($param->phpName, $overrides)) {
$value = $overrides[$param->phpName];
$value = match (true) {
$param->isPlain => $value,
$param->isPlain && $param->scalarType === null => $value,
$param->isSensitive => ValueCaster::castSensitive($param, $value, static::class),
default => ValueCaster::cast($param, $value),
};
Expand Down
10 changes: 10 additions & 0 deletions src/Support/ClassMetaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,19 @@ enumClass: $enumClass,
phpType: RuleInferrer::phpTypeName($parameter),
xmlSource: self::xmlSource($parameter, $phpName, (string) $inputNames[0]),
isSensitive: $parameter->getAttributes(Sensitive::class) !== [],
scalarType: self::scalarType($parameter),
);
}

private static function scalarType(ReflectionParameter|ReflectionProperty $parameter): ?string
{
$type = $parameter->getType();

return $type instanceof \ReflectionNamedType && in_array($type->getName(), ['int', 'float', 'string', 'bool'], true)
? $type->getName()
: null;
}

private static function bindCaster(CastsValue $caster, ReflectionParameter|ReflectionProperty $parameter): CastsValue
{
return $caster instanceof BindsToField
Expand Down
61 changes: 47 additions & 14 deletions src/Support/HydratorCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static function compile(string $class): Closure
$p = $meta->parameters;
$pipes = $meta->pipes;

$fn = eval('return '.self::generate($class, $meta).';');
$fn = eval('declare(strict_types=1); return '.self::generate($class, $meta).';');

// Any direct property assignment (constructor-less, or a hybrid
// class's extra properties) may target a readonly property, which
Expand Down Expand Up @@ -172,7 +172,7 @@ public static function compilePipeless(string $class): Closure
$p = $meta->parameters;
$pipes = $meta->pipes;

$fn = eval('return '.self::generate($class, $meta).';');
$fn = eval('declare(strict_types=1); return '.self::generate($class, $meta).';');

return self::$pipelessHydrators[$class] = self::bindForAssignments($fn, $meta, $class);
}
Expand Down Expand Up @@ -243,28 +243,41 @@ static function (#[\\SensitiveParameter] array \$d) use (\$p, \$pipes): \\{$clas
}

$unknownCheck = self::buildUnknownKeysCheck($class, $meta);
[$body, $argList] = self::buildParts($class, $meta);
$scrub = " } catch (\\Throwable \$e) {\n"
." throw \\StdOut\\SimpleDataObjects\\Support\\Redactor::scrubConstructorArguments(\$e, \\{$class}::class);\n"
[$body, $argList] = self::buildParts($class, $meta, coerceInline: false);
$construct = "new \\{$class}({$argList})";
$rethrow = "throw \\StdOut\\SimpleDataObjects\\Support\\Redactor::scrubConstructorArguments(\$e, \\{$class}::class);";
$catch = static fn (string $retry): string => " } catch (\\Throwable \$e) {\n"
." if (! \$e instanceof \\TypeError || (\$c = \\StdOut\\SimpleDataObjects\\Support\\ScalarCoercion::input(\$d, \$p)) === null) {\n"
." {$rethrow}\n"
." }\n"
." \$d = \$c;\n"
." try {\n"
." {$retry}\n"
." } catch (\\Throwable \$e) {\n"
." {$rethrow}\n"
." }\n"
." }\n";

if ($meta->hasExtraProperties) {
$extraBody = self::buildPropertyAssignments($class, $meta, includePipesPrelude: false);
$retry = $catch("\$o = {$construct};");

return <<<PHP
static function (#[\\SensitiveParameter] array \$d) use (\$p, \$pipes): \\{$class} {
{$unknownCheck}{$body} try {
\$o = new \\{$class}({$argList});
{$scrub}{$extraBody} return \$o;
\$o = {$construct};
{$retry}{$extraBody} return \$o;
}
PHP;
}

$retry = $catch("return {$construct};");

return <<<PHP
static function (#[\\SensitiveParameter] array \$d) use (\$p, \$pipes): \\{$class} {
{$unknownCheck}{$body} try {
return new \\{$class}({$argList});
{$scrub}}
return {$construct};
{$retry}}
PHP;
}

Expand Down Expand Up @@ -326,7 +339,7 @@ private static function buildUnknownKeysCheck(string $class, ClassMeta $meta): s
*
* @return array{0: string, 1: string} pipeline body and argument list
*/
private static function buildParts(string $class, ClassMeta $meta): array
private static function buildParts(string $class, ClassMeta $meta, bool $coerceInline = true): array
{
$classExport = var_export($class, true);
$body = '';
Expand All @@ -351,12 +364,12 @@ private static function buildParts(string $class, ClassMeta $meta): array
continue;
}

$f = self::fieldAccessExpr($i, $param, $classExport, $body);
$f = self::fieldAccessExpr($i, $param, $classExport, $body, $coerceInline);

// Missing key and explicit null both resolve to null — ?? is exact here,
// but only for a single name: with aliases, $d[null] would read the "" key.
if (count($param->inputNames) === 1 && $param->isPlain && $param->allowsNull && ! $param->isOptional && (! $param->hasDefault || $param->defaultValue === null)) {
$args[] = "\$d[{$f['key']}] ?? null";
$args[] = $coerceInline ? self::plainRead($i, $param, "\$d[{$f['key']}] ?? null") : "\$d[{$f['key']}] ?? null";

continue;
}
Expand Down Expand Up @@ -398,17 +411,37 @@ private static function resolveKeyExpr(int $i, array $names, string &$body): arr
return [$var, "{$var} !== null"];
}

private static function plainRead(int $i, ParameterMeta $param, string $read): string
{
if ($param->scalarType === null) {
return $read;
}

$v = "\$s{$i}";
$check = match ($param->scalarType) {
'int' => "\\is_int({$v} = {$read})",
'float' => "\\is_float({$v} = {$read}) || \\is_int({$v})",
'string' => "\\is_string({$v} = {$read})",
default => "\\is_bool({$v} = {$read})",
};
$nullable = $param->allowsNull ? " || {$v} === null" : '';
$field = var_export((string) $param->inputNames[0], true);

return "({$check}{$nullable} ? {$v} : \\StdOut\\SimpleDataObjects\\Support\\ScalarCoercion::coerce({$v}, '{$param->scalarType}', {$field}))";
}

/**
* Shared per-parameter pieces: the hot path inlines them into a ternary,
* the collecting path wraps them in try/catch.
*
* @return array{key: string, presence: string, present: string, absent: string, missingKey: string}
*/
private static function fieldAccessExpr(int $i, ParameterMeta $param, string $classExport, string &$body): array
private static function fieldAccessExpr(int $i, ParameterMeta $param, string $classExport, string &$body, bool $coerceInline = true): array
{
[$key, $presence] = self::resolveKeyExpr($i, $param->inputNames, $body);

$present = match (true) {
$param->isPlain && $coerceInline => self::plainRead($i, $param, "\$d[{$key}]"),
$param->isPlain => "\$d[{$key}]",
$param->isSensitive => "\\StdOut\\SimpleDataObjects\\Support\\ValueCaster::castSensitive(\$p[{$i}], \$d[{$key}], {$classExport})",
default => "\\StdOut\\SimpleDataObjects\\Support\\ValueCaster::cast(\$p[{$i}], \$d[{$key}])",
Expand Down Expand Up @@ -468,7 +501,7 @@ private static function buildPropertyAssignments(string $class, ClassMeta $meta,
// Missing key and explicit null both resolve to null — ?? is exact here.
// Only safe for a single accepted name; see resolveKeyExpr().
if (count($param->inputNames) === 1 && $param->isPlain && $param->allowsNull && ! $param->isOptional && (! $param->hasDefault || $param->defaultValue === null)) {
$body .= " {$target} = \$d[{$f['key']}] ?? null;\n";
$body .= " {$target} = ".self::plainRead($i, $param, "\$d[{$f['key']}] ?? null").";\n";

continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Support/MetadataRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class MetadataRegistry
*/
private const string FILE_SUFFIX = '.meta.php';

private const string FORMAT = 'sdo-cache-4';
private const string FORMAT = 'sdo-cache-5';

/** @var array<string, ClassMeta> */
private static array $cache = [];
Expand Down Expand Up @@ -140,7 +140,7 @@ private static function persist(string $class, string $file, ClassMeta $meta): b
$hydrator = HydratorCompiler::generate($class, $meta);
$serializer = SerializerCompiler::generate($class, $meta);

$code = "<?php\n\n"
$code = "<?php\n\ndeclare(strict_types=1);\n\n"
."\$meta = {$export};\n"
."\$p = \$meta->parameters;\n"
."\$pipes = \$meta->pipes;\n\n"
Expand Down
2 changes: 2 additions & 0 deletions src/Support/ParameterMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function __construct(
/** `@name` for an attribute, `#text` for the element's text, otherwise a child element name. */
public readonly ?string $xmlSource = null,
public readonly bool $isSensitive = false,
public readonly ?string $scalarType = null,
) {
$this->isPlain = $caster === null
&& $nestedDataClass === null
Expand Down Expand Up @@ -89,6 +90,7 @@ enumClass: $state['enumClass'],
phpType: $state['phpType'] ?? null,
xmlSource: $state['xmlSource'] ?? null,
isSensitive: $state['isSensitive'] ?? false,
scalarType: $state['scalarType'] ?? null,
);
}
}
84 changes: 84 additions & 0 deletions src/Support/ScalarCoercion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace StdOut\SimpleDataObjects\Support;

use InvalidArgumentException;
use StdOut\SimpleDataObjects\Exceptions\DataHydrationException;

final class ScalarCoercion
{
private const float INT_LIMIT = 9.2233720368547758E18;

/**
* @param array<mixed> $d
* @param array<int, ParameterMeta> $parameters
* @return array<mixed>|null
*/
public static function input(#[\SensitiveParameter] array $d, array $parameters): ?array
{
$coerced = null;

foreach ($parameters as $param) {
if ($param->scalarType === null || ! $param->isPlain) {
continue;
}

foreach ($param->inputNames as $name) {
if (! array_key_exists($name, $d) || self::fits($d[$name], $param)) {
continue;
}

$coerced ??= $d;
$coerced[$name] = self::coerce($d[$name], $param->scalarType, (string) $param->inputNames[0]);
}
}

return $coerced;
}

public static function coerce(#[\SensitiveParameter] mixed $value, string $type, string $field): int|float|string|bool
{
$coerced = match ($type) {
'int' => self::toInt($value),
'float' => self::toFloat($value),
'string' => is_string($value) || is_int($value) || is_float($value) || $value instanceof \Stringable ? (string) $value : null,
default => is_bool($value) ? $value : (is_string($value) || is_int($value) ? filter_var($value, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE) : null),
};

return $coerced ?? throw DataHydrationException::invalidValue(
$field,
new InvalidArgumentException('Cannot convert '.get_debug_type($value)." to {$type} without loss."),
);
}

private static function fits(#[\SensitiveParameter] mixed $value, ParameterMeta $param): bool
{
return ($value === null && $param->allowsNull) || match ($param->scalarType) {
'int' => is_int($value),
'float' => is_float($value) || is_int($value),
'string' => is_string($value),
default => is_bool($value),
};
}

private static function toInt(#[\SensitiveParameter] mixed $value): ?int
{
return match (true) {
is_int($value) => $value,
is_float($value) => $value >= -self::INT_LIMIT && $value < self::INT_LIMIT && $value === (float) (int) $value ? (int) $value : null,
is_string($value) => filter_var($value, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE),
default => null,
};
}

private static function toFloat(#[\SensitiveParameter] mixed $value): int|float|null
{
return match (true) {
is_int($value), is_float($value) => $value,
is_string($value) && is_numeric($value) && is_finite((float) $value) => (float) $value,
default => null,
};
}
}
4 changes: 3 additions & 1 deletion src/Support/ValueCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public static function cast(ParameterMeta $meta, #[\SensitiveParameter] mixed $v
return self::castEnum($meta, $value);
}

return $value;
return $meta->scalarType === null || ($value === null && $meta->allowsNull)
? $value
: ScalarCoercion::coerce($value, $meta->scalarType, (string) $meta->inputNames[0]);
} catch (DataHydrationException $e) {
throw $e;
} catch (\Throwable $e) {
Expand Down
Loading
Loading