Skip to content

Zod transform() causes response schema to lose type information in OpenAPI spec #893

@aon

Description

@aon

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

5.5.0

Plugin version

No response

Node.js version

22

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

15

Description

When using @fastify/swagger with fastify-type-provider-zod, Zod schemas that include .transform() methods result in incorrect OpenAPI specifications. The transformed fields in response schemas are rendered as empty objects {} instead of maintaining their proper type information, while request schemas work correctly.

Expected Behavior

Both request and response schemas should maintain type information for transformed fields:

"stringWithTransform": {
  "type": "string"
}

Actual Behavior

  • ✅ Request body schema: Correctly shows "type": "string"
  • ❌ Response schema: Shows empty object {}
{
  "paths": {
    "/": {
      "post": {
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "properties": {
                  "stringWithTransform": {
                    "type": "string"  // ✅ Correct
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "content": {
              "application/json": {
                "schema": {
                  "properties": {
                    "stringWithTransform": {}  // ❌ Bug: Missing type
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Environment

  • Node version: 22
  • @fastify/swagger: 9.5.1
  • fastify: 5.5.0
  • fastify-type-provider-zod: 6.0.0
  • zod: 4.1.5

Link to code that reproduces the bug

https://github.com/aon/fastify-swagger-zod-transform-bug

Additional context

I've reported this issue as well in turkerdev/fastify-type-provider-zod#208

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions