0
Fork 0
mirror of https://github.com/mastra-ai/mastra.git synced 2026-11-17 08:44:46 +00:00

(fix): Fix duplicate null types in JSON Schema generation (#3258)

## Problem
When converting Zod schemas back to JSON Schema, fields with `type:
"null"` were being processed incorrectly, resulting in invalid JSON
Schema output like `{ "type": ["null", "null"] }`. This happens because
these fields were being made both nullable and optional, effectively
duplicating the null type in the array of possible types.

## Solution
Added a check to detect when a field's type is explicitly "null" and
make it required in those cases, preventing the additional nullable
modifier from being applied. This ensures the generated JSON Schema
remains valid and accurately represents the intended type structure.

Example:
```json
// Before (invalid)
{
  "myField": {
    "type": ["null", "null"]
  }
}

// After (valid)
{
  "myField": {
    "type": "null"
  }
}
```

## Changes
- Added `isTypeRequired` check in `jsonSchemaToModel` to detect explicit
null types
- Modified field generation logic to skip nullable/optional modifiers
for null-typed fields

This fix ensures proper JSON Schema generation while maintaining
backward compatibility with existing schema definitions.

---------

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>
This commit is contained in:
Marcos Candeia 2025-03-25 16:29:15 -03:00 • committed by GitHub
commit a8bd4cfce8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 1 deletions