Skip to content

Recursive Schema - Cannot locate inline $id references #410

@sinclairzx81

Description

@sinclairzx81

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

[email protected]

Plugin version

n/a

Node.js version

16.13.1

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

10

Description

Hi. I'm currently looking at recursive schema composition in TypeBox, and have been carrying out a few tests in Fastify. Have run into an issue in fast-json-stringify where it seems unable to locate a previously defined identifier for an inline (non-external) reference.

Also may have picked up on a small type error where $id is not a valid property which makes passing top level $id for general schemas not possible.

image

Related Issue

#384

Steps to Reproduce

The following is the minimal repro replicating the following schema type.

interface Node {
  id: string
  nodes: Node[]
}
import fastJson from 'fast-json-stringify'

const stringify = fastJson({
    "$id": "Node",
    "type": "object",
    "properties": {
        "id": {
            "type": "string"
        },
        "nodes": {
            "type": "array",
            "items": {
                "$ref": "Node"
            }
        }
    },
    "required": [
        "id",
        "nodes"
    ]
})

Results in the following error

        schema = externalSchema[ref[0]];
                               ^

TypeError: Cannot read properties of undefined (reading 'Node')

Expected Behavior

fast-json-stringify should be able to accept schemas of this form.

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