Null entry in schema content map crashes startup with TypeError instead of FST_ERR_SCH_CONTENT_MISSING_SCHEMA

Open
#7,246 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@Ganesh1729-ui is already working on this.

Since Nov 22, 2026.

  • #7247 by @Ganesh1729-ui — open
  • #7256 by @seragAhmed — open
  • #7267 by @draarivpatel-ui — open

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].schema
  • lib/schemas.js:109 (normalizeSchema, response content): contentProperty[mediaName].schema
  • lib/validation.js:330 (compileSchemasForValidation, body content): contentProperty[contentType].schema
  • lib/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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from fastify/fastify

All issues in fastify/fastify

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.