Skip to content

missing swagger route doc when using model[] in response #1147

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

Open
vikyw89 opened this issue Mar 29, 2025 · 4 comments
Open

missing swagger route doc when using model[] in response #1147

vikyw89 opened this issue Mar 29, 2025 · 4 comments
Labels
bug Something isn't working

Comments

@vikyw89
Copy link

vikyw89 commented Mar 29, 2025

What version of Elysia is running?

Latest version: 1.2.25 published 3 weeks ago

What platform is your computer?

ubuntu 24

What steps can reproduce the bug?

import { t } from "elysia";

export const todoSchema = t.Object({
  id: t.String(),
  createdAt: t.Date(),
  updatedAt: t.Date(),
  createdById: t.String(),

  //   data
  title: t.String(),
  description: t.Optional(t.String()),
  assignedAt: t.Optional(t.Date()),
  completedAt: t.Optional(t.Date()),

  //   relations
  SubtaskIds: t.Array(t.String()),
  ParentTaskIds: t.Array(t.String()),
});

export const todoRouter = new Elysia({ prefix: "/todos", tags: ["todos"] })
  .model({
    todo: todoSchema,
  })
  .get(
    "",
    (ctx) => {
      return [
        {
          id: "1",
          createdAt: new Date(),
          updatedAt: new Date(),
          createdById: "1",
          title: "Todo 1",
          description: "Description 1",
          assignedAt: new Date(),
          completedAt: new Date(),
          SubtaskIds: ["1", "2", "3"],
          ParentTaskIds: ["1", "2", "3"],
        },
      ];
    },
    {
      response: "todo[]",
      query: t.Object({
        userId: t.String(),
      }),
    }
  );

add swagger plugin and check the route documentation, it's missing

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

Have you try removing the node_modules and bun.lockb and try again yet?

No response

@vikyw89 vikyw89 added the bug Something isn't working label Mar 29, 2025
@ledihildawan
Copy link

ledihildawan commented Apr 2, 2025

The /todos endpoint doesn't show up in Swagger due to response validation. It should be response: t.Array(todoSchema) instead of response: "todo[]". And your code looks like this:

import Elysia, { t } from 'elysia';

export const todoSchema = t.Object({
  id: t.String(),
  createdAt: t.Date(),
  updatedAt: t.Date(),
  createdById: t.String(),
  title: t.String(),
  description: t.Optional(t.String()),
  assignedAt: t.Optional(t.Date()),
  completedAt: t.Optional(t.Date()),
  SubtaskIds: t.Array(t.String()),
  ParentTaskIds: t.Array(t.String()),
});

export const todoService = new Elysia({ name: 'todos/service' }).model({
  todo: todoSchema,
});

export const todoRouter = new Elysia({ prefix: '/todos', tags: ['todos'] })
  .use(todoService)
  .get(
    '/',
    () => {
      return [
        {
          id: '1',
          createdAt: new Date(),
          updatedAt: new Date(),
          createdById: '1',
          title: 'Todo 1',
          description: 'Description 1',
          assignedAt: new Date(),
          completedAt: new Date(),
          SubtaskIds: ['1', '2', '3'],
          ParentTaskIds: ['1', '2', '3'],
        },
      ];
    },
    {
      response: t.Array(todoSchema),
      query: t.Object({
        userId: t.String(),
      }),
    }
  )
  .get('/:id', ({ params: { id } }) => id, {
    params: t.Object({
      //   id: t.RegExp(/^\d+$/),
      id: t.String({ format: 'regex', pattern: '^\\d+$' }),
    }),
  });

@kravetsone
Copy link
Contributor

The /todos endpoint doesn't show up in Swagger due to response validation. It should be response: t.Array(todoSchema) instead of response: "todo[]". And your code looks like this:

import Elysia, { t } from 'elysia';

export const todoSchema = t.Object({
  id: t.String(),
  createdAt: t.Date(),
  updatedAt: t.Date(),
  createdById: t.String(),
  title: t.String(),
  description: t.Optional(t.String()),
  assignedAt: t.Optional(t.Date()),
  completedAt: t.Optional(t.Date()),
  SubtaskIds: t.Array(t.String()),
  ParentTaskIds: t.Array(t.String()),
});

export const todoService = new Elysia({ name: 'todos/service' }).model({
  todo: todoSchema,
});

export const todoRouter = new Elysia({ prefix: '/todos', tags: ['todos'] })
  .use(todoService)
  .get(
    '/',
    () => {
      return [
        {
          id: '1',
          createdAt: new Date(),
          updatedAt: new Date(),
          createdById: '1',
          title: 'Todo 1',
          description: 'Description 1',
          assignedAt: new Date(),
          completedAt: new Date(),
          SubtaskIds: ['1', '2', '3'],
          ParentTaskIds: ['1', '2', '3'],
        },
      ];
    },
    {
      response: t.Array(todoSchema),
      query: t.Object({
        userId: t.String(),
      }),
    }
  )
  .get('/:id', ({ params: { id } }) => id, {
    params: t.Object({
      //   id: t.RegExp(/^\d+$/),
      id: t.String({ format: 'regex', pattern: '^\\d+$' }),
    }),
  });

Todo[] should work too

Idk why salty merge it without fully testing

@ledihildawan
Copy link

Todo[] should work too

Idk why salty merge it without fully testing

How is it now? Is it working?
It's better to stick with what's shown in the tutorial — it's safer that way.
There might be a bug fix for it in the future.

@vikyw89
Copy link
Author

vikyw89 commented Apr 3, 2025

Image

The autocompletion is there, just when used model[] won't appear in swagger, only "model" appear

But yes that workaround using t.Array(todoSchema) works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants