Skip to content

Dereference fails to dereference if root object has $ref #172

@hixus

Description

@hixus

Tried to deref json-schema produced by quicktype. Quicktype adds all objects to definitions and puts main object $ref to root. dereference seems to pull main object from root correctly but does not dereference any of the nested refrences. Below is and example of quicktype json-schema.

const $RefParser = require("@apidevtools/json-schema-ref-parser");
const assert = require("assert");

const schemaByQuickType = {
  $schema: "http://json-schema.org/draft-06/schema#",
  $ref: "#/definitions/Row",
  definitions: {
    Row: {
      type: "object",
      additionalProperties: false,
      properties: {
        a: {
          type: "integer"
        },
        b: {
          type: "integer"
        },
        c: {
          $ref: "#/definitions/C"
        }
      },
      required: ["a", "b", "c"],
      title: "Row"
    },
    C: {
      type: "object",
      additionalProperties: false,
      properties: {
        ca: {
          type: "integer"
        },
        cb: {
          type: "integer"
        }
      },
      required: ["ca", "cb"],
      title: "C"
    }
  }
};

(async () => {
  const dereferencedC = {
    type: "object",
    additionalProperties: false,
    properties: { ca: { type: "integer" }, cb: { type: "integer" } },
    required: ["ca", "cb"],
    title: "C"
  };
  const parser = new $RefParser();
  const unexpected = await parser.dereference(schemaByQuickType);
  assert.deepStrictEqual(unexpected.properties.c, dereferencedC); // is actually {'$ref': '#/definitions/C'}

  const { Row, ...restOfTheDefinitions } = schemaByQuickType.definitions;
  const expected = await parser.dereference({
    ...Row,
    definitions: restOfTheDefinitions
  });
  assert.deepStrictEqual(expected.properties.c, dereferencedC); // works as expected
})();

Example also in codesandbox https://codesandbox.io/s/unruffled-keller-jotwi?file=/src/index.js

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions