Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid references in schema after using TypeBox Modules #854

Open
2 tasks done
zdila opened this issue Jan 15, 2025 · 1 comment
Open
2 tasks done

Invalid references in schema after using TypeBox Modules #854

zdila opened this issue Jan 15, 2025 · 1 comment
Labels
bug Confirmed bug

Comments

@zdila
Copy link

zdila commented Jan 15, 2025

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.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 zdila changed the title Invalid schema Invalid references in schema after using TypeBox Modules Jan 15, 2025
@mcollina
Copy link
Member

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

@mcollina mcollina added the bug Confirmed bug label Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug
Projects
None yet
Development

No branches or pull requests

2 participants