[wp-trac] [WordPress Trac] #46238: REST API: Allow conditional field registration

WordPress Trac noreply at wordpress.org
Tue Feb 12 10:22:39 UTC 2019


#46238: REST API: Allow conditional field registration
-------------------------+------------------------------
 Reporter:  dmsnell      |       Owner:  (none)
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  REST API     |     Version:
 Severity:  normal       |  Resolution:
 Keywords:               |     Focuses:
-------------------------+------------------------------

Comment (by mmtr86):

 Thanks for opening this ticket @dmsnell!

 > we came across this when wanting to add metadata to video files, which
 are post types and conflated with posts and other media attachments

 For context, this is
 [[https://github.com/Automattic/jetpack/pull/11162|the GitHub PR]] where
 we adding this behavior. I'd like to clarify that we were actually adding
 the new REST field only to the media attachments with
 `register_rest_field( 'attachment', ... )`, so the posts were not
 affected. But indeed, this is adding the field to all the attachments and
 we only want it on video attachments.

 > it uses a "sentinel value" passed into the get_callback() function which
 can be returned to indicate that the field should not exist.

 I like how this approach is backwards compatible with the existing
 behavior, and your suggested implementation looks elegant and simple to
 me.

 I wonder however what implications this may cause to the schema. Since we
 cannot conditionally add the field to the schema, it could present an
 inconsistency:
 - When the field is declared on the schema but not present on the
 response.
 - When the field is undeclared on the schema but present on the response.

 On the previous PR, we opted for declaring the field on the schema. This
 doesn't cause any failure to the responses not containing the field, but
 the inconsistency between schema and response is there.

 I'm not familiar with API schemas, but maybe there is a way to define a
 field as optional so we make the schema to always match the response.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/46238#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list