Fastify's swagger does not load when using zod-openapi auto registering schema with @fastify/[email protected]
First start the fastify server
npm install
npm serveThen try to open the swagger page: http://127.0.0.1:5000/documentation
The error "Failed to load API definition." should appear.
Since @fastify/swagger version 9.2.0 that contains this PR fastify/fastify-swagger#826, when a response with no description contains a reference, it will try to get the description from the reference.
But this happen AFTER fastify-zod-openapi had add the ref in the response, but BEFORE fastify-zod-openapi created the actual component. So fastify/swagger try here https://github.com/fastify/fastify-swagger/blob/master/lib/util/resolve-schema-reference.js#L13 to get a defnition on a component that does not exist yet.
This description fallback happens in this function call that is between the 2 transformation steps of fastify-zod-openapi here and here
Downgrade @fastify/swagger from 9.2.0 to 9.1.0
The fix can be easy by simply change in https://github.com/fastify/fastify-swagger/blob/master/lib/util/resolve-schema-reference.js from
return resolvedReference.definitions[schemaId]to this
return resolvedReference.definitions?.[schemaId]