Skip to content

Conversation

@cosmastech
Copy link
Contributor

To close: #58300

For whatever reason, we have tests in our test suite that perform assertions against a JsonResource class. They would call toArray() on an instance, and then manually call toArray() on AnonymousResourceCollections returned.

It's always uncomfortable to change tests to accommodate a framework change. This change gives developers back that confidence without breaking the new functionality introduced in 12.45.0

is_null($this->resource) => [],
is_array($this->resource) => $this->resource,
$this->resource instanceof Collection => $this->resource->map(fn ($val) => $val instanceof JsonResource ? $val->resolve($request) : $val)->toArray(),
default => $this->resource->toArray($request),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not positive that this is correct. It's possible that the resource isn't an array, a Collection, or null... in that case, we don't know if there's a toArray() method 🤷

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make sense to check $this->resource instanceof Arrayable before calling toArray()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JsonResource doesn't implement Arrayable anyways. It would have to be a method_exists($this->resource, 'toArray') check. And even then, the toArray() method here differs from Arrayable in its input parameter. 🤷

Also, if it's not an array, null, Arrayable, JsonResource, or Collection, what's the expected behavior? Should it throw an exception? Return an empty array? I have no idea how it's being used by others, so I imagine any choice would cause heartache and strife 😆

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough 👍🏻

@crynobone crynobone marked this pull request as draft January 7, 2026 02:14
@cosmastech cosmastech closed this Jan 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling toArray() on AnonymousResourceCollection returns an array of resources

2 participants