Deferencing by $id with $ref in same schema doesn't seem to resolve
See original GitHub issueHi 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:
- Created 5 years ago
- Comments:10 (10 by maintainers)
Top GitHub Comments
The fix is released in version
1.11.1
.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.