nabbar / SwaggerValidator-PHP

A Swagger Validation and Parser as lib for PHP to secure and helpful application for request / response validating, security stage, testunit skeleton, testauto generation, ... This lib can be used into any existing application who's having a swagger definition file for request/response.
Apache License 2.0
21 stars 3 forks source link

Validation Error : Value ! The Value does not respect specification with parameters : #/schemes ! #22

Closed richp10 closed 7 years ago

richp10 commented 7 years ago

Thanks for the other fix - my swagger.json now loads without error.

I now run:

$swagger->validate(new \SwaggerValidator\Common\Context(\SwaggerValidator\Common\Context::TYPE_REQUEST,
          \SwaggerValidator\Common\Context::MODE_DENY));

And get error:

[2017-04-25 13:50:56][DECODE][{SwaggerValidator\DataType\TypeObject::jsonUnSerialize#133] - Decoding Path "#" As "SwaggerValidator\DataType\TypeObject" 
[2017-04-25 13:50:56][DECODE][{SwaggerValidator\Object\Swagger::jsonUnSerialize#91] - Decoding Path "#" As "SwaggerValidator\Object\Swagger" 
[2017-04-25 13:50:56][VALIDATE][KO][DATAVALUE][{SwaggerValidator\Object\Swagger::checkSchemes#245] - Scheme requested is not allowed --- {"contextMode":"DENY","contextType":"REQUEST","contextLocation":null,"contextScheme":false,"contextHost":false,"contextBasePath":"","contextRequestPath":null,"contextRoutePath":null,"contextMethod":"get","contextDataPath":["#","schemes"],"contextDataCheck":[],"contextDataValue":false,"contextDataExists":true,"contextDataEmpty":true,"contextDecodeError":null,"contextDataType":null,"contextOther":[],"contextExternalRef":[],"contextIsCombined":null,"contextValidationCode":null,"mockedData":[]}

Swagger Validation Error : Value ! The Value does not respect specification with parameters : #/schemes ! 
 F:\WAPP\apps\rco-api2\vendor\njuhel\swagger-validator\src\Object\Swagger.php:245
 F:\WAPP\apps\rco-api2\vendor\njuhel\swagger-validator\src\Object\Swagger.php:192
 F:\WAPP\apps\rco-api2\vendor\njuhel\swagger-validator\src\Object\Swagger.php:119

This is using the same swagger.json I sent you ..

Thanks!

nabbar commented 7 years ago

This error is not an error of parsing or libs, it is a validation error. The http scheme is empty .

Maybe you have call the validation in php-cli mode... In this case, you need to set the $_SERVER["REQUEST_SCHEME"] to simulate the http call.

You can find the code who read this env var in the context class : https://github.com/nabbar/SwaggerValidator-PHP/blob/master/src/Common/Context.php#L182 :

    /**
     * Load the called URL/Path to identify component
     * like scheme, host, base path
     */
    public function loadUri()
    {
        $this->contextScheme = $this->getEnv('REQUEST_SCHEME');
        $this->contextHost   = $this->getEnv('SERVER_NAME');
        $uri = explode('?', $this->getEnv('REQUEST_URI'));
        $this->contextBasePath = array_shift($uri);
    }

Or, (and it could be a good idea) you can override the context/contextBase classes'methods and start the validation with you customized context classes.

You have some interface in this folder to help you in the custmization : https://github.com/nabbar/SwaggerValidator-PHP/tree/master/src/Interfaces

Ps : the scheme is the http, https in any url.

nabbar commented 7 years ago

I complete my comments to explain this error logs :

    [2017-04-25 13:50:56] :                     date time in old ISO form
    [VALIDATE] :                            operation of validation a request or a response
    [KO] :                              validation is in error (a parameter, body, header, url... validation could be wrong)
    [{SwaggerValidator\Object\Swagger::checkSchemes#245] :      origin of the error (Namespace, class, method and line)
    - Scheme requested is not allowed :                 error message
    ... :                               the rest is a context usefull data extraction

In pretty print :

    {
    "contextMode":"DENY",               => validation is run as deny mode, 2 options : DENY (stop with exception on any error find) or PASS (clean all bad data and continue validation)
    "contextType":"REQUEST",            => type of validation : Request or Response
    "contextLocation":null,             => the location elements (header, body, form, ...)
    "contextScheme":false,              => the scheme find (http, https, ...)
    "contextHost":false,                => the host extracted from the called url
    "contextBasePath":"",               => the api base path find in the called url
    "contextRequestPath":null,          => the request path find in the called url
    "contextRoutePath":null,            => the route path selected following the request path, scheme, basepath, parameters, …
    "contextMethod":"get",              => the method use in the http request  
    "contextDataPath":["#","schemes"],      => this array represent all keys of the swagger schema from the beginning “#” to the current location
    "contextDataCheck":[],              => current data check (example size, type, format, …)
    "contextDataValue":false,           => the value currently validated
    "contextDataExists":true,           => precise if the data is find (like an isset or array_key_exists) : in swagger require do not means is not empty
    "contextDataEmpty":true,            => precise if the data is empty or not : : in swagger require do not means is not empty
    "contextDecodeError":null,          => error when decoded object (xml / json)
    "contextDataType":null,             => type of data (string, int32, int64, …)
    "contextOther":[],              => other data that could helps in understanding the error
    "contextExternalRef":[],            => list of all external ref used in the validation process
    "contextIsCombined":null,           => precise if the current validation is using anyOf, allOf or one of process
    "contextValidationCode":null,           => precise the error validation code when validating params/body 
    "mockedData":[]                 => this array help for mocking call in unit test for having the data sending to simulate query
    }