Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SchemaStorage::expandRefs() loops infinitely on recursive $ref definition #53

Closed
MaikWagner opened this issue Aug 28, 2018 · 1 comment

Comments

@MaikWagner
Copy link

Hi,

I was trying to validate a response with a json schema that has circular $ref definitions. This is basically allowed (https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03#section-7) but leads to an php error after looping infinitely:

PHP Fatal error: Maximum execution time of 30 seconds exceeded in vendor/justinrainbow/json-schema/src/JsonSchema/SchemaStorage.php on line 85

With xdebug enabled it exits after a while and throws this backtrace:

PHP Fatal error: Uncaught Error: Maximum function nesting level of '256' reached, aborting! in /vendor/justinrainbow/json-schema/src/JsonSchema/SchemaStorage.php:86
Stack trace:
#0 vendor/justinrainbow/json-schema/src/JsonSchema/SchemaStorage.php(86): is_object('id')
#1 vendor/justinrainbow/json-schema/src/JsonSchema/SchemaStorage.php(89): JsonSchema\SchemaStorage->expandRefs('id', 'internal://prov...')
#2 vendor/justinrainbow/json-schema/src/JsonSchema/SchemaStorage.php(107): JsonSchema\SchemaStorage->expandRefs(Array, 'internal://prov...')
#3 vendor/justinrainbow/json-schema/src/JsonSchema/SchemaStorage.php(107): JsonSchema\SchemaStorage->expandRefs(Object(stdClass), 'internal://prov...')
#4 vendor/justinrainbow/json-schema/src/JsonSchema/SchemaSto in vendor/justinrainbow/json-schema/src/JsonSchema/SchemaStorage.php on line 86

schema-loop-debug

You can reproduce the issue with the following code:

test.php

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/Response.php';
require_once __DIR__ . '/SwaggerSchemaManager.php';
require_once __DIR__ . '/SwaggerValidator.php';
require_once __DIR__ . '/Validator.php';

$definition =  __DIR__ . '/swagger.json';
if (!is_readable($definition)) {
        throw new Exception("Could not read '$definition'.");
}

//$header = getHeader((array) ([]));
//$body = $filename === '' ? '' : readFromFile($filename);
$response = new Response(200, [], "", "/ps?hash=123", 'GET');

$validator = new SwaggerValidator('file://' . $definition);

if ($result = $validator->validateResponse($response)) {
        print("ok\n");
} else {
        fprintf(STDERR, implode(', ', $validator->getErrors()) . "\n");
        exit(1);
}

swagger.json

{
  "swagger": "2.0",
  "info": {
    "title": "Test",
    "version": "0.0.1"
  },
  "paths": {
    "/ps": {
      "get": {
        "parameters": [
          {
            "in": "query",
            "name": "hash",
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "foo",
            "schema": {
              "$ref": "#/definitions/actionItem"
            }
          }
        }
      }
    }
  },
  "definitions": {
    "response": {
    },
    "actionItem": {
      "type": "object",
      "properties": {
        "value": {
          "type": "string"
        },
        "refs": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/actionItem"
          }
        }
      }
    }
  }
}
@Maks3w
Copy link
Owner

Maks3w commented Aug 28, 2018

Feel free to open the issue to the justinrainbow/json-schema project.

Alternative, you can instantiate the SchemaManager with a full schema (with resolved references) using the constructor https://github.com/Maks3w/SwaggerAssertions/blob/master/src/SchemaManager.php#L40

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants