elysia icon indicating copy to clipboard operation
elysia copied to clipboard

Setting reference model as default response throws an internal server error

Open ajaykarthikr opened this issue 7 months ago • 1 comments

What version of Elysia is running?

1.2.25

What platform is your computer?

Darwin 24.4.0 arm64 arm

What steps can reproduce the bug?

When you create a reference model and set it as default response in a route, elysia throws undefined is not an object (evaluating 'maybeNameOrSchema.type').

import Elysia, { t } from 'elysia';

const MessageSchema = t.Object({
  message: t.String(),
});

export const appService = new Elysia({
  name: 'appService',
})
  .model({
    message: MessageSchema,
  }, {
    $id: 'message'
  })
  .get(
    '/health',
    () => {
      return {
        timestamp: new Date().toISOString(),
        status: 'ok',
      };
    },
    {
      response: {
        200: t.Object({
          status: t.String(),
          timestamp: t.String(),
        }),
        default: t.Ref('message')
      }
    },
  )
  .listen(3000);

What is the expected behavior?

It should return 200 as response.

What do you see instead?

It returns undefined is not an object (evaluating 'maybeNameOrSchema.type') as response. The error doesn't even reach onError hook.

Additional information

Even setting value of default as message without t.Ref returns the same error.

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

yes

ajaykarthikr avatar Apr 15 '25 05:04 ajaykarthikr

On further checking it looks like it happens even when setting MessageSchema or any typebox schema directly as default response.

ajaykarthikr avatar Apr 15 '25 06:04 ajaykarthikr

I assume that this was fixed in 1.3 as I'm unable to reproduce on latest (1.3.1) with the following code

import { Elysia, t } from 'elysia'

const MessageSchema = t.Object({
	message: t.String()
})

const app = new Elysia({
	name: 'appService'
})
	.model({
		message: MessageSchema
	})
	.get(
		'/health',
		() => {
			return {
				timestamp: new Date().toISOString(),
				status: 'ok'
			}
		},
		{
			response: {
				200: t.Object({
					status: t.String(),
					timestamp: t.String()
				}),
				default: t.Ref('message')
			}
		}
	)

app.handle(new Request('http://localhost/health'))
	.then((x) => x.json())
	.then(console.log)

SaltyAom avatar May 27 '25 11:05 SaltyAom

Yes, this is fixed. Thanks @SaltyAom.

ajaykarthikr avatar Jun 05 '25 10:06 ajaykarthikr