Skip to content

@elysiajs/openapi couldn't automatically infer response type #322

@ParticleG

Description

@ParticleG

Happens with elysia@1.4.22 and @elysiajs/openapi@1.4.14.

Manually specifying the response property is required to display the response type in OpenAPI; automatic type inference is completely unavailable.

For example:

// src/modules/project/index.ts

export const project = new Elysia({ prefix: '/api/v1/project' })
  .use(projectService)
  .get('/', async ({}) => {
    try {
      return {
        success: true,
        data: await ProjectService.getAllProjects(),
      }
    } catch (error) {
      return {
        success: false,
        message: (error as Error).message,
      }
    }
  },{
    // This would work
    response: t.Object({
      success: t.Boolean(),
      message: t.Optional(t.String()),
      data: t.Optional(t.Any())
    }),
  })
  .get('/test' , async ({}) => {
    // This won't show up in the OpenAPI docs
    return {
      success: true,
      message: 'Project route is working!',
    }
  })
// src/index.ts

import { Elysia, t } from 'elysia'
import cors from '@elysiajs/cors'
import { fromTypes, openapi } from '@elysiajs/openapi'
import staticPlugin from '@elysiajs/static'
import { env } from '@yolk-oss/elysia-env'

import { log } from '@/log'
import { device } from '@/modules/device'
import { project } from '@/modules/project'

const app = new Elysia()
  .use(cors())
  .use(
    env({
      DATABASE_URL: t.String({
        format: 'uri',
        description: 'Database connection URL',
      }),
    }),
  )
  .use(log.into())
  .use(
    openapi({
      references: fromTypes(
        process.env.NODE_ENV === 'production'
          ? 'dist/index.d.ts'
          : 'src/index.ts',
      ),
      documentation: {
        info: {
          title: 'Elysia TypeGen Example',
          version: '1.0.0',
          description: 'All response here generated from types',
        },
      },
    }),
  )
  .use(staticPlugin())
  .use(device)
  .use(project)
  .listen(3000)

log.info(`🦊 ElysiaJS is running at ${app.server?.url}`)
log.info(`⚡️ Check OpenAPI docs at ${app.server?.url}openapi`)

The /api/v1/project route displays response type correctly, while /api/v1/project/test doesn't show response type at all.

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions