Skip to content

[8.x] Fix model serialization on anonymous components - #39319

Merged
taylorotwell merged 4 commits into
8.xfrom
component-model-passing
Oct 22, 2021
Merged

taylorotwell merged 4 commits into
8.xfrom
component-model-passing

Conversation

@taylorotwell

@taylorotwell taylorotwell commented Oct 22, 2021 •

Copy link
Copy Markdown
Member

Fixes #39232

When rendering anonymous components, we do not know which attributes passed to the components are going to be used as "props" (not sanitized) vs. "attributes" (sanitized since they are likely going to be rendered into the DOM directly). With class based components we do not have this issue since we can determine which attributes are actually props by looking at the properties on the class.

Therefore, when using anonymous components, we pass every attribute as both a prop and an attribute and then the @props directive removes the specified props from the $attributes bag. However, in the process of passing everything as attributes, every object with a __toString method gets escaped - I guess because we assumed they would be rendered in the DOM?

Anyways, that is pretty undesirable in the case of Eloquent models and collections especially because the serialization process could be resource intensive if it loads additional relationships.

In order to try to solve this in a way that is as backwards-compatible as possible (in case people really are putting JSON serialized models into the DOM, I introduced a new CanBeEscapedWhenConvertedToString interface which models and collections can use.

When the Blade attribute sanitization method encounters an object of this type, we will invoke the method defined by the contract which states that when this object is converted to a string it should be escaped. Therefore, if people really do try to render this object as a string it will still be escaped.


Would appreciate any feedback on this if anyone has a better idea or sees any downfalls with this approach.

@stevebauman

Copy link
Copy Markdown
Contributor

Nice, I think this is a good solution 👍

Though, the interface name is a bit long imo, what do you think about EscapeableWhenStringified?

@GrahamCampbell GrahamCampbell changed the title Fix model serialization on anonymous components [8.x] Fix model serialization on anonymous components Oct 22, 2021
@taylorotwell
taylorotwell merged commit c2dd983 into 8.x Oct 22, 2021
@taylorotwell
taylorotwell deleted the component-model-passing branch October 22, 2021 18:01
salmanarshad321 pushed a commit to salmanarshad321/framework that referenced this pull request Sep 23, 2026
* Fix model serialization on anonymous components

* Apply fixes from StyleCI

* no need to pass value

* renaming

Co-authored-by: Taylor Otwell <taylorotwell@users.noreply.github.com>
binaryfire added a commit to hypervel/components-backup that referenced this pull request Oct 1, 2026
Use explicit upstream container setup where tests require exact namespace
or view-existence calls. Preserve each lookup count and fallback sequence;
keep the simple view-factory helper for tests that only need a default.
This restores upstream structure while retaining typed application mocks,
fluent returns, coroutine cleanup and native xxh128 component namespaces.

Add missing direct and nested namespaced component lookup coverage and the
paginator attribute-sanitization assertion. Use a concrete Paginator to
satisfy Hypervel's abstract pagination contract. Correct the withName
expectation that previously declared a second mock method named "test"
instead of checking the component name. Restore parent attributes and
correct the two copied comments that named the wrong anonymous views.

Upstream source: laravel/framework 13.x at
01d008c9b5f32cb7c5e50a9a22273113d810b2a2.
laravel/framework#61117
laravel/framework#55874
laravel/framework#55256
laravel/framework#52669
laravel/framework#39319
laravel/docs#9984

Validation: both changed Blade files pass individually and the View suite
passes within the affected-package ParaTest run. Formatting and diff checks
pass. Existing source and documentation already implement the reconciled
lookup and escaping behavior; no rendering source changes are required.
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.

Passing an Eloquent Model to an anonymous component results in expensive toJson call

2 participants