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

Circluar reference detected one level too late when referencing local schema by name

Open SebastienGllmt opened this issue 7 months ago • 0 comments

What works

The given schema

{
  "$id": "recursive.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "properties": {
    "label": {
      "anyOf": [
        {
          "type": "integer"
        }
      ]
    },
    "value": {
      "$ref": "#"
    }
  },
  "title": "RecursiveType",
  "type": "object"
}

gives the resulting JS object which is properly circular right away (object > [Circular])

<ref *1> {
  '$id': 'recursive.json',
  '$schema': 'https://json-schema.org/draft/2020-12/schema',
  properties: {
    label: { anyOf: [ { type: 'integer' } ] },
    value: [Circular *1]
  },
  title: 'RecursiveType',
  type: 'object'
}

What doesn't work

Change the reference to be by name instead of just #

{
  "$id": "recursive.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "properties": {
    "label": {
      "anyOf": [
        {
          "type": "integer"
        }
      ]
    },
    "value": {
      "$ref": "recursive.json"
    }
  },
  "title": "RecursiveType",
  "type": "object"
}

This instead gives the following TS. Note that it's the same as above EXCEPT it's expanded one level too deep object > object > [Circular]

{
  '$id': 'recursive.json',
  '$schema': 'https://json-schema.org/draft/2020-12/schema',
  properties: {
    label: { anyOf: [ { type: 'integer' } ] },
    value: <ref *1> {
      '$id': 'recursive.json',
      '$schema': 'https://json-schema.org/draft/2020-12/schema',
      properties: {
        label: { anyOf: [ { type: 'integer' } ] },
        value: [Circular *1]
      },
      title: 'RecursiveType',
      type: 'object'
    }
  },
  title: 'RecursiveType',
  type: 'object'
}

SebastienGllmt avatar Apr 06 '25 05:04 SebastienGllmt