How to specify a root directory for `$ref` resolution?
RyoJerryYu opened this issue · 1 comments
Prerequisite
I have a bundle of json schema files on local file system as below:
/path/to/base/dir/
└── schema/
└── v2/
├── named_api_resource.json
└── pokemon/
└── $id/
└── index.json
In schema/v2/pokemon/$id/index.json
, There is a $ref
to /schema/v2/pokemon/$id/index.json
:
{
"$schema": "http://json-schema.org/schema#",
"properties": {
"abilities": {
"items": {
"properties": {
"ability": {
"$ref": "/schema/v2/named_api_resource.json"
}
},
"required": [
"ability"
],
"type": "object"
},
"type": "array"
}
},
"required": [
"abilities"
],
"type": "object"
}
What did you expect to happen?
I want /schema/v2/named_api_resource.json
can be correctly resolved to /path/to/base/dir/schema/v2/named_api_resource.json
.
These schemas are downloaded from internet and I prefer not to modify these schemas.
What happened?
I tried to generate these schemas using command below:
go-jsonschema -p schema ./schema/v2/pokemon/\$id/index.json ./schema/v2/named_api_resource.json
And of course it failed, with follow message:
go-jsonschema: Failed: could not generate type for field "abilities": could not generate type for field "ability": could not follow $ref "/schema/v2/named_api_resource.json" to file "/schema/v2/named_api_resource.json": error resolving symlinks in : lstat /schema: no such file or directory
Possible Reason
And I think the reason is /schema/v2/named_api_resource.json
was resolved as the absolute path to my file system:
go-jsonschema/pkg/generator/generate.go
Lines 187 to 206 in fda3a01
Question
Is there any way to specify a root directory for reference resolution? If not, may I contribute to this feature?
Hi @RyoJerryYu , thanks for opening this issue. File resolution is a fairly complex topic in json schema, and one I only recently started to investigate (see https://json-schema.slack.com/archives/CT8QRGTK5/p1696579815116399 and https://json-schema.slack.com/archives/CT8QRGTK5/p1696613711695749).
It'll probably take a long time for me to get to the point I discussed on slack, so I guess that if you want to work on a --schemas-root-dir PR, we could make use of it for the time being, and solve your issue, so please have a go at it :)