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

Does not resolve a top level $ref to a definition when using dereference

Open JaredAAT opened this issue 3 years ago • 1 comments

I don't know enough of the technical terms sorry, but i'm not sure if this is the correct behaviour. I have a schema that contains a $ref to a definition:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "$ref": "#/definitions/PricingRuleDetail",
  "definitions": {
        "PricingRuleDetail": {
           "type": "string"
         }
   }
}

and when using this code:

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

const main = async () => {
  const file = 'schema.json'
  const deReferencedSchema = await $RefParser.dereference(`./schemas/${file}`, {})
}

main()

it returns me a schema like

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "$ref": "#",
  "definitions": {
        "PricingRuleDetail": {
           "type": "string"
         }
   }
}

I would expect the schema to be resolved like:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "PricingRuleDetail": {
           "type": "string"
  },
  "definitions": {
        "PricingRuleDetail": {
           "type": "string"
         }
   }
}

Though i suspect i'm asking too much here... Is there a different tool or library I should use to get my expected result?

JaredAAT avatar Sep 27 '22 13:09 JaredAAT

Encountered the same issue, looks like top-level refs aren't supported very well, see https://github.com/APIDevTools/json-schema-ref-parser/issues/201, https://github.com/APIDevTools/json-schema-ref-parser/issues/259, https://github.com/APIDevTools/json-schema-ref-parser/issues/48.

abraha2d avatar Jan 27 '23 05:01 abraha2d