json-schema icon indicating copy to clipboard operation
json-schema copied to clipboard

Bundling schema with urn ID does not replace urn $ref

Open andlbrei opened this issue 9 months ago • 4 comments

Hi,

I'm trying to bundle a schema with urn identifiers. I added the urn plugin for @hyperjump/browser based on the example. The external schema (Address) is added to $defs in the resulting schema, but the $ref is not changed to refer to $defs. What am I doing wrong?

Combining the two following schemas:

{
	"$schema": "https://json-schema.org/draft/2020-12/schema",
	"$id": "urn:test:something/schemas/CompanyCertificate",
	"type": "object",
	"title": "Company Certificate",
	"description": "It's the Company Certificate",
	"properties": {
		"companyInformation": {
			"type": "object",
			"properties": {
				"businessAddress": {
					"$ref": "urn:test:something/schemas/Address"
				}
			}
		}
	}
}
{
	"$schema": "https://json-schema.org/draft/2020-12/schema",
	"$id": "urn:test:something/schemas/Address",
	"type": "object",
	"required": ["addressLine1", "postalCode", "postalPlace"],
	"properties": {
		"addressLine1": {
			"type": "string"
		},
		"postalCode": {
			"type": "string"
		},
		"postalPlace": {
			"type": "string"
		}
	}
}

With this code:

addUriSchemePlugin("urn", {
  parse: (urn, baseUri) => {
    let { nid, nss, query, fragment } = parseUrn(urn);
    nid = nid.toLowerCase();

    if (!mappings[nid]?.[nss]) {
      throw Error(`Not Found -- ${urn}`);
    }

    let uri = mappings[nid][nss];
    uri += query ? `?${query}` : "";
    uri += fragment ? `#${fragment}` : "";

    return retrieve(uri, baseUri);
  },
});

registerSchema(schema);
registerSchema(Address);

const bundledSchema = await bundle(
  "urn:test:something/schemas/CompanyCertificate"
);

Results in this:

{
	"$schema": "https://json-schema.org/draft/2020-12/schema",
	"type": "object",
	"title": "Company Certificate",
	"description": "It's the Company Certificate",
	"properties": {
		"companyInformation": {
			"type": "object",
			"properties": {
				"businessAddress": { "$ref": "urn:test:something/schemas/Address" }
			}
		}
	},
	"$defs": {
		"Address": {
			"$id": "Address",
			"type": "object",
			"required": ["addressLine1", "postalCode", "postalPlace"],
			"properties": {
				"addressLine1": { "type": "string" },
				"postalCode": { "type": "string" },
				"postalPlace": { "type": "string" }
			}
		}
	}
}

Thanks!

andlbrei avatar May 22 '24 13:05 andlbrei