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

Avoiding downloads of URI $ids #244

Closed
darcyrush opened this issue Aug 4, 2021 · 1 comment
Closed

Avoiding downloads of URI $ids #244

darcyrush opened this issue Aug 4, 2021 · 1 comment

Comments

@darcyrush
Copy link

Forgive me if this is the same as all these issues, but I am new to JSON schemas and the tooling around it so would like clarification if I am misunderstanding something. I am using Schema 2020-12 with .json files on a filesystem with the json files nested in folders.

parent_folder
    folder_1
        example_1.json
    folder_2
        example_2.json

example_1.json

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/parent_folder/folder_1/example_1.json"
  "type": "object",
  "properties": {
    "data": {
      "$ref": "https://example.com/parent_folder/folder_2/example_2.json"
    }
  },
}

example_2.json

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/parent_folder/folder_2/example_2.json"
  "type": "object",
  "properties": {
    "prop": "found"
  }
}

I am setting the URI to a bogus URL but from what I read on json-schema.org, it does not mean anything is necessarily downloaded.

I am loading both schemas

import Ajv from 'ajv/dist/2020';
import addFormats from 'ajv-formats';
import * as example_1 from '../../parent_folder/folder_1/example_1.json';
import * as example_2 from '../../parent_folder/folder_2/example_2.json';

const ajv = new Ajv({ allErrors: true });
addFormats(ajv);
ajv.addSchema(example_1, 'ex1')
ajv.addSchema(example_2, 'ex2')

However when running, the library tries to download the schemas defined in the $id which don't exist.

To avoid the library from trying to download the JSON via HTTPS I disabled the http resolver (I am running via CLI) so that the file resolver is used instead

json2ts -i './parent_folder/' -o './derefed/' --'$refOptions' '{ resolve: { http: false } }'

Which instead results in an error in the library trying to read the root folder of my project instead of reading the files in /path/to/my/project/root/schemas/json

stack: 'ResolverError: Error opening file "/path/to/my/project/root/" \n' +
      'EISDIR: illegal operation on a directory, read\n' +
      '    at /path/to/my/project/root/node_modules/@apidevtools/json-schema-ref-parser/lib/resolvers/file.js:52:20\n' +
      '    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:52:12)',
    code: 'ERESOLVER',
    message: 'Error opening file "/path/to/my/project/root/" \n' +

I am using json-schema-to-typescript which uses this library as an underlyging library so I have the same error. Using AJV with the URIs seems to work fine.

If I change the $ref values to be relative, then json-schema-ref-parser compiles ok, but then AJV complains it cannot find the relative links.

  "$ref": "./../parent_folder/folder_2/example_2.json"

I don't want to use FQPN of file/// for multiple reasons which are out of scope here.

Can someone clarify if I am understanding the spec correctly, if what I want to do is possible and if the tooling currently supports it?

@jonluca
Copy link
Collaborator

jonluca commented Mar 6, 2024

This shoudl be fixed in the latest version

@jonluca jonluca closed this as completed Mar 6, 2024
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