Skip to content

How to validate against json schema containing circular $ref? #274

New issue

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

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

Already on GitHub? # to your account

Closed
csimplestring opened this issue May 26, 2016 · 11 comments
Closed

How to validate against json schema containing circular $ref? #274

csimplestring opened this issue May 26, 2016 · 11 comments

Comments

@csimplestring
Copy link

Hi, thanks for this great work. I found that there is a new major version released to support $ref. However, where can I find the usage example about it?

Now I have json schema files, which some of them contains circular $ref. Namely, schema-A.json has a $ref to schema-B.json, schema-B.json has a $ref to schema-A.json.

Any help is appreciated and thanks in advanced.

@moderndeveloperllc
Copy link

For an example of $ref usage, I would take a look here: http://json-schema.org/example2.html. Circular $ref, like circular references, or arguments for that matter, will probably cause issues. The idea behind using $ref is to have reusable "modules" of information that can be used elsewhere. A circular reference would mean that you are referencing a parent object within a child object?

@jojo1981
Copy link

@csimplestring The schema can have circular dependencies which are resolved by the RefResolver. It will create an object graph in the memory. This schema in memory can be used to validate your data against. So this library can handle all $ref cases.

@csimplestring
Copy link
Author

Can you give me an example code? Thanks!

---Sent from Boxer | http://getboxer.com

On 28 May 2016 09:56:56 CEST, Joost Nijhuis notifications@github.com wrote:@csimplestring The schema can have circular dependencies which are resolved by the RefResolver. It will create an object graph in the memory. This schema in memory can be used to validate your data against. So this library can handle all $ref cases. —You are receiving this because you were mentioned.Reply to this email directly, view it on GitHub, or mute the thread.

@csimplestring
Copy link
Author

@jojo1981 Thanks for your work. I just try it by using it. When I want to validate against a circular reference json schema, it seems to fail due to infinite recursion loop and use up all memory. Do I use the RefResolver in a wrong way?

I just use the RefResolver as the example in readme, also I notice there is a big change in API. So I guess maybe it is better to give me a demonstration or example in a correct way?

@psafarov
Copy link

Yes, RefResolver inlines all refs, so you get an infinite loop if you have recursion in your schema

@psafarov
Copy link

It's necessary to rethink schema compilation to avoid this problem

@jojo1981
Copy link

jojo1981 commented Jun 1, 2016

@csimplestring and @psafarov The RefResolver should be able to handle this. Can you verify you're using the newest version of this library? Also can you add the schema you're using? Then I can try to reproduce this.

@psafarov
Copy link

psafarov commented Jun 4, 2016

@jojo1981 unfortunately, I cant publish the schema I used, but what I found is that it occurs because object with reference to itself inside gets to ObjectIterator which iterates object recursively

@psafarov
Copy link

psafarov commented Jun 5, 2016

#277 the issue is solved in this pull request

@csimplestring
Copy link
Author

Hi, @jojo1981 Sorry for late response. After checking with my colleages, we found that the RefResovler should not be re-used in a for-loop.

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;
}

I guess the RefResolver maintains a cache internally, could you explain a bit?

@bighappyface
Copy link
Collaborator

Circular references came in #277 and are available in 3.0.0

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

No branches or pull requests

5 participants