Skip to content

Using custom AJV breaks resolving references #1855

@jsumners

Description

@jsumners

🐛 Bug Report

When using a custom AJV instance as described in the documentation (https://www.fastify.io/docs/latest/Validation-and-Serialization/#schema-compiler) one cannot provide a schema with references as a response schema.

To Reproduce

'use strict';

const AJV = require('ajv');
const ajv = new AJV();

ajv.addSchema({
  $id: 'urn:schema:foo',
  definitions: {
    foo: { type: 'string' }
  },

  type: 'object',
  properties: {
    foo: { $ref: '#/definitions/foo' }
  }
});

ajv.addSchema({
  $id: 'urn:schema:response',
  type: 'object',
  properties: {
    foo: { $ref: 'urn:schema:foo#/definitions/foo' }
  }
});

const fastify = require('fastify')();
fastify.setSchemaCompiler(schema => ajv.compile(schema));

fastify.route({
  path: '/',
  method: 'GET',
  schema: {
    response: {
      '2xx': ajv.getSchema('urn:schema:response').schema
    }
  },
  handler(req, reply) {
    reply.send({ foo: 'bar' });
  }
});

fastify.inject({ url: '/' }, (err, response) => {
  if (err) {
    throw err;
  }
  console.log(response.body);
});

Expected behavior

The route should register and resolve references correctly.

Paste the results here:

/private/tmp/17/fastify-custom-ajv/index.js:44
    throw err;
    ^
FastifyError [FST_ERR_SCH_BUILD]: FST_ERR_SCH_BUILD: Failed building the schema for GET: /, due error Cannot read property '$ref' of undefined
    at Object.afterRouteAdded (/private/tmp/17/fastify-custom-ajv/node_modules/.registry.npmjs.org/fastify/2.8.0/node_modules/fastify/lib/route.js:224:37)
    at after (/private/tmp/17/fastify-custom-ajv/node_modules/.registry.npmjs.org/fastify/2.8.0/node_modules/fastify/lib/route.js:165:25)
    at Object._encapsulateThreeParam (/private/tmp/17/fastify-custom-ajv/node_modules/.registry.npmjs.org/avvio/6.2.2/node_modules/avvio/boot.js:417:7)
    at Boot.callWithCbOrNextTick (/private/tmp/17/fastify-custom-ajv/node_modules/.registry.npmjs.org/avvio/6.2.2/node_modules/avvio/boot.js:339:5)
    at Boot._after (/private/tmp/17/fastify-custom-ajv/node_modules/.registry.npmjs.org/avvio/6.2.2/node_modules/avvio/boot.js:230:26)
    at Plugin.exec (/private/tmp/17/fastify-custom-ajv/node_modules/.registry.npmjs.org/avvio/6.2.2/node_modules/avvio/plugin.js:89:17)
    at Boot.loadPlugin (/private/tmp/17/fastify-custom-ajv/node_modules/.registry.npmjs.org/avvio/6.2.2/node_modules/avvio/plugin.js:175:10)
    at Task.release (/private/tmp/17/fastify-custom-ajv/node_modules/.registry.npmjs.org/fastq/1.6.0/node_modules/fastq/queue.js:127:16)
    at worked (/private/tmp/17/fastify-custom-ajv/node_modules/.registry.npmjs.org/fastq/1.6.0/node_modules/fastq/queue.js:169:10)
    at toLoad.finish (/private/tmp/17/fastify-custom-ajv/node_modules/.registry.npmjs.org/avvio/6.2.2/node_modules/avvio/plugin.js:178:7)

Your Environment

  • node version: 10.16.3
  • fastify version: 2.8.0
  • os: Mac

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