Skip to content

[8.x] Pass null to custom cast set method when value is null #38004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

jnoordsij
Copy link
Contributor

When using a custom cast class for casting attributes on a model, it is currently impossible to add custom behavior when the value set equals to null. This is due to the fact that in this case there is a manual catch that calls the setter with the current model value instead of the given null value.

By the looks of it, I think the purpose of this clause is to automatically set all attributes values provided by the class set method equal to null, which sounds reasonable but then again prevents users for adding custom behavior. It might be even better to remove the clause altogether (though this probably should be considered a breaking change), any feedback is appreciated!

@taylorotwell
Copy link
Member

I don't think we can change any behavior related to this on a patch release?

@jnoordsij
Copy link
Contributor Author

This change actually doesn't change the behavior really; the only thing that changes is the set call receiving null (as one would expect when setting a property to null) instead of "old value". The array_map callback still results in all attribute values being forcibly changed to null, regardless of the output of the cast.

Maybe this doesn't really help all that much, especially considering no one else has stumbled upon this problem yet, so it might be better if I create a PR targetting master that removes the clause altogether to allow users to have custom logic for handling null values.

@GrahamCampbell GrahamCampbell changed the title Pass null to custom cast set method when value is null [8.x] Pass null to custom cast set method when value is null Jul 14, 2021
@GrahamCampbell
Copy link
Member

The fact that no tests broke means there is not sufficient test coverage here. Can you add a new test that used to fail, but after your changes, now passes?

@taylorotwell
Copy link
Member

Agree with @GrahamCampbell's comment. That would be helpful.

@taylorotwell
Copy link
Member

Marking as draft for now - please mark as active once that has been done.

@taylorotwell taylorotwell marked this pull request as draft July 15, 2021 14:10
@jnoordsij
Copy link
Contributor Author

I added a test based on the example in the docs.

@jnoordsij jnoordsij marked this pull request as ready for review July 16, 2021 13:22
@taylorotwell
Copy link
Member

I really don't think I can merge this on a patch on further thought. I'll think about it for the 9.x release.

@jnoordsij jnoordsij deleted the pass-null-to-custom-cast-setter branch March 1, 2022 10:49
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.

3 participants