hyperjump-io / json-schema

JSON Schema Validation, Annotation, and Bundling. Supports Draft 04, 06, 07, 2019-09, 2020-12, OpenAPI 3.0, and OpenAPI 3.1
https://json-schema.hyperjump.io/
MIT License
224 stars 21 forks source link

schema doesn't get included in bundle #67

Closed the42 closed 4 months ago

the42 commented 4 months ago

I am using a small helper to bundle schemas, at the bottom. Unfortunately the bundled schema doesn't contain one of the to be bundled schemas.

I tried to re-register the schema, which results in an already registered error, so the schema is read. However, when I call bundle, it is not contained.

The schema A has $id: "https://assets.wko.at/schemas/aw/aw_catalogs" and doesn't get included. Schema B has $id:"https://assets.wko.at/schemas/aw/aw" and $refs A as "/schemas/aw/aw_catalogs#/$defs/Niederlassungstyp"

Maybe within B I $ref elements in A of this schema in an erroneous way, which results in this schema not be exported/bundled?

import { readFileSync } from "node:fs";
import { registerSchema } from "@hyperjump/json-schema/draft-2020-12";
import { bundle } from "@hyperjump/json-schema/bundle";
import  minimist  from "minimist";

var argv = minimist(process.argv.slice(2));
var rooturi = argv['rooturi'];

if(rooturi==undefined) {
    console.log('required argument "--rooturi" not provided\n');
    console.log('\tusage: sbundle --rooturi="rooturi" file1.json file2.json ...\n');
    process.exit(-1);
}

for(const file of argv._) {
    registerSchema(JSON.parse(readFileSync(file, "utf8")))
}

const bundledSchema = await bundle(rooturi);

console.log(JSON.stringify(bundledSchema, null, 2));
the42 commented 4 months ago

Well, I was a bit terse about the error, mostly because my setup is not that trivial. I nailed the problem futher down, when a schema is transitively $ref ed, it doesn't get included, so

Schema A --> refs Schema B --> refs Schema C

I a call bundle with Schema a as main uri, Schema C is not included.

However, if I change Schema A to

Schema A --> refs Schema B Schema A --> refs Schema C

all is bundled up. So it might be a transitive referencing issue?

jdesrosiers commented 4 months ago

Transitive references works in general. I have tests to verify that it does. It's possible that there's a bug in some edge case, but it could also be an error in the schema. There are cases where something could look like a reference, but isn't a reference in that context and so the bundler wouldn't detect it. For example: "const": { "$ref": "./my-schema" }. I'd look for things like that in your schema. Without more information, that's the best help I can offer. If you can provide a minimal example to reproduce the error, I can be of more help.