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

Repeating relative $refs with id's result in invalid json schema generation #355

Open
phawxby opened this issue Jul 30, 2024 · 0 comments
Open

Comments

@phawxby
Copy link

phawxby commented Jul 30, 2024

Sample
https://github.com/phawxby/json-ref-example/blob/main/src/example.ts

What I think is happening here is that the repetition of the reference to root.schema.json is being resolved down to $refs in later references. However because those sub schemas (sub1.schema.json) include as $id that changes the base uri for resolution. The result is #/oneOf/0/allOf/0 is being resolved from sub2.schema.json and not from root.schema.json.

When passed into AJV this then results in.

MissingRefError: can't resolve reference #/oneOf/0/allOf/0 from id sub2.schema.json

Removing the $id from sub*.schema.json which prevents the base uri from being changed corrects the issue.

Compiled output

{
  '$id': 'root.schema.json',
  '$schema': 'https://json-schema.org/draft/2019-09/schema',
  type: 'object',
  oneOf: [
    {
      '$id': 'sub1.schema.json',
      '$schema': 'https://json-schema.org/draft/2019-09/schema',
      type: 'object',
      allOf: [
        {
          '$id': 'base.schema.json',
          '$schema': 'https://json-schema.org/draft/2019-09/schema',
          type: 'object',
          properties: { foo: { type: 'string' } }
        },
        { properties: { name: { type: 'string', const: 'sub1' } } }
      ]
    },
    {
      '$id': 'sub2.schema.json',
      '$schema': 'https://json-schema.org/draft/2019-09/schema',
      type: 'object',
      allOf: [
        { '$ref': '#/oneOf/0/allOf/0' },
        { properties: { name: { type: 'string', const: 'sub2' } } }
      ]
    },
    {
      '$id': 'sub3.schema.json',
      '$schema': 'https://json-schema.org/draft/2019-09/schema',
      type: 'object',
      allOf: [
        { '$ref': '#/oneOf/0/allOf/0' },
        { properties: { name: { type: 'string', const: 'sub3' } } }
      ]
    }
  ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant