APIDevTools / json-schema-ref-parser

Parse, Resolve, and Dereference JSON Schema $ref pointers in Node and browsers
https://apitools.dev/json-schema-ref-parser
MIT License
952 stars 227 forks source link

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

Closed JaredAAT closed 6 months ago

JaredAAT commented 1 year ago

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?

abraha2d commented 1 year ago

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.