Skip to content

Using named schema in items field breaks schema validation #1491

@SkeLLLa

Description

@SkeLLLa

🐛 Bug Report

Using some named schema as items in other schema cause exception when using first schema

To Reproduce

Steps to reproduce the behavior:

Add schema with simple item. Then add schema for items array and use first schema $id in items.
Then use the first schema in some route.

Paste your code here:

const fastify = require('fastify')();

(async () => {
  const name = 'item';
  const item = {type: 'object', properties: {foo: {type: 'string'}}};
  fastify.addSchema({
    $id: `${name}`,
    ...item,
  });
  fastify.addSchema({
    $id: `${name}List`,
    type: 'array',
    items: `${name}#`,
  });
  fastify.get(
    '/',
    {
      schema: {
        response: {
          200: `${name}#`,
        },
      },
    },
    async (req, res) => {
      return {};
    }
  );
  await fastify.ready();
  await fastify.listen(3000);
  console.log('app listens');
})();
(node:21665) UnhandledPromiseRejectionWarning: Error: schema is invalid: data.items should be object,boolean, data.items should be array, data.items should match some schema in anyOf
at Ajv.validateSchema (/tmp/xxx/node_modules/ajv/lib/ajv.js:177:16)
at Ajv._addSchema (/tmp/xxx/node_modules/ajv/lib/ajv.js:306:10)
at Ajv.addSchema (/tmp/xxx/node_modules/ajv/lib/ajv.js:136:29)
at Object.keys.forEach.key (/tmp/xxx/node_modules/fast-json-stringify/index.js:937:11)
at Array.forEach (<anonymous>)
at isValidSchema (/tmp/xxx/node_modules/fast-json-stringify/index.js:936:33)
at build (/tmp/xxx/node_modules/fast-json-stringify/index.js:39:3)
at getValidatorForStatusCodeSchema (/tmp/xxx/node_modules/fastify/lib/validation.js:13:10)
at /tmp/xxx/node_modules/fastify/lib/validation.js:19:21
at Array.reduce (<anonymous>)

Expected behavior

No exception thrown.

Your Environment

  • node version: 11
  • fastify version: 2.0.0
  • os: Mac, Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions