-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Saving a certain meta key saves other unmodified keys, using core/editor editPost action #17864
Comments
@epiqueras is there a chance to take a look at this issue? This regression is still present in WordPress 5.2.4 and Gutenberg 6.7.0. If there is anything to clarify please let me know. |
When saving an object property like meta. We merge in any changes to the current persisted object and send that. In general, The issue here is that the API is returning empty strings or false booleans for undefined meta keys and those are being sent back when saving, masking the difference between an actual undefined value and an empty string or false boolean. I believe this should be fixed at the API level. |
IIRC the last time @kadamwhite and I discussed default meta, we came to the conclusion that if you updated a resource with the default value, that it should save the default value. It'd be a breaking API change to stop saving default metadata when it is posted back. But I can see how this would be frustrating in this use case. Something to discuss at an upcoming Rest API meeting for sure. Was Gutenberg pre 6.7.0 not sending back the previous |
I guess so.
But, then why doesn’t get_post_meta also return the default values?
…On Wed, Oct 30, 2019 at 1:32 PM Timothy Jacobs ***@***.***> wrote:
IIRC the last time @kadamwhite <https://github.com/kadamwhite> and I
discussed default meta, we came to the conclusion that if you updated a
resource with the default value, that it should save the default value.
It'd be a breaking API change to stop saving default metadata when it is
posted back. But I can see how this would be frustrating in this use case.
Something to discuss at an upcoming Rest API meeting for sure.
------------------------------
Was Gutenberg pre 6.7.0 not sending back the previous meta property and
just including edited fields?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#17864?email_source=notifications&email_token=AESFA2BSMAGKYINEHCPNVW3QRHVNJA5CNFSM4I7AJTMKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECVVNIQ#issuecomment-548099746>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AESFA2BZIKR4E3WUGDZ4ZSTQRHVNJANCNFSM4I7AJTMA>
.
|
It does to the extent that it can. Currently, you can't specify a specific default value for the underlying |
I meant for the variant of |
If you are using a "multiple" meta key, then the REST API won't return default values either. This would change after 43941. |
I see. What do you think the timeline for that change will be (43941)? I can hack this in the client for now if necessary. |
The goal is 5.4, I think were trying for 5.4-early to give it as much time to soak as possible. |
I created a trac ticket with an alternate way to solve the problem: https://core.trac.wordpress.org/ticket/48478 |
Thanks! |
Following up on this, we chatted briefly about that idea at WordCamp US, and we weren't thrilled with the idea. I was looking at some other code and I think restoring the previous behavior in Gutenberg would be a good idea for another reason too. If an invalid value is stored for a meta key, the REST API will surface this by returning the field as |
I understand why with custom defaults we would want to save when the defaults are posted back. But, I don't think we should be saving the empty value. Is that something we can change? Otherwise, we are kind of breaking the stateless transfer contract of REST APIs and hacking a fix into what should be a general client library that works with any REST API. The problem lies in the fact that we are not differentiating empty from default. What I'm proposing is:
|
It’d be a BC break we’d have to justify. I’ll make sure it’s on the next meeting agenda. We aren’t meeting tomorrow because of the holidays, but for next week. I’m not dead set against it, but even if we do make that change, there is still the issue with invalid values. I’m still unclear on what actually changed in Gutenberg or what package the change happened in. Could you possibly illustrate what the hack would be? |
Thanks 😄
If you can't send back
The saving was done in a more ad-hoc way instead of being in |
You can't. The idea is to not unintentionally remove data. I'm actually in the middle of writing the docs for that 😃. I think it would be a good idea to allow some way for that. Discussion on meta: WP-API/WP-API#68
As we've found out through this issue and the other areas where you can't always
So |
Well, sending back
Purely because of backwards compatibility?
Yes.
It works on an entity config abstraction which defines a REST endpoint among other things. |
Yes, but the REST API can't disambiguate between you intentionally sending
No. Because of the risk of conflicts. If you don't know if your
Is there an issue number describing the change? The more I think about merging with stored data, instead of only persisting the changed values, the more possible conflicts I can see. This would lose any edits that happened between writes, even if those fields are never exposed to Gutenberg.
Right, but those are all fetched from WordPress and use hardcoded WordPress routes. The package is also described as a package providing data for WordPress. I don't understand why this package would be CMS agnostic. |
This is the same for any edit on any API, if you don't have a fresh API response and you "PUT" a new value, any changes in the meantime are lost, not sure it should be a consideration. If we don't want to merge with stored data, the best way to achieve this is to provide "PATCH" endpoints. Can we consider that all existing PUT endpoints in Core are in fact PATCH endpoints? |
That's not what I'm saying. To demonstrate what I think the incorrect behavior is in the most clear case, let's say
{
"meta": {
"field_1": "Hello",
"field_2": "World"
}
}
{
"meta": {
"field_1": "Hello",
"field_2": "Earth"
} Changes to
Even though it is semantically incorrect, many APIs implement a looser version of Even when updating a nested property of a specific field, for instance how Stripe updates metadata. The client also needs to be pragmatic.
AFAIK all endpoints in Core have PATCH semantics and it is advertised as such in the I would personally like for the REST API to have different handlers for |
That's exactly what I understood and what I was saying is that but you're saying that all endpoints are actually PATCH endpoints, that's good news and I think it means we should just update |
That seems conflicting with the way sending back default values is handled and contradicts that the API has
That should be dealt with by making post meta a separate resource in the REST API, not by breaking REST contracts.
It's API agnostic, so that custom blocks can sync to whatever APIs they need to sync with.
That's a
If this is true for all endpoints, then I agree with Riad that we should just change If it is not, then I think we should move forward with #17864 (comment). |
That's true, but it is how the REST API operates. We can't just change that without breaking backwards compatibility.
I linked earlier to one of the many discussions about support post meta in the API. That is where it worked with separate routes. And it changed hereish to the version we have today: WP-API/wp-api-meta-endpoints#15. I'm trying to find more reasoning around those changes.
It is a Again, I don't disagree about it not following the semantics. And I agree it would be ideal if we did follow the expected semantics and RFCs more closely. But I think we really have to be pragmatic here. And collect these improvements for a possible v3 at some point in the future. |
Yeah, non-REST APIs will use
Totally agree, that's why my 2 suggestions for moving forward don't involve changing the semantics. One is a small fix to how meta default and null value saving works and the other is fully client-side, but to make a decision we first need to know if this |
Sure, but then it is just an argument about what constitutes a REST API. Stripe describes its API as a REST API. And is generally heralded as a good example of API design.
Yeah, using
I'm not aware of any |
Great, thanks! 😄 |
@epiqueras @TimothyBJacobs Just looping back to this - can you please confirm whether or not this is still an issue that needs to remain open? |
Describe the bug
When saving a meta key in a post all other non-modified keys are also saved. This leads to keys which havent been modified to save empty values.
To reproduce
Steps to reproduce the behavior:
Expected behavior
With WordPress 5.1 and 5.2, saving meta using
wp.data.dispatch("core/editor").editPost
only saves the meta keys that changed. This means that the ouput is:However in the latest Gutenberg (and at least the previous version) it saves the value of the changed key and an empty value in any non-saved key:
This is problematic as its saving values that were not intended. Before, one could check if a key was saved using
get_post_custom_keys
. This function now returns bothmy_meta_b
andmy_meta_c
but WP 5.1, 5.2 do not.I hope its clear, thanks.
The text was updated successfully, but these errors were encountered: