Skip to content

fix: extract nested schemaTypes from arrays in Fastify routes#12

Merged
eropple merged 1 commit intomainfrom
ed/typed-array-extraction-fix
Feb 5, 2026
Merged

fix: extract nested schemaTypes from arrays in Fastify routes#12
eropple merged 1 commit intomainfrom
ed/typed-array-extraction-fix

Conversation

@eropple
Copy link
Owner

@eropple eropple commented Feb 5, 2026

Summary

Fixes a bug where nested schemaType-wrapped schemas inside Type.Array were not being extracted to components/schemas when used in Fastify routes.

Example that was broken:

const Inner = schemaType("Inner", Type.Object({ foo: Type.String() }));
const Outer = schemaType("Outer", Type.Object({ 
  items: Type.Array(Inner)  // Inner was not extracted!
}));

Root Cause

findTaggedSchemasInSchemas used TypeBox's Type.IsArray() to detect array schemas. However, Type.IsArray() relies on an internal ~kind property that Fastify strips during schema processing. This caused the array detection to fail, so nested schemas within arrays were never discovered.

Fix

Replace Type.IsArray(s) with a direct JSON Schema check: s.type === 'array' && s.items !== undefined. This approach works regardless of whether TypeBox's internal metadata is present.

Test Plan

  • Added test for basic nested schemaType in arrays
  • Added test for deeply nested arrays (array of arrays)
  • Added test for nullable arrays via Type.Union
  • Added test that schema symbols survive route handler processing
  • All 80 tests pass

When a schemaType-wrapped schema was used in a Fastify route and
contained a Type.Array with another schemaType inside, the inner
schema was not being extracted to components/schemas.

Root cause: findTaggedSchemasInSchemas used TypeBox's Type.IsArray()
which relies on an internal ~kind property that Fastify strips during
schema processing.

Fix: Replace Type.IsArray(s) with a direct JSON Schema check
(s.type === 'array' && s.items !== undefined) that works regardless
of whether TypeBox metadata is present.

Added tests for:
- Basic nested schemaType in arrays
- Deeply nested arrays (array of arrays)
- Nullable arrays via Type.Union
@eropple eropple merged commit 4680d90 into main Feb 5, 2026
4 checks passed
@eropple eropple deleted the ed/typed-array-extraction-fix branch February 5, 2026 01:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant