Determine and implement a strategy for "merging" response metadata when redirecting to a route for swagger #526
Labels
epic
Feature that requires significant effort to implement
project:API
Items related to the API project
type:documentation
Tasks:
Swashbuckle.AspNetCore.SwaggerGen.IOperationFilter
in namespaceAPI.SwaggerGen
to populate potential responses for redirected actionsSynopsis
Through testing I have noticed a quirk of swagger that causes endpoints that redirect to not populate the forwarded response codes / response types.
For example,
GET
/me
will redirect toGET
/users/{id}
by forwarding the authorized user's idObjectively, this documentation is completely correct since redirects are separate requests, meaning a request to this endpoint (without following the redirect) will return a response code of 302 with no body.
However, this setup causes swagger to not generate response type information because it does not "look forward" into what the redirected request will return so to speak:
This is a problem because any code generator will not be able to infer the response type of this operation. The above definition creates the following client code with
nswag
:Solution
At this time, since we only use redirects in a limited capacity I have simply just copy / pasted the
ProducesResponseTypeAttribute<>
and<response code="">
of the end destination onto the original request. This achieves the desired result for now but it is not refactor proof and will surely cause issues in the future with any schema changes or changes to the end destination's response codes.For example (copying the response attributes from
GET
/users/{id}
toGET
/me
):Produces the desired definition with populated response references:
A desired permanent fix would alter the swagger generator to look ahead into the redirected action and replicate its potential response metadata onto the original action. This can likely be achieved with a custom
Swashbuckle.AspNetCore.SwaggerGen.IOperationFilter
. The operation filter exposes the entire swagger document as well as theMethodInfo
for the current operation which can likely be reflected on to determine if a redirect will occur.The text was updated successfully, but these errors were encountered: