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

Conversation

ivan-tymoshenko
Copy link
Member

@ivan-tymoshenko ivan-tymoshenko commented Jun 29, 2022

Checklist

Fix #481
Fix fastify/fastify#4092

#480 sets merged schema as a location, but doesn't set json pointer to the root of this merged schema.

@@ -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.

Copy link
Member

@climba03003 climba03003 left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina merged commit bf43750 into master Jun 29, 2022
@mcollina mcollina deleted the reset-json-pointer-in-merged-allOf-schema branch June 29, 2022 14:58
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

Successfully merging this pull request may close these issues.

Ajv validation inside allOf Serialization issues after the recent fast-json-stringify update
4 participants