Opened 6 years ago
Closed 6 years ago
#45448 closed defect (bug) (fixed)
rest_sanitize_value_from_schema() returns invalid value when using a schema of type array with a null value
Reported by: | hogash | Owned by: | SergeyBiryukov |
---|---|---|---|
Milestone: | 5.1 | Priority: | normal |
Severity: | normal | Version: | 4.9.8 |
Component: | REST API | Keywords: | has-patch needs-unit-tests |
Focuses: | rest-api | Cc: |
Description
When using the following configuration, you will get an array containing 1 empty string instead of an empty array;
$schema = array( 'type' => 'array', 'items' => [ 'type' => 'string', ], ); $value = rest_sanitize_value_from_schema( null, $schema ); var_dump( $value );
The solution for this would be to modify the following line in rest_sanitize_value_from_schema():
$value = preg_split( '/[\s,]+/', $value );
with
$value = preg_split( '/[\s,]+/', $value, null, PREG_SPLIT_NO_EMPTY );
Attachments (2)
Change History (10)
#3
@
6 years ago
@wpboss I had a look at preg_split()
and stumbled upon an interesting comment here:
https://secure.php.net/manual/en/function.preg-split.php#121475
that null
input for the limit will generate an error in strict_type
mode in PHP 7.1+
I could verify that with this snippet:
<?php declare(strict_types=1); $value = null; $value = preg_split( '/[\s,]+/', $value, null, PREG_SPLIT_NO_EMPTY ); var_dump( $value );
will generate a fatal error in PHP 7.0+ according to:
So instead of:
$value = preg_split( '/[\s,]+/', $value, null, PREG_SPLIT_NO_EMPTY );
it looks like one could consider:
$value = preg_split( '/[\s,]+/', $value, -1, PREG_SPLIT_NO_EMPTY );
as already used elsewhere in core.
I have fixed this and test it.
@wpboss I guess you mean manually tested it. It would also be interesting to check if the current unit tests for rest_sanitize_value_from_schema()
and the restapi group will run successfully.
#4
@
6 years ago
Thanks for the update @wpboss
ps: I created a new ticket #45486 for the correction of the inline documentation of rest_sanitize_value_from_schema()
that I noticed here.
#5
@
6 years ago
- Keywords needs-unit-tests added
- Milestone changed from Awaiting Review to 5.1
- Owner set to SergeyBiryukov
- Status changed from new to reviewing
I have fixed this and test it.