elysia icon indicating copy to clipboard operation
elysia copied to clipboard

Merge group schema with route schema

Open StrangeBytesDev opened this issue 1 year ago • 4 comments

What is the problem this feature would solve?

Currently, values set in a group guard over overwritten if also defined in a route. For example:

new Elysia()
    .group(
        '/something'
        {
            response: {
                default: t.Object({message: t.String()})
            }
        },
        (app) => app
            .get('/hello', () => 'hello', {response: {200: t.String()})
    )

The response object for the "/hello" route will only contain the entry for the 200 response. If instead the response object was merged together, it'd be useful for adding default responses to a group. This would be especially useful for the "default" response which is really handy for handling errors.

What is the feature you are proposing to solve the problem?

Merge together the group guard response with the route defined response.

What alternatives have you considered?

No response

StrangeBytesDev avatar Jun 27 '24 23:06 StrangeBytesDev

It should have been fixed by https://github.com/elysiajs/elysia/pull/502 (more specifically by SaltyAom's commit https://github.com/elysiajs/elysia/commit/da0fa83eacf00527e183f1dc410a699298657b9d) but it seems like it's broken.

Vahelnir avatar Jul 16 '24 20:07 Vahelnir

Unable to replicate on 1.1.7 with the following code:

import { Elysia, t } from 'elysia'

new Elysia()
	.group(
		'/something',
		{
			response: {
				400: t.Object({ message: t.String() })
			}
		},
		(app) =>
			app
				.get('/200', ({ error }) => 'a', {
					response: { 200: t.String() }
				})
				.get('/400', ({ error }) => error(400, { message: 'a' }), {
					response: { 200: t.String() }
				})
	)
	.listen(3000)

Both endpoint validation work correctly on my end.

Could you provide more specific reproduction step on how to replicate the error?

SaltyAom avatar Aug 27 '24 10:08 SaltyAom

In the example above, the 400 response will be included for both routes. But if you use either "default" or "error", that won't work.

import { Elysia, t } from 'elysia'

new Elysia()
	.group(
		'/something',
		{
			response: {
				error: t.Object({ message: t.String() })
			}
		},
		(app) =>
			app
				.get('/200', ({ error }) => 'a', {
					response: { 200: t.String() }
				})
				.get('/400', ({ error }) => error(400, { message: 'a' }), {
					response: { 200: t.String() }
				})
	)
	.listen(3000)

StrangeBytesDev avatar Oct 07 '24 02:10 StrangeBytesDev

In Elyisa.js 1.2.25 this bug is still present. If you set default or error in group/guard, then it overwrites whatever is defined in the schema.

@SaltyAom

ajaykarthikr avatar Apr 01 '25 09:04 ajaykarthikr

We never have support for response.default, and response.error so it seems to be a problem with a type issue (will look into it on my end)

But other than that, this seems to be fixed by Standalone validator

So, I'm closing as it has the original issue is about response.default If you have a problem similar to this, please open a new one

SaltyAom avatar Aug 01 '25 19:08 SaltyAom