Skip to content

[bug] Swagger plugin doesn't respect TypeBox types #220

@madkarmaa

Description

@madkarmaa

I've looked at both Elysia's docs (here and here) and TypeBox's docs, but I can't seem to be able to make Swagger document the correct types.

  • Bun version: 1.2.17
  • ElysiaJS version: 1.3.5
  • @elysiajs/swagger version: 1.3.0

Image

src/common-responses.ts

import { t } from 'elysia';

export const ActionResponseSchema = (message: string) =>
    t.Object({ message: t.Literal(message) }, { description: message });

src/routes/users/index.ts

import { t } from 'elysia';
import { tag } from '@utils';
import { findUsers } from '@db/queries/users';

export const SafeUserSchema = t.Object(
    {
        id: t.Number(),
        username: t.String(),
        display_name: t.Nullable(t.String()),
        permissions: t.Union([t.Literal('user'), t.Literal('admin')]),
    },
    { description: 'The user details' }
);

export default (route: App) =>
    route.get('/', async () => await findUsers({}), {
        detail: {
            tags: tag('Users'),
        },
        response: {
            200: t.Array(SafeUserSchema, { description: 'The list of users' }),
        },
    });

src/routes/users/[id]/(get).ts

import { t } from 'elysia';
import { tag } from '@utils';
import { findUserById } from '@db/queries/users';
import { HTTPError } from '@middlewares/errors';
import { SafeUserSchema } from '..';
import { ActionResponseSchema } from '@responses';

export default (route: App) =>
    route.get(
        '/',
        async ({ params }) => {
            const user = await findUserById(params.id);
            if (!user) throw new HTTPError('NOT_FOUND', 'User not found');
            return user;
        },
        {
            params: t.Object({ id: t.Integer() }),
            detail: { tags: tag('Users') },
            response: {
                200: SafeUserSchema,
                404: ActionResponseSchema('User not found'),
            },
        }
    );

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