Skip to content

Local references in external schemas are not resolved. #9

@csdebruin

Description

@csdebruin
    const resolvedSchema = resolver.resolve(
        {
            "$id": "test",
            "title": "Test",
            "type": "object",
            "properties": {
                "TestString": { "$ref": "DefSchema#/$defs/Username" }
            }                      
        }, 
        {
            externalSchemas: [
                {
                    "$id": "DefSchema",
                    "$defs": {
                        "Username": { "$ref": "#/$defs/string:10" },
                        "string:10": {
                            "type": "string",
                            "maxLength": 10
                        }
                    }
                }
            ]
        }
    )

Produces the following incorrect reference:

{
  "$id": "test",
  "definitions": {
     "def-0": {
      "$defs": {
        "Username": { "$ref": "#/$defs/string:10" }
      }
    }
  }
}

As opposed to the expected:

{
  "$id": "test",
  "definitions": {
     "def-0": {
      "$defs": {
        "Username": { "$ref": "#/definitions/def-0/$defs/string:10" }
      }
    }
  }
}

Activity

Eomm

Eomm commented on Oct 21, 2023

@Eomm
Owner

Finally I was able to replicate it:

const RefResolver = require('json-schema-resolver')

const ref = RefResolver({
  clone: true, // Clone the input schema without changing it. Default: false,
  buildLocalReference (json, baseUri, fragment, i) {
    return `def-${i}` // default value
  }
})

const inputSchema = {
  "$id": "http://example.com/root.json",
  "definitions": {
    "A": { "$id": "#foo" },
    "B": {
      "$id": "other.json",
      "definitions": {
        "X": { "$id": "#bar", type: 'string' },
        "Y": { "$id": "t/inner.json", type: 'boolean' }
      }
    },
    "C": {
      "$id": "urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f",
      type: 'object',
    }
  }
}

const addresSchema = {
  $id: 'relativeAddress', // Note: prefer always absolute URI like: http://mysite.com
  type: 'object',
  properties: {
    zip: { $ref: 'urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f' },
    zip2: { $ref: 'http://example.com/t/inner.json' },
    city: { $ref: 'http://example.com/root.json#foo' }
  }
}

const singleSchema = ref.resolve(addresSchema, { externalSchemas: [inputSchema] })
console.log(singleSchema)
{
  '$id': 'relativeAddress',
  type: 'object',
  properties: {
    zip: { '$ref': '#/definitions/def-5' },
    zip2: { '$ref': '#/definitions/def-4' },
    city: { '$ref': '#/definitions/def-0foo' } ❗️ the $ref has the additional `foo` 
  },
  definitions: {
    'def-0': {
      '$id': 'http://example.com/root.json',
      definitions: [Object],
      [Symbol(json-schema-resolver.refToDef)]: 'def-0',
      [Symbol(json-schema-resolver.consumed)]: true
    },
    'def-4': {
      '$id': 't/inner.json',
      type: 'boolean',
      [Symbol(json-schema-resolver.refToDef)]: 'def-4',
      [Symbol(json-schema-resolver.consumed)]: true
    },
    'def-5': {
      '$id': 'urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f',
      type: 'object',
      [Symbol(json-schema-resolver.refToDef)]: 'def-5',
      [Symbol(json-schema-resolver.consumed)]: true
    }
  },
  [Symbol(json-schema-resolver.ignore)]: true
}
added
bugSomething isn't working
on Oct 21, 2023
self-assigned this
on Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @Eomm@csdebruin

      Issue actions

        Local references in external schemas are not resolved. · Issue #9 · Eomm/json-schema-resolver