[wp-trac] [WordPress Trac] #56483: Weird oneOf behaviour upon validation of post meta value upon GET retrieval via REST API

WordPress Trac noreply at wordpress.org
Thu Sep 1 00:50:04 UTC 2022


#56483: Weird oneOf behaviour upon validation of post meta value upon GET retrieval
via REST API
--------------------------+------------------------------
 Reporter:  joeyojoeyo12  |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  REST API      |     Version:  6.0.1
 Severity:  major         |  Resolution:
 Keywords:  needs-patch   |     Focuses:  rest-api
--------------------------+------------------------------

Comment (by joeyojoeyo12):

 Another example, eventually illustrating some deeper rooted problems:

 {{{#!php
 <?php
 add_action(
         'rest_api_init',
         function () {

                 register_post_meta(
                         post_type: 'my_custom_post',
                         meta_key:  'purchase_details',
                         args:      [
                                            'type'         => 'object',
                                            'single'       => true,
                                            'show_in_rest' => [
                                                    'schema' => [
                                                            'oneOf' => [
                                                                    [
 'title'                => 'first_possibility',
 'type'                 => 'object',
 'properties'           => [
 'first_name' => [
 'type'     => 'string',
 'required' => true
 ],
 'age'        => [
 'type'     => 'integer',
 'required' => true
 ],
 'gender'     => [
 'type'     => 'string',
 'enum'     => [
 'm',
 'w',
 'f'
 ],
 'required' => true
 ],
 'purchase_comment'      => [
 'type'     => 'string',
 'required' => true
 ],
 'products'  => [
 'type'        => 'array',
 'uniqueItems' => true,
 'items'       => [
 'type'    => 'string',
 'pattern' => '[A-z]'
 ],
 'minItems'    => 1,
 'required'    => true
 ],
 'online_id'     => [
 'type'     => 'string',
 'pattern'  => '[0-9]_[A-z]{10}',
 'required' => true
 ],
 'shop_location'  => [
 'type'                 => 'object',
 'patternProperties'    => [
 '[A-z]' => [
 'type'        => 'array',
 'uniqueItems' => true,
 'items'       => [
 'type'    => 'string',
 'pattern' => '[A-z]'
 ],
 'minItems'    => 0
 ]
 ],
 'minProperties'        => 0,
 'additionalProperties' => false,
 'required'             => true
 ],
 'feedbacks'  => [
 'type'                 => [
 'object',
 'null'
 ],
 'properties'           => [
 'intros' => [
 'type'        => 'array',
 'uniqueItems' => true,
 'items'       => [
 'type' => 'string'
 ],
 'required'    => true
 ],
 'mains' => [
 'type'        => 'array',
 'uniqueItems' => true,
 'items'       => [
 'type' => 'string'
 ],
 'required'    => true
 ],
 'submains' => [
 'type'        => 'array',
 'uniqueItems' => true,
 'items'       => [
 'type' => 'string'
 ],
 'required'    => true
 ],
 'footers' => [
 'type'        => 'array',
 'uniqueItems' => true,
 'items'       => [
 'type' => 'string'
 ],
 'required'    => true
 ],
 'signatures' => [
 'type'        => 'array',
 'uniqueItems' => true,
 'items'       => [
 'type' => 'string'
 ],
 'required'    => true
 ],
 'addresses' => [
 'type'     => 'array',
 'items'    => [
 'type' => 'string'
 ],
 'required' => true
 ]
 ],
 'additionalProperties' => false,
 'required'             => true
 ]
 ],
 'additionalProperties' => false
                                                                    ],
                                                                    [
 'title'                => 'second_possibility',
 'type'                 => 'object',
 'properties'           => [
 'first_name'    => [
 'type'     => 'string',
 'required' => true
 ],
 'order_name'     => [
 'type'     => 'string',
 'required' => true
 ],
 'product_name'     => [
 'type'        => 'array',
 'uniqueItems' => true,
 'items'       => [
 'type'    => 'string',
 'pattern' => '[A-z]{5}'
 ],
 'minItems'    => 1,
 'required'    => true
 ],
 'product_category'      => [
 'type'                 => 'object',
 'patternProperties'    => [
 '[A-z]{5}' => [
 'type'        => 'array',
 'uniqueItems' => true,
 'items'       => [
 'type' => 'string'
 ],
 'minItems'    => 0
 ]
 ],
 'minProperties'        => 1,
 'additionalProperties' => false,
 'required'             => true
 ],
 'online_name'        => [
 'type'     => 'string',
 'pattern'  => '[A-z]{10}',
 'required' => true
 ],
 'shop_location'  => [
 'type'                 => 'object',
 'patternProperties'    => [
 '[A-z]' => [
 'type'        => 'array',
 'uniqueItems' => true,
 'items'       => [
 'type'    => 'string',
 'pattern' => '[A-z]'
 ],
 'minItems'    => 0
 ]
 ],
 'minProperties'        => 0,
 'additionalProperties' => false,
 'required'             => true
 ],
 'feedbacks'  => [
 'type'                 => [
 'object',
 'null'
 ],
 'properties'           => [
 'intros' => [
 'type'        => 'array',
 'uniqueItems' => true,
 'items'       => [
 'type' => 'string'
 ],
 'required'    => true
 ],
 'mains' => [
 'type'        => 'array',
 'uniqueItems' => true,
 'items'       => [
 'type' => 'string'
 ],
 'required'    => true
 ],
 'submains' => [
 'type'        => 'array',
 'uniqueItems' => true,
 'items'       => [
 'type' => 'string'
 ],
 'required'    => true
 ],
 'footers' => [
 'type'        => 'array',
 'uniqueItems' => true,
 'items'       => [
 'type' => 'string'
 ],
 'required'    => true
 ],
 'signatures' => [
 'type'        => 'array',
 'uniqueItems' => true,
 'items'       => [
 'type' => 'string'
 ],
 'required'    => true
 ],
 'addresses' => [
 'type'     => 'array',
 'items'    => [
 'type' => 'string'
 ],
 'required' => true
 ]
 ],
 'additionalProperties' => false,
 'required'             => true
 ],
 'profile_doc' => [
 'type'     => 'string',
 'pattern'  => '^[0-9]+\.(jpg|jpeg|png)$',
 'required' => true
 ]
 ],
 'additionalProperties' => false
                                                                    ]
                                                            ]
                                                    ]
                                            ]
                                    ]
                 );

                 register_post_meta(
                         post_type: 'my_custom_post',
                         meta_key:  'product_category_id',
                         args:      [
                                            'type'          => 'integer',
                                            'single'        => true,
                                            'auth_callback' => function (
                                                    $allowed,
                                                    $meta_key,
                                                    $object_id,
                                                    $user_id,
                                                    $cap,
                                                    $caps
                                            ) {
                                                    return
 current_user_can( 'edit_dashboard' );
                                            },
                                            'show_in_rest'  => [
                                                    'schema' => [
                                                            'type'    =>
 'integer',
                                                            'minimum' => 1
                                                    ]
                                            ]
                                    ]
                 );

                 register_post_meta(
                         post_type: 'my_custom_post',
                         meta_key:  'product_shop_id',
                         args:      [
                                            'type'          => 'integer',
                                            'single'        => true,
                                            'auth_callback' => function (
                                                    $allowed,
                                                    $meta_key,
                                                    $object_id,
                                                    $user_id,
                                                    $cap,
                                                    $caps
                                            ) {
                                                    return
 current_user_can( 'edit_dashboard' );
                                            },
                                            'show_in_rest'  => [
                                                    'schema' => [
                                                            'type'    =>
 'integer',
                                                            'minimum' => 0
                                                    ]
                                            ]
                                    ]
                 );

         }
 );
 }}}

 With this setting, try to add a new
 {{{
 purchase_details
 }}}
  meta value via
 {{{
 add_post_meta
 }}}
 , and then to try to retrieve it via
 {{{
 REST
 }}}
  without its value being
 {{{
 null
 }}}
 .

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


More information about the wp-trac mailing list