iron-bound-designs / wp-rest-api-schema-validator

Validate WP REST API requests using a complete JSON Schema validator.
https://timothybjacobs.com/2017/05/17/json-schema-and-the-wp-rest-api/
MIT License
12 stars 2 forks source link

NULL value found, but an integer is required #7

Open thefrosty opened 3 years ago

thefrosty commented 3 years ago

Found another issue on the same plane as #4, this one for taxonomies.

Running a curl on my local WordPress install:

curl --location --request GET 'https://local.test/wp-json/wp/v2/tags' \
--header 'Authorization: Basic SomeAweseomKey' \
--header 'Cookie: PHPSESSID=ba5ea78b3f9eeddf3cd064674e1381c2'

Returns:

{
    "code": "rest_invalid_param",
    "message": "Invalid parameter(s): post",
    "data": {
        "status": 400,
        "params": {
            "post": "NULL value found, but an integer is required"
        }
    }
}
thefrosty commented 3 years ago

Quick fix:

<?php
$taxonomies = \get_taxonomies();
$fixQueryParams = static function (array $query_params): array {
    if (
        isset($query_params['post']) &&
        (isset($query_params['post']['type']) && \strcasecmp($query_params['post']['type'], 'integer') === 0) &&
        (isset($query_params['post']['type']) && \gettype($query_params['post']['type']) !== 'array')
    ) {
        $query_params['post']['type'] = [$query_params['post']['type'], 'null'];
    }

    return $query_params;
};
\array_walk(
    $taxonomies,
    fn(string $taxonomy): bool => \add_filter("rest_{$taxonomy}_collection_params", $fixQueryParams)
);