Friday, September 9, 2022
HomeWordPress Developmentrelaxation api - WP_REST_Request::get_json_params() Parsing null as Zero

relaxation api – WP_REST_Request::get_json_params() Parsing null as Zero


Why do I get 0 when WP_REST_Request::get_json_params() parses a JSON physique worth of null?

I’ve a technique which is accountable for turning a WP_REST_Request into one other format which is appropriate for my enterprise logic.

operate prepare_item_for_database(WP_REST_Request $request): array
{
    $payload = $request->get_json_params();
    // ... validation
    return $payload;
}

When I’ve a request which incorporates null values, the WP_REST_Request::get_json_params() operate creates PHP 0, as a substitute of null. I get the next.

Why do I get 0 within the PHP array, as a substitute of null?

HTTP POST Physique

{
    "title": "s",
    "feature_id": null,
    "note_id": null
}

PHP Parsed Worth

// What I get
Array
(
    [name] => s
    [feature_id] => 0
    [note_id] => 0
)

// What I anticipate
Array
(
    [name] => s
    [feature_id] => null
    [note_id] => null
)

The schema for the endpoint is like the next

[
    'properties' => [
        'name'  => [
            'type' => 'string'
        ],
        'feature_id' => [
            'type' => 'integer'
        ],
        'note_id' => [
            'type' => 'integer'
        ],
    ]
];

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments