Skip to content

Circular references #281

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

Closed
nicolas-t opened this issue Jun 16, 2016 · 3 comments
Closed

Circular references #281

nicolas-t opened this issue Jun 16, 2016 · 3 comments

Comments

@nicolas-t
Copy link

Hi,

I'm using the v2.0.5, and I encouter this error:

PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 32 bytes) in vendor/justinrainbow/json-schema/src/JsonSchema/Iterator/ObjectIterator.php on line 127

This error seems to be related to circular references.

Here is a minimal version of my 2 schemas:

Author.json _in some cases_ will return a collection of Posts

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "id": "Author.json",
    "type": "object",
    "properties": {
        ...
        "posts": {
            "type": "array",
            "items": {
                "$ref": "Post.json#"
            }
        }
    }
}

Post.json _in some cases_ will return an Author

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "id": "Post.json",
    "title": "Post Schema",
    "type": "object",
    "properties": {
        ...
        "author": {
            "$ref": "Author.json#"
        }
    }
}

I just have two cases:

  1. I want an author with a collection of posts
  2. I want a post with his author

The circular cases (where I want an author with his posts+author) never happens in real life.

Not sure if i'm clear.

My question:
Is it a problem of the way I designed my schemas or is it a problem of your validation library?

Best regards,

@csimplestring
Copy link

csimplestring commented Jun 19, 2016

@nicolas-t Can you check this issue #274 ? In my comment, I found that

In our code, we want to cache all the resolved schemas, so we write like this:

// This will cause infinite looping and crash
$refResolver = new RefResolver(new UriRretriever(), new UriResolver());
for ($schemas as $schema) {
  $resolved = $refResolver->resolver($schema);
  $cache[] = $resolved;
}

// but this will work ok
for ($schemas as $schema) {
  $refResolver = new RefResolver(new UriRretriever(), new UriResolver());
  $resolved = $refResolver->resolver($schema);
  $cache[] = $resolved;
}

It seems that every time you dereference a schema, you need to create a new RefResolver. You can try this way to see if it works or not

@nicolas-t
Copy link
Author

nicolas-t commented Jun 19, 2016

Thanks !

yes I saw your comment before opening this issue, but in my code i'm not looping over schemas.

My code looks like this:

$refResolver = new JsonSchema\RefResolver(new JsonSchema\Uri\UriRetriever(), new JsonSchema\Uri\UriResolver());
$schema = $refResolver->resolve('file://mySchema.json'));

$validator = new JsonSchema\Validator();
$validator->check($json->data, $schema);

So i'm not sure how to use your example.

@bighappyface
Copy link
Collaborator

Should be solved with #277 and is available in 3.0.0

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

3 participants