jimblackler / jsonschemafriend

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

Example from specification fails with NPE #12

Open olegshtch opened 2 years ago

olegshtch commented 2 years ago

I've prepared test suite based on https://json-schema.org/draft/2020-12/json-schema-core.html#recursive-example in https://github.com/json-schema-org/JSON-Schema-Test-Suite/pull/521

Currently it fails with NullPointerException:

java.lang.NullPointerException
    at net.jimblackler.jsonschemafriend.Validator.validate(Validator.java:95)
    at net.jimblackler.jsonschemafriend.Validator.validate(Validator.java:179)
    at net.jimblackler.jsonschemafriend.Validator.validate(Validator.java:76)
    at net.jimblackler.jsonschemafriend.Validator.validate(Validator.java:412)
    at net.jimblackler.jsonschemafriend.Validator.validate(Validator.java:76)
    at net.jimblackler.jsonschemafriend.Validator.validate(Validator.java:505)
    at net.jimblackler.jsonschemafriend.Validator.validate(Validator.java:156)
    at net.jimblackler.jsonschemafriend.Validator.validate(Validator.java:71)
    at net.jimblackler.jsonschemafriend.SuiteTest.lambda$scan$1(SuiteTest.java:100)
jimblackler commented 2 years ago

I tested it this way and it worked:

{
  "$defs": {
    "tree": {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "$id": "https://example.com/tree",
      "$dynamicAnchor": "node",
      "type": "object",
      "properties": {
        "data": true,
        "children": {
          "type": "array",
          "items": {
            "$dynamicRef": "#node"
          }
        }
      }
    }
  },
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/strict-tree",
  "$dynamicAnchor": "node",
  "$ref": "tree",
  "unevaluatedProperties": false
}
{
    "children": [ { "daat": 1 } ]
}

But I will attempt to patch your test locally and see what happens.

olegshtch commented 2 years ago

Maybe it doesn't work because tree schema in other file.

jimblackler commented 2 years ago

Step one is to not NPE if $dynamicAnchor doesn't resolve, which is obviously bad. https://github.com/jimblackler/jsonschemafriend/commit/c2d871f19a3e83a24fdbf5ff6a3b195e0c586e0a

It doesn't fix your test case though so I'll look more into that.

jimblackler commented 2 years ago

OK the issue is the port spec in the URL http://localhost:1234/strict-tree.json. The library was mishandling those. https://github.com/jimblackler/jsonschemafriend/commit/e5d884a8499c820313767cc492f5e0463c0c8559 contains fixes for this specific test case (although it's possible there are more).