Closed thufschmitt closed 3 months ago
Not handling them was in part a conscious decision to not make arbitrary network requests for converting a schema. I was hoping that actual external references would be uncommon in the wild. I suppose, turning them into Dyn
and warning about it might be a good middle ground.
I ran into the same issue. I don't mind inlining external references in a pre-processing steps - that provides an opportunity to separate network access from the schema conversion step. I looked around and found JSON Schema $Ref Parser which handles that pre-processing. Here is a minimal script:
// index.mjs
import $RefParser from "@apidevtools/json-schema-ref-parser"
// Pass a file path, URL, or string as the last CLI argument
const input_schema = process.argv[process.argv.length - 1]
const schema = await $RefParser.bundle(input_schema, { /* options here */ })
console.log(JSON.stringify(schema, null, 2))
Install the $Ref Parser library with:
$ npm install @apidevtools/json-schema-ref-parser
And run it like this,
$ node index.mjs ./schema-with-external-references.jsonschema > inlined.jsonschema
In case anyone needs to tweak the parser options, they are documented here.
For any future reader, I've implemented some missing features around local references recently in js2n and wrote a small wrapper around the JSON schema ref parser lib. For a full Nix-based pipeline, see https://github.com/tweag/nickel-kubernetes/, and in particular the flake.nix
- which mostly just pipes together the json-schema-bundler
NPM wrapper defined in the same repository, following @hallettj 's suggestion, and json-schema-to-nickel. For example we could generate contracts for the whole set of Kubernetes resources
The tool will currently barf if it encounters any external reference.
Handling them might be a pain, but it could be nicer to just ignore the reference (returning a
Dyn
instead?) and keep going.Reproduction: