Skip to content

Commit

Permalink
fix(laravel): Eloquent date and datetime type detection (#6529)
Browse files Browse the repository at this point in the history
* fix(laravel): Eloquent date and datetime type detection

* fix
  • Loading branch information
dunglas authored Aug 21, 2024
1 parent 2909441 commit 2df0860
Showing 1 changed file with 8 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,16 @@ public function create(string $resourceClass, string $property, array $options =
}

$builtinType = $p['cast'] ?? $p['type'];
if ('datetime' === $builtinType) {
$type = new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTimeImmutable::class);
} else {
if (\in_array($builtinType, Type::$builtinTypes, true)) {
$type = new Type($builtinType, $p['nullable']);
} else {
$type = new Type(Type::BUILTIN_TYPE_STRING, $p['nullable']);
}
}
$type = match ($builtinType) {
'datetime', 'date' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTime::class),
'immutable_datetime', 'immutable_date' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTimeImmutable::class),
default => new Type(\in_array($builtinType, Type::$builtinTypes, true) ? $builtinType : Type::BUILTIN_TYPE_STRING, $p['nullable']),
};

$propertyMetadata = $propertyMetadata
return $propertyMetadata
->withBuiltinTypes([$type])
->withWritable($propertyMetadata->isWritable() ?? true)
->withReadable($propertyMetadata->isReadable() ?? false === $p['hidden']);

return $propertyMetadata;
}

foreach ($this->modelMetadata->getRelations($model) as $relation) {
Expand All @@ -95,13 +89,12 @@ public function create(string $resourceClass, string $property, array $options =
}

$type = new Type($collection ? Type::BUILTIN_TYPE_ITERABLE : Type::BUILTIN_TYPE_OBJECT, false, $relation['related'], $collection, collectionValueType: new Type(Type::BUILTIN_TYPE_OBJECT, false, $relation['related']));
$propertyMetadata = $propertyMetadata

return $propertyMetadata
->withBuiltinTypes([$type])
->withWritable($propertyMetadata->isWritable() ?? true)
->withReadable($propertyMetadata->isReadable() ?? true)
->withExtraProperties(['eloquent_relation' => $relation] + $propertyMetadata->getExtraProperties());

return $propertyMetadata;
}

return $propertyMetadata;
Expand Down

0 comments on commit 2df0860

Please sign in to comment.