jimblackler / jsonschemafriend

A JSON Schema loader and validator, delivered as a Java library.
Apache License 2.0
52 stars 23 forks source link

SchemaStore enters infinite loop when $schema returns no content #43

Closed lizjackson-toast closed 11 months ago

lizjackson-toast commented 11 months ago

Loading a JSON schema through a SchemaStore object results in an infinite loop if a JSON schema has a $schema that has no content (such as http://json-schema.org/schema, which returns a 301) and also does not have an $id.

Here's how this happens:

  1. We enter a while (true) loop here.
  2. Then we try getting content from the $schema here and content is null.
  3. The store call here throws an exception due to the null content.
  4. We catch this exception in the catch (JsonProcessingException e) block.
  5. Then we make a Document object called doc and an Elements object called links here. However, since content is null, doc is null and links has a size of zero.
  6. As a result, we never set resolved to true here because there are no links to loop through, and we display the "Was not valid JSON" warning from this block.
  7. Then we continue here, loop again, hit the same problem, and keep looping ad infinitum.

Even though the jsonschemafriend readme lists a specific set of schemas that are supported in the $schema field, jsonschemafriend should throw an exception if the $schema has no content rather than looping infinitely.

Let me know if you'd like me to fix this myself or if someone closer to jsonschema friend would prefer to address this.

Thank you!

jimblackler commented 11 months ago

Thanks for the report and all the detective work.

If you do want to fix it yourself I'll take the patch, that would probably be faster than waiting for me to do it, given that I'm juggling about five projects at the moment.

lizjackson-toast commented 11 months ago

You know what – turns out we resolved this issue by upgrading from 0.11.4 to 0.12.2. No updates are needed after all; thanks for adding the latest updates!