Skip to content

Schema absolute references failure #2446

@jsumners

Description

@jsumners

🐛 Bug Report

When registering routes with schemas that include properties with absolute references to other schemas the following error is generated:

FastifyError [Error]: Failed building the serialization schema for GET: /, due to error Bad arguments: First argument should be a string, second should be an array of strings
    at Boot.<anonymous> (/private/tmp/29/schema-ref-test/node_modules/fastify/lib/route.js:271:19)

To Reproduce

Steps to reproduce the behavior:

"use strict";

const AJV = require("ajv");
const ajvInstance = new AJV();

const baseSchema = {
  $id: "http://example.com/schemas/base",
  definitions: {
    hello: { type: "string" },
  },
  type: "object",
  properties: {
    hello: { $ref: "#/definitions/hello" },
  },
};

const refSchema = {
  $id: "http://example.com/schemas/ref",
  type: "object",
  properties: {
    hello: { $ref: "http://example.com/schemas/base#/definitions/hello" },
  },
};

ajvInstance.addSchema(baseSchema);
ajvInstance.addSchema(refSchema);

/***********/

const fastify = require("fastify")({ logger: { level: "debug" } });
fastify.setValidatorCompiler(function ({ schema }) {
  return ajvInstance.compile(schema);
});

fastify.route({
  path: "/",
  method: "GET",
  schema: {
    response: {
      "2xx": ajvInstance.getSchema("http://example.com/schemas/ref").schema,
    },
  },
  handler(req, res) {
    res.send({ hello: "world" });
  },
});

fastify.inject({ url: "/" }, (err, response) => {
  if (err) throw err;

  console.log(response);
});

Expected behavior

The route schema should resolve any references without crashing.

Your Environment

  • node version: 12
  • fastify version: >=3.1.1
  • os: Mac

Metadata

Metadata

Assignees

Labels

discussionIssues or PRs with this label will never stalefeature requestNew feature to be added

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions