Why do I get 0 when WP_REST_Request::get_json_params() parses a JSON body value of null?
I have a method which is responsible for turning a WP_REST_Request into another format which is acceptable for my business logic.
function prepare_item_for_database(WP_REST_Request $request): array
{
$payload = $request->get_json_params();
// ... validation
return $payload;
}
When I have a request which includes null values, the WP_REST_Request::get_json_params() function creates PHP 0, instead of null. I get the following.
Why do I get 0 in the PHP array, instead of null?
HTTP POST Body
{
"name": "s",
"feature_id": null,
"note_id": null
}
PHP Parsed Value
// What I get
Array
(
[name] => s
[feature_id] => 0
[note_id] => 0
)
// What I expect
Array
(
[name] => s
[feature_id] => null
[note_id] => null
)
The schema for the endpoint is like the following
[
'properties' => [
'name' => [
'type' => 'string'
],
'feature_id' => [
'type' => 'integer'
],
'note_id' => [
'type' => 'integer'
],
]
];
'type' => ['integer', 'null'],causes null to be parsed. So it was the schema.intval(null)you will get 0.