Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local references in external schemas are not resolved. #9

Open
csdebruin opened this issue Jan 2, 2023 · 1 comment
Open

Local references in external schemas are not resolved. #9

csdebruin opened this issue Jan 2, 2023 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@csdebruin
Copy link

    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" }
      }
    }
  }
}
@Eomm
Copy link
Owner

Eomm commented Oct 21, 2023

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
}

@Eomm Eomm added the bug Something isn't working label Oct 21, 2023
@Eomm Eomm self-assigned this Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants