Skip to content

Route methods do not consider prefix in parent instance #23

Description

@revosw

In a setup I have, I make a subroute under /admin/:id. The normal instance.get() method in the subroute recognizes that the prefix has been added, so /ventilator is appended to /admin/:id. However, instance.zod.get() does not recognize the prefix, and insists that I have to assign /admin/:id/ventilator as the path.

export const runServer = async () => {
  const fastify = Fastify()

  // fastify-zod integration
  await register(fastify, {
    jsonSchemas: buildJsonSchemas(schemas),
    swaggerOptions: {
      routePrefix: '/swagger',
      exposeRoute: true,
    }
  })

  fastify.register(adminRoutes, {
    prefix: '/admin/:id'
  })

  const port = 3000
  fastify.listen({ port }, err => {
    if (err) throw err
    console.log(`Listening on port ${port}`)
  })
}

runServer()
import { FastifyPluginAsync } from 'fastify'
import { ObjectId } from 'mongodb'
import { Ventilator, ventilator } from '../../schemas/ventilator/ventilator'

const adminRoutes: FastifyPluginAsync = async (instance) => {
    // {"message":"Route GET:/admin/62765ac9c76bdc1796d81b89/ventilator not found","error":"Not Found","statusCode":404}
    instance.zod.get('/ventilator', {operationId: 'getVentilator', params: 'GetVentilatorHandlerParams', reply: 'ventilatorWithId'}, async ({params}) => {
        const document = await instance.mongo.db?.collection<Ventilator>('ventilators').findOne({vid: new ObjectId(params.vid)})
        if (document == null) throw new Error('Could not retrieve document.')
        return document
    })
    // {"vid":"62765ac9c76bdc1796d81b89"}
    instance.get<{Params: {vid: ObjectId, ventilatorid: ObjectId, itemid: ObjectId}}>('/ventilator', (req, res) => {
        res.send(req.params)
    })
}


export default adminRoutes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions