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

Bundle produces invalid output when there are additional properties on $ref object pointing to a recursive schema

Open mickdekkers opened this issue 7 years ago • 2 comments

Repro here: https://github.com/mickdekkers/json-schema-ref-parser-extra-ref-props-issue

Issue description

json-schema-ref-parser's .bundle() method seems to have trouble with additional properties on $ref objects in certain cases where recursive schemas are used. For example with a linked list schema, which references itself on the next property.

a graph of the references of a potentially failing schema

This is a schema for a linked list node:

{
    "$id": "Node.schema.json",
    "definitions": {
        "node": {
            "type": "object",
            "properties": {
                "data": {
                    "description": "Some data",
                    "type": "integer"
                },
                "next": {
                    "$ref": "#/definitions/node"
                }
            }
        }
    }
}

If that schema is then referenced through a $ref object happens to contain an additional property, in this case title:

{
    "$id": "LinkedList.schema.json",
    "definitions": {
        "linkedList": {
            "$ref": "Node.schema.json#/definitions/node",
            "title": "linkedList"
        }
    }
}

The resulting bundle that json-schema-ref-parser produces looks like this:

{
    "$id": "LinkedList.schema.json",
    "definitions": {
        "linkedList": {
            "$ref": "#/definitions/linkedList/properties/next",
            "title": "linkedList"
        }
    }
}

Note how the definition for the node schema isn't included in the bundle, and the $ref object references itself.

Caveats

Interestingly enough, the issue does not occur when the node schema's next $ref object also contains an additional property. It also does not occur when only the node schema's next $ref object contains an additional property.

Here's a table of when the issue occurs:

Folder Setup Issue occurs?
LinkedList Basic linked list schema No
LinkedListRootRefTitle Linked list with additional property on root $ref object (LinkedList.schema.json) Yes
LinkedListNextRefTitle Linked list with additional property on next $ref object (Node.schema.json) No
LinkedListBothRefsTitle Linked list with additional property on both $ref objects No

mickdekkers avatar Feb 09 '18 12:02 mickdekkers

awesome! Thank you for the detailed bug report and repro. 👍 🥇

JamesMessinger avatar Feb 26 '18 11:02 JamesMessinger

@mnaumanali94 can we investigate to make sure this isn't still happening? As OpenAPI v3.1 supports $ref siblings we'll see more people using them soon.

philsturgeon avatar Aug 15 '21 09:08 philsturgeon