Fastify: array type in response schema

Created on 4 Oct 2017  路  3Comments  路  Source: fastify/fastify

Hi,

I am playing with the framework to know if I should keep using it and this crashes the server when I npm server. Not sure why and I couldn't find anything in the docs. Thanks for your insight!

const userSchema = {
  schema: {
    response: {
      200: {
        type: 'object',
        properties: {
          users: { type: 'array' }
        }
      }
    }
  }
TypeError: Cannot read property '$ref' of undefined
    at buildArray (/Users/om/Documents/projects/ch/node-api/node_modules/fast-json-stringify/index.js:401:19)
    at nested (/Users/m/Documents/projects/ch/node-api/node_modules/fast-json-stringify/index.js:518:19)
    at Object.keys.forEach (/Users/m/Documents/projects/ch/node-api/node_modules/fast-json-stringify/index.js:365:18)
    at Array.forEach (native)
    at buildObject (/Users/m/Documents/projects/ch/node-api/node_modules/fast-json-stringify/index.js:352:40)
    at build (/Users/m/Documents/projects/crowdhub/node-api/node_modules/fast-json-stringify/index.js:56:14)
    at getValidatorForStatusCodeSchema (/Users/m/Documents/projects/ch/node-api/node_modules/fastify/lib/validation.js:15:10)
    at /Users/m/Documents/projects/ch/node-api/node_modules/fastify/lib/validation.js:21:21
    at Array.reduce (native)
    at getResponseSchema (/Users/m/Documents/projects/ch/node-api/node_modules/fastify/lib/validation.js:20:22)
    at build (/Users/m/Documents/projects/ch/node-api/node_modules/fastify/lib/validation.js:44:28)
    at _fastify.after (/Users/m/Documents/projects/ch/node-api/node_modules/fastify/fastify.js:400:7)
    at Function._encapsulateThreeParam (/Users/m/Documents/projects/ch/node-api/node_modules/avvio/boot.js:271:7)
    at Boot.callWithCbOrNextTick (/Users/m/Documents/projects/ch/node-api/node_modules/avvio/boot.js:223:5)
    at Boot._after (/Users/m/Documents/projects/ch/node-api/node_modules/avvio/boot.js:180:26)
    at process.nextTick (/Users/m/Documents/projects/ch/node-api/node_modules/avvio/plugin.js:30:5)

Most helpful comment

I just ran into the same issue and found the solution in this issue.

Just want to put this here for somebody else who might be asking this question...

type : 'array' requires items in a similar fashion as the type object requires properties.

type: 'array',
items: {
  type: 'object',
    properties : {
      name: { type: 'string' },
      nickname: { type: 'string' },
      age: { type: 'integer' }
   }
}

All 3 comments

This seems to be a fast-json-stringify issue: here the code accesses to items['$refs'] directly without checking if items exists.

Please report this as fast-json-stringify bug here

Done. Thanks!

I just ran into the same issue and found the solution in this issue.

Just want to put this here for somebody else who might be asking this question...

type : 'array' requires items in a similar fashion as the type object requires properties.

type: 'array',
items: {
  type: 'object',
    properties : {
      name: { type: 'string' },
      nickname: { type: 'string' },
      age: { type: 'integer' }
   }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

mcollina picture mcollina  路  4Comments

Jegannathan picture Jegannathan  路  3Comments

zairl23 picture zairl23  路  3Comments

moltar picture moltar  路  3Comments

avilang picture avilang  路  3Comments