Luracast / Restler

Simple and effective multi-format Web API Server to host your PHP API as Pragmatic REST and/or RESTful API
http://luracast.com/products/restler/
GNU Lesser General Public License v2.1
1.36k stars 315 forks source link

choice problem with validation #657

Closed kaizirlewagen closed 3 years ago

kaizirlewagen commented 3 years ago

I am using version 5.0.5

If i use a choice like this:

* @param int $loglevel {@from query} Log level (0 = INFO, 1 = DEBUG) {@choice 0,1} {@required false}

I get the following error:

  "error": {
    "code": 400,
    "message": "Bad Request: Invalid value specified for `loglevel`. Expecting integer value"
  },
  "debug": {
    "source": "Validator.php:718 at validate stage",

Only if i select 0 (zero) through the choice. All other integer values are possible only 0 does not work. I need 0 (zero). If i use the empty value, this will be interpreted as 0 (zero).

How can i fix this?

richardeaxon commented 3 years ago

I think https://github.com/Luracast/Restler/issues/659 is related and probably has todo with this commit https://github.com/Luracast/Restler/commit/c115e237129d611413b5658f469e61382a383d88.

kaizirlewagen commented 3 years ago

In version 5 the file ValidationInfo.php does not have the === "problem". If i remove === check, the error is already there "Expecting integer value".

    public static function numericValue($value)
    {
        return ( int )$value == $value
            ? ( int )$value
            : floatval($value);
    }

So, the choice problem does already exist.

The error occurres in Validator.php line 517.

            switch ($info->type) {
                case 'int' :
                case 'float' :
                case 'number' :
                    if (!is_numeric($input)) {
                        $error .= '. Expecting '
                            . ($info->type === 'int' ? 'integer' : 'numeric')
                            . ' value';
                        break;
                    }
Arul- commented 3 years ago

Hope f2c365680ffb46466858a2961ab43bd05d999fb4 in v5 branch fixes this. Can you check?

kaizirlewagen commented 3 years ago

@Arul- works perfect. Thx for your work !!!