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

fix: reset json pointer in merged allOf schema #482

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ function mergeAllOfSchema (location, schema, mergedSchema) {
mergedSchema.$id = `merged_${randomUUID()}`
ajvInstance.addSchema(mergedSchema)
location.schemaId = mergedSchema.$id
location.jsonPointer = '#'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it be problem of deeply nested schema?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should work with nested schemas. Can you provide an example of what you mean?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get my answer when I create the example.
Here comes one problem related to previous fix.

Recursive $ref will be a problem when using allOf.
It may not be a good usage.

{
  $id: 'base',
  anyOf: [
    {
      type: 'object',
      allOf: [
        {
          properties: {
            a: {
              anyOf: [
                { const: 'A1' },
                { const: 'A2' }
              ]
            }
          }
        },
        {
          properties: {
            b: { const: 'B' }
          }
        },
        {
          properties: {
            c: {
              oneOf: [
                { $ref: 'base' },
                { type: 'null' }
              ]
            }
          }
        }
      ]
    }
  ]
}

{
    $id: 'base',
    type: 'object',
    allOf: [
      {
        properties: {
          a: {
            anyOf: [
              { const: 'A1' },
              { const: 'A2' }
            ]
          }
        }
      },
      {
        properties: {
          b: { const: 'B' }
        }
      },
      {
        properties: {
          c: {
            oneOf: [
              { $ref: 'base' },
              { type: 'null' }
            ]
          }
        }
      }
    ]
  }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can fix this. But there are some other problems with merging schemas that we should also. We can't use deepMerge for merging. I was trying to make it better #453, but it's not enough. We can check how json-schema-merge-allof works.

In general, we have some places where we have troubles (if/then, oneOf, anyOf, allOf, type array), bc json schema format designed for validation and described input types and we use json schema for serialization and ask people to describe output types.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know another good library that uses json schema for serialization. @climba03003 @Eomm @mcollina If you know, please share a link. It will help in the design process.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it worth an issue for discussion.

}

function buildInnerObject (location) {
Expand Down
35 changes: 35 additions & 0 deletions test/ref.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1891,3 +1891,38 @@ test('input schema is not mutated', (t) => {
t.equal(output, '{"obj":"test"}')
t.same(schema, clonedSchema)
})

test('anyOf inside allOf', (t) => {
t.plan(1)

const schema = {
anyOf: [
{
type: 'object',
allOf: [
{
properties: {
a: {
anyOf: [
{ const: 'A1' },
{ const: 'A2' }
]
}
}
},
{
properties: {
b: { const: 'B' }
}
}
]
}
]
}

const object = { a: 'A1', b: 'B' }
const stringify = build(schema)
const output = stringify(object)

t.equal(output, JSON.stringify(object))
})