nickel-lang / json-schema-to-nickel

Convert JSON schemas into Nickel contracts
Apache License 2.0
29 stars 0 forks source link

Don't plain fail on external references #48

Closed thufschmitt closed 3 months ago

thufschmitt commented 8 months ago

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:

$ json-schema-to-nickel <<EOF
{ "\$ref": "https://raw.githubusercontent.com/microsoft/vscode/main/extensions/configuration-editing/schemas/devContainer.codespaces.schema.json" }
EOF
thread 'main' panicked at 'not implemented', src/definitions.rs:62:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
vkleen commented 8 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.

hallettj commented 6 months ago

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.

yannham commented 2 months ago

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