fastify-swagger icon indicating copy to clipboard operation
fastify-swagger copied to clipboard

Invalid references in schema after using TypeBox Modules

Open zdila opened this issue 10 months ago • 1 comments

Prerequisites

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

Fastify version

5.2.1

Plugin version

9.4.1

Node.js version

22

Operating system

Linux

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

Debian Testing

Description

I am not sure if it is issue of @fastify/type-provider-typebox or of this package. After migrating TypeBox to 0.34 I had to use its Type.Module:

  const Module = Type.Module({
    Foo: Type.Ref("Bar"),
    Bar: Type.String(),
  });

  const Foo = Module.Import("Foo");

  type FooRequest = FastifyRequest<{
    Reply: Static<typeof Foo>;
  }>;

  fastify.withTypeProvider<TypeBoxTypeProvider>().get("/foo", {
    schema: {
      produces: ["application/json"],
      response: {
        200: Foo,
      },
    },

    async handler(_: FooRequest, reply) {
      await reply.send("OK");
    },
  });

Response handling works correctly, but /documentation generates invalid references:

{
  "openapi": "3.1.0",
  "info": {
    "title": "Test",
    "version": "0.1.0"
  },
  "components": {
    "schemas": {}
  },
  "paths": {
    "/foo": {
      "get": {
        "responses": {
          "200": {
            "description": "Default Response",
            "content": {
              "application/json": {
                "schema": {
                  "$defs": {
                    "Foo": {
                      "$ref": "#/components/schemas/def-1",
                      "title": "Foo"
                    },
                    "Bar": {
                      "type": "string",
                      "title": "Bar"
                    }
                  },
                  "$ref": "#/components/schemas/def-0"
                }
              }
            }
          }
        }
      }
    }
  }
}

Link to code that reproduces the bug

included in the description

Expected Behavior

Schema should be valid.

zdila avatar Jan 15 '25 11:01 zdila

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

mcollina avatar Jan 16 '25 16:01 mcollina

I have also noticed the same issue. E.g. I can have following:

export const HumanModule = Type.Module({
  AddressSchema: Type.Object({
    street: Type.String(),
    streetNumber: Type.Number(),
  }),
  PersonSchema: Type.Object({
    name: Type.String(),
    homeAddress: Type.Ref('AddressSchema'),
    workAddress: Type.Ref('AddressSchema'),
  }),
  PostRequestSchema: Type.Object({
    person: Type.Ref('PersonSchema'),
  }),
});

export const PersonResponseSchema = Type.Object({
  status: Type.String(),
});

export const FooSchema = {
  post: {
    request: HumanModule.Import('PostRequestSchema'),
    response: {
      200: PersonResponseSchema,
    },
  },
};

The openapi json is created but the schemas under components part is empty

 "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT"
      }
    },
    "schemas": {}
  },

Without the typebox Module it works:

  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT"
      }
    },
    "schemas": {
      "def-0": {
        "type": "object",
        "properties": {
          "street": {
            "type": "string"
          },
          "streetNumber": {
            "type": "number"
          }
        },
        "required": [
          "street",
          "streetNumber"
        ],
        "title": "address"
      }
    }
  },

I don't know if this is an issue in the typebox Module.Import() or am I missing something from my schema that would fix this

tparvi avatar Jul 03 '25 09:07 tparvi