Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to specify a root directory for $ref resolution? #161

Open
RyoJerryYu opened this issue Nov 2, 2023 · 1 comment
Open

How to specify a root directory for $ref resolution? #161

RyoJerryYu opened this issue Nov 2, 2023 · 1 comment

Comments

@RyoJerryYu
Copy link

RyoJerryYu commented Nov 2, 2023

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:

func (g *Generator) loadSchemaFromFile(fileName, parentFileName string) (*schemas.Schema, error) {
if !filepath.IsAbs(fileName) {
fileName = filepath.Join(filepath.Dir(parentFileName), fileName)
}
exts := append([]string{""}, g.config.ResolveExtensions...)
for i, ext := range exts {
qualified := fileName + ext
// Poor man's resolving loop.
if i < len(exts)-1 && !fileExists(qualified) {
continue
}
var err error
qualified, err = filepath.EvalSymlinks(qualified)
if err != nil {
return nil, fmt.Errorf("error resolving symlinks in %s: %w", qualified, err)
}

Question

Is there any way to specify a root directory for reference resolution? If not, may I contribute to this feature?

@omissis
Copy link
Owner

omissis commented Nov 7, 2023

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants