-
Hello 👋 I've a question about usage of Directly using the Returns decorator it's really simple to define exact or oneOf schema without any problems: ...
@Returns(200, Model)
@Returns(400, [ErrorModel1, ErrorModel2])
async endpointMethod() {...} Then I've added a decorator for custom authentication which uses specific middleware to handle authentication logic. Where I've used the following pattern to inject middleware's response error model (so I don't need to write that on every endpoint that uses this decorator): export function Authenticated(): Function {
return useDecorators(
UseAuth(BearerAuthMiddleware),
Security("BearerAuth"),
Returns(401, UnauthenticatedErrorModel));
} Now the initial endpoint looks like this and still works as expected (spec defines 200, 400 and 401 error models correctly)... ...
@Authenticated()
@Returns(200, Model)
@Returns(400, [ErrorModel1, ErrorModel2])
async endpointMethod() {...} ... but let's say the ...
@Authenticated()
@Returns(200, Model)
@Returns(400, [ErrorModel1, ErrorModel2])
@Returns(401, ServiceErrorModel);
async endpointMethod() {...} And at that moment we now have two models that are related to the same http status code (ServiceErrorModel and UnauthenticatedErrorModel) which aren't passed into Returns decorator as an array but separately, and that causes only one to appear in the final schema. So my question is, is there a way to solve this (how to make final schema use oneOf for ServiceErrorModel and UnauthenticatedErrorModel)? If so, I would really appreciate an example 🙏. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
Returns(400, [ErrorModel1, ErrorModel2]) Isn’t correct. try Returns(400).OneOf(ErrorModel1, ErrorModel2) |
Beta Was this translation helpful? Give feedback.
-
Here is the minimal example to reproduce what i'm trying to say (and trying to merge 😅): import { Get, Required, Returns, Security } from "@tsed/schema";
import { Controller, Middleware, MiddlewareMethods, UseAuth } from "@tsed/common";
import { useDecorators } from "@tsed/core";
class Model1 { @Required() model1: string; }
class Model2 { @Required() model2: string; }
class Model3 { @Required() model3: string; }
@Middleware()
class AuthMiddleware implements MiddlewareMethods {
public async use(): Promise<void> { }
}
function Authenticated(): Function {
return useDecorators(UseAuth(AuthMiddleware), Security("BearerAuth"), Returns(401, Model2));
}
@Controller("/auth")
export class AuthController {
@Get("/")
@Authenticated()
@Returns(200, Model1)
@Returns(401, Model3) // **this one gets "lost" as a side effect of included @Authenticated overriding it.**
get() { }
} |
Beta Was this translation helpful? Give feedback.
My bad, I given you the wrong code example.
The returns decorator apply by default the 401 Unauthorize model if there is no second parameters. So to fix that, gives Object as second parameters to avoid the default behavior: