question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deferencing by $id with $ref in same schema doesn't seem to resolve

See original GitHub issue

Hi there, we have used your library a few times but are currently experiencing an issue in 1.11.0 (actually I have tried older versions too). I was trying to use $id references as in the example here: https://json-schema.org/understanding-json-schema/structuring.html#using-id-with-ref

The issue I have is with a schema that looks like simple-schema.json:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "type": "object",
  "properties": {
    "events": {
      "type": "array",
      "items": {
        "oneOf": [
          {
            "$ref": "#event"
          }
        ]
      }
    }
  },
  "additionalProperties": false,
  "required": [
    "events"
  ],
  "definitions": {
    "event": {
      "$id": "#event",
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        }
      }
    }
  }
}

And a sample JSON that looks like simple-event.json:

{
  "events": [
    {
      "name": "example"
    }
  ]
}

Example test:

import static com.google.common.io.Resources.getResource;

import com.google.common.io.Resources;
import org.everit.json.schema.Schema;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.Charset;

class JsonSchemaTest {

    @Test
    void dereferencing() throws JSONException, IOException {
        String stringSchema = Resources.toString(getResource("simple-schema.json"), Charset.forName("utf-8"));
        JSONObject jsonSchema = new JSONObject(new JSONTokener(stringSchema));

        Schema schema = SchemaLoader.load(jsonSchema);
        String simple = Resources.toString(getResource("simple-event.json"), Charset.forName("utf-8"));
        schema.validate(new JSONObject(simple));
    }
}

Validation fails seemingly because the event object definition cannot be found.

If I use the absolute reference "$ref": "#/defintions/event", it works fine.

I’ve tried using the SchemaLoader with resolutionScope pointing to its own schema, but that also didn’t work.

There is a javascript validator that seems to be okay in this example: https://github.com/epoberezkin/ajv

I saw a similar issue that is closed https://github.com/everit-org/json-schema/issues/244 and seemed to be closed because the problem wasn’t meant to work with external schemas.

Thanks in advance for your feedback!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
erosbcommented, Mar 17, 2019

The fix is released in version 1.11.1.

0reactions
erosbcommented, Feb 3, 2019

Good question. It also works if you use "event" without the leading hashmark. I will investigate this in detail at some time during the week. Thanks for raising the issue & writing a reproduction test.

Read more comments on GitHub >

github_iconTop Results From Across the Web

json - Can a property of an allOf item contain a $ref to another ...
The "$id" keyword defines a URI for the schema, and the base URI that other URI references within the schema are resolved against....
Read more >
Only You Can Bring Modern OpenAPI Bundling to JavaScript
If you're familiar with OpenAPI you're probably familiar with “Bundling”, also known by some as “Resolving” or “Dereferencing”, “External…
Read more >
A Media Type for Describing JSON Documents - JSON Schema
1. Introduction. JSON Schema is a JSON media type for defining the structure of JSON data. · 2. Conventions and Terminology · 3....
Read more >
Issue 460: URI Management - CIDOC CRM
I'm dereferenced on verson 5.0.4. ... We have a provisional, not dereferenced URI: ... how much of the CRM is returned (one concept...
Read more >
JSON Schema: A Media Type for Describing JSON Documents
JSON Schema asserts what a JSON document must look like, ways to extract ... JSON Schema is intended to define validation, documentation, hyperlink...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found