'build fastify-swagger scheme for multiple response data with a single 200 code

I use fastify-swagger (https://github.com/fastify/fastify-swagger) for my fastify server. I also use the JSEND (https://github.com/omniti-labs/jsend) standard, which means that the data returned may be different with the same response code 200.

I do not understand how I can build a scheme for multiple response data with a single 200 code.

The Open Api 3 specification (which fastify-swagger supports) seems to allow you to describe this (https://swagger.io/specification /)

enter image description here

I tried this, but it doesn't work. The response from the server is simply not validated and comes as is.

const chema_1 = {
  description: 'description',
  type: 'object',
  properties: {
    status: {
      type: 'string',
      description: 'string'
    },
  }
}

const chema_2 = {
  description: 'description',
  type: 'object',
  properties: {
    body: {
      type: 'string',
      description: 'number'
    },
  }
}

fastify.route({
    method: 'GET',
    url: '/coursesList',
    schema: {
        response: {
        '200': {
            oneOf: [
            chema_1,
            chema_2,
            ]
        }
        }
    },
    handler: async (request, reply) => {
        // handler
    }
})

I also tried this. Same behavior:

const chema_1 = {
  response: {
    '200': {
      description: 'description',
      type: 'object',
      properties: {
        status: {
          type: 'string',
          description: 'string'
        },
      }
    }
  }
}

const chema_2 = {
  response: {
    '200': {
      description: 'description',
      type: 'object',
      properties: {
        body: {
          type: 'string',
          description: 'number'
        },
      }
    }
  }
}

fastify.route({
    method: 'GET',
    url: '/coursesList',
    schema: {
        oneOf: [
            chema_1,
            chema_2,
        ]
    },
    handler: async (request, reply) => {
        // handler
    }
})

There are no such examples in the documentation. Maybe someone can help me?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source