-
Notifications
You must be signed in to change notification settings - Fork 508
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
Add an option to explicitly type SuccessResponse #387
Comments
If you wouldn’t mind, could you please give a real world example of why the api would return different types? I saw your example in the tests but it’s hard to understand why someone would want to do this. Please provide the explanation in a non-technical / user-story-centric manner. In other words, pretend I know nothing about TypeScript or Swagger, but I just want to get some kind of business goal accomplished. |
One example: |
Thank you so much for explaining your use case. Your goal of defining a union of response types should be handled through #393 (which we're taking submissions on. If you'd like to submit that help, we would love the contribution 😍). I describe that more in detail here: #369 (comment) As for why I'm closing this issue, if a user of tsoa wants to "explicitly type the SuccessResponse" they should do that with the return type of the controller function. To do it any other way would prevent TypeScript from validating that the route is actually returning the type that we're communicating in the Swagger model. |
Your original PR gave me an idea that could be closer to your original intention. So if you wanted to get swagger output that resembles this:
we could extend
So yea, I'm open to ideas on how to accomplish this. Ultimately @WoH, I want you and others to be able to accomplish your needs. But I have to make sure that it's accomplished in a way that keeps the library compatible with swagger and OpenAPI 3.0. So I'm going to leave this PR closed until #393 is finished. But we can keep chatting if you and others would like to continue to discuss the optimal solution. :) |
Thanks for your response.
I want to split both onto different http status codes.
As for extending
This is allowed, unchecked, but will be ignored, because the SuccessResponse will be added here. I know this is not preferable but might be needed. The only other approach I could see is exposing a TsoaError to be extended by the developer that has a status code property that will be documented and used. |
Good point about the success response containing the error model. That’s not a good choice on my part. So maybe the solution is to use
Modifying the type for the success code will be prevented after #119 is fixed since it’s the best way to fix the duplicate code error in Swagger. |
I'm kind of running into this problem at the moment, in that I want to return multiple types depending on the status code. But throwing an error isn't type checked and arguably exceptions aren't the right concept for statuses that might be pretty common. It feels unsatisfying to set the type of the At any time I can use Why not make the status part of the return value? Something like:
It might potentially? be possible with some magic to deconstruct the return type into an OpenAPI spec for that route. At the very least might it not make sense to include a version of the aforementioned middleware that understands a single error type, so at least the user can throw that type and have it be interpreted, without having to include a middleware (EDIT: from a github comment). I'd be happy to contribute all or any of this by the way, if I knew it would be welcome. Thanks for the work on the library, btw, was exactly what I was looking for and I love it so far. |
I'd like to head what you think about this: #617. I don't know koa very well, is there a similar option for that framework maybe? |
Now that #393 is finished can we take another look at this issue? I really like @michaelbeaumont's suggestion of making the status code part of the controller's return type. But since that would be a breaking change I would equally welcome a typed |
Hey there. I believe we can even do better than that. Right now, the best way to imitate that is to return the SuccessResponse as the return type and throw/catch for alternative responses. |
Usually, the SuccessResponse uses the return type of the annotated function to determine the type to be documented by swagger. However, there are cases when I want to return a type union
A | B
documented asSuccessResponse<A>()
andResponse<B>
from a controller.In these cases, the function body returns a type union of
A | B
to get full TS support.In order to document this behavior using TSOA, it should be possible to override the default return type using the SuccessResponse decorator (i.e.
@SuccessResponse<A>(...)
).Alternatives considered:
@Response<A>(200, ...)
since the default behavior of TSOA is to overwrite that with the default SuccessResponse here.A
from the controller function and returningB as any
feels badSyntax:
@SuccessResponse<OverrideType>(...)
This does not address returning a type union within one response documented as oneOf by default.
Related PR, opened an issue for discussion.
The text was updated successfully, but these errors were encountered: