Skip to content
Merged
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
11 changes: 7 additions & 4 deletions src/GeneratedModels/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ private static function camelToSnake(string $camelCase): string
/**
* Automatic JSON serialization using reflection and property types
*/
public function jsonSerialize(): array
public function jsonSerialize(): array|object
{
$result = [];
$reflection = new \ReflectionClass($this);
$constructor = $reflection->getConstructor();

if (!$constructor) {
return [];
return (object)[];
}

foreach ($constructor->getParameters() as $param) {
Expand All @@ -147,7 +147,8 @@ public function jsonSerialize(): array
$result[$jsonKey] = $this->serializeValue($value);
}

return $result;
// Force empty arrays to be encoded as objects {} instead of arrays []
return empty($result) ? (object)[] : $result;
}

/**
Expand Down Expand Up @@ -188,7 +189,9 @@ private function serializeValue(mixed $value): mixed
*/
public function toArray(): array
{
return $this->jsonSerialize();
$result = $this->jsonSerialize();
// If jsonSerialize returns an empty object (for empty models), convert to empty array
return is_array($result) ? $result : [];
}

/**
Expand Down