Skip to content

Compound documents are bundled incorrectly, but dereferenced correctly. #422

Description

@typetetris

Version used

15.3.5

The input json used

test.json This is

bundling example
from json-schema.org.

{
  "$id": "https://example.com/schemas/customer",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "first_name": { "type": "string" },
    "last_name": { "type": "string" },
    "shipping_address": { "$ref": "/schemas/address" },
    "billing_address": { "$ref": "/schemas/address" }
  },
  "required": ["first_name", "last_name", "shipping_address", "billing_address"],
  "$defs": {
    "address": {
      "$id": "https://example.com/schemas/address",
      "$schema": "http://json-schema.org/draft-07/schema#",
      "type": "object",
      "properties": {
        "street_address": { "type": "string" },
        "city": { "type": "string" },
        "state": { "$ref": "#/definitions/state" }
      },
      "required": ["street_address", "city", "state"],
      "definitions": {
        "state": { "enum": ["CA", "NY", "... etc ..."] }
      }
    }
  }
}

bundling

program

test.cjs
const RefParser = require("@apidevtools/json-schema-ref-parser");
const schema = require("./test.json");


RefParser.bundle(schema).then((result) => console.log(JSON.stringify(result)));

result

Result of bundling beautified by jq
{
  "$id": "https://example.com/schemas/customer",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "first_name": {
      "type": "string"
    },
    "last_name": {
      "type": "string"
    },
    "shipping_address": {
      "$ref": "#"
    },
    "billing_address": {
      "$ref": "#"
    }
  },
  "required": [
    "first_name",
    "last_name",
    "shipping_address",
    "billing_address"
  ],
  "$defs": {
    "address": {
      "$id": "https://example.com/schemas/address",
      "$schema": "http://json-schema.org/draft-07/schema#",
      "type": "object",
      "properties": {
        "street_address": {
          "type": "string"
        },
        "city": {
          "type": "string"
        },
        "state": {
          "$ref": "#/definitions/state"
        }
      },
      "required": [
        "street_address",
        "city",
        "state"
      ],
      "definitions": {
        "state": {
          "enum": [
            "CA",
            "NY",
            "... etc ..."
          ]
        }
      }
    }
  }
}

problem

Notice the {"$ref":"#"} for the shipping_address and billing_address properties.

expected behaviour

Either don't change it, or present valid json pointer references which are applied to the top level document everywhere, which would be good for libraries like RJSF, but might be complicated? (This would probably mean altering the compound schemas to not contain $id any more and changing all the references in them. Probably ok or should bundle guarantee the compound schemas are unchanged?)

dereferencing

program

test.cjs
const RefParser = require("@apidevtools/json-schema-ref-parser");
const schema = require("./test.json");


RefParser.dereference(schema).then((result) => console.log(JSON.stringify(result)));

result

Result of bundling
{
  "$id": "https://example.com/schemas/customer",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "first_name": {
      "type": "string"
    },
    "last_name": {
      "type": "string"
    },
    "shipping_address": {
      "$id": "https://example.com/schemas/address",
      "$schema": "http://json-schema.org/draft-07/schema#",
      "type": "object",
      "properties": {
        "street_address": {
          "type": "string"
        },
        "city": {
          "type": "string"
        },
        "state": {
          "enum": [
            "CA",
            "NY",
            "... etc ..."
          ]
        }
      },
      "required": [
        "street_address",
        "city",
        "state"
      ],
      "definitions": {
        "state": {
          "enum": [
            "CA",
            "NY",
            "... etc ..."
          ]
        }
      }
    },
    "billing_address": {
      "$id": "https://example.com/schemas/address",
      "$schema": "http://json-schema.org/draft-07/schema#",
      "type": "object",
      "properties": {
        "street_address": {
          "type": "string"
        },
        "city": {
          "type": "string"
        },
        "state": {
          "enum": [
            "CA",
            "NY",
            "... etc ..."
          ]
        }
      },
      "required": [
        "street_address",
        "city",
        "state"
      ],
      "definitions": {
        "state": {
          "enum": [
            "CA",
            "NY",
            "... etc ..."
          ]
        }
      }
    }
  },
  "required": [
    "first_name",
    "last_name",
    "shipping_address",
    "billing_address"
  ],
  "$defs": {
    "address": {
      "$id": "https://example.com/schemas/address",
      "$schema": "http://json-schema.org/draft-07/schema#",
      "type": "object",
      "properties": {
        "street_address": {
          "type": "string"
        },
        "city": {
          "type": "string"
        },
        "state": {
          "enum": [
            "CA",
            "NY",
            "... etc ..."
          ]
        }
      },
      "required": [
        "street_address",
        "city",
        "state"
      ],
      "definitions": {
        "state": {
          "enum": [
            "CA",
            "NY",
            "... etc ..."
          ]
        }
      }
    }
  }
}

problems

None I can spot. So json-schema-ref-parser already knows how to resolve those local references in a compound document.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions