Null entry in schema content map crashes startup with TypeError instead of FST_ERR_SCH_CONTENT_MISSING_SCHEMA
Maintainers usually reply within 1 day
@Ganesh1729-ui is already working on this.
Since Nov 22, 2026.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- javascript, nodejs
- Domain
- backend
Research direction
The four dereference sites are named in the report: lib/schemas.js lines 86 and 109 (body and response content in normalizeSchema) and lib/validation.js lines 34 and 330 (compileSchemasForSerialization and compileSchemasForValidation). Read the existing FST_ERR_SCH_CONTENT_MISSING_SCHEMA branches around each site to confirm optional chaining lets them catch the null case, then mirror the {} cases already covered in test/schema-feature.test.js and test/schema-serialization.test.js with null and undefined entries for both body and response. Done means fastify.ready returns that error code for each case. Note an open pull request (#7247) already targets this, so check it before starting.
Written by the indexing model from the issue text.
Description
Bug Report
Describe the bug
When a route schema uses a per-content-type map (schema.body.content or schema.response[code].content) and one of the entries is null instead of an object with a schema key, Fastify crashes at startup with a raw TypeError instead of the documented FST_ERR_SCH_CONTENT_MISSING_SCHEMA error.
Steps to reproduce
const Fastify = require('fastify')
const fastify = Fastify()
fastify.post('/', {
handler: (req, reply) => reply.send(req.body),
schema: { body: { content: { 'application/json': null } } }
})
fastify.ready((err) => {
console.log(err && err.code, '|', err && err.message)
})
The same crash happens on the response side with schema: { response: { 200: { content: { 'application/json': null } } } }.
Expected behavior
fastify.ready should receive the documented error: code FST_ERR_SCH_CONTENT_MISSING_SCHEMA, message Schema is missing for the content type 'application/json'. That is what already happens when the entry is an empty object ({}), covered by test/schema-feature.test.js, and when the entry has no schema key, covered by test/schema-serialization.test.js.
Actual behavior
fastify.ready receives a raw TypeError: Cannot read properties of null (reading 'schema'), with no error code. The same happens when the entry value is undefined.
Root cause
The content map entries are dereferenced without a null check in four places:
lib/schemas.js:86(normalizeSchema, body content):contentProperty[contentType].schemalib/schemas.js:109(normalizeSchema, response content):contentProperty[mediaName].schemalib/validation.js:330(compileSchemasForValidation, body content):contentProperty[contentType].schemalib/validation.js:34(compileSchemasForSerialization, response content):schema.content[mediaName].schema
Reading .schema with optional chaining (?.schema) at these sites lets the existing FST_ERR_SCH_CONTENT_MISSING_SCHEMA check handle the null case, matching the behavior for {} entries.
Versions
- Fastify version: 6.0.0-alpha.6 (main at 6de5abe, 2026-11-21)
- Node.js version: 24.22.1
- Dominant language
- JavaScript
- Stars
- 37.5k
- Forks
- 3.2k
- Avg merge
- 2d 1h
- Merged PRs (30d)
- 22
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from fastify/fastify
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Document using preClose to stop starting new plugin work during shutdownPossibly taken @lazerg claimed this 49 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 69/100
fastify/fastify#7083 · 4 comments ·
Maintainers usually reply within 1 day
-
docs(Getting-Started): Install section only documents npm and yarnPossibly taken @Shreya657 claimed this 85 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 94/100
fastify/fastify#6983 · 1 comment ·
Maintainers usually reply within 1 day
-
`path` alias missing from RouteOptions typePossibly taken @veeceey claimed this 285 days ago. Opentypescript
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
fastify/fastify#6012 · 10 comments ·
Maintainers usually reply within 1 day
-
Using $ref in response work really strange/not at allPossibly taken @mcollina claimed this 1 day ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 20/100
Maintainers usually reply within 1 day
Similar issues
-
FEATURE REQUEST
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
linuxmint/cinnamon-spices-applets#9356 ·
Maintainers usually reply within 1 day
-
needs-triage Platform(Default)
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
linagora/twake-drive#4659 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day