Skip to content

mottet/bug-fastify-zod-openapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fastify's swagger crash when using @fastify/swagger 9.2.0 with zod-openapi auto registering schema

What is the bug

Fastify's swagger does not load when using zod-openapi auto registering schema with @fastify/[email protected]

How to reproduce the bug

First start the fastify server

npm install
npm serve

Then try to open the swagger page: http://127.0.0.1:5000/documentation

The error "Failed to load API definition." should appear.

What causes this bug

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

Workaround

Downgrade @fastify/swagger from 9.2.0 to 9.1.0

How to fix it

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]

About

Bug demonstration on auto registering schema since fastify 5

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published