Swagger - same route, both GET method, swagger 2.0 not supported - c#

Hiiiiiii.
I have 2 methods with the form show below:
Swagger showed me 500 error. Not supported.
[System.Web.Http.AllowAnonymous]
[System.Web.Http.HttpGet]
[System.Web.Http.Route("api/some/saveTemplate/1.1/action")]
public async Task<IHttpActionResult> SaveSomething(string param1, string param2)
{
//some.code();
return Ok(json);
}
[System.Web.Http.AllowAnonymous]
[System.Web.Http.HttpGet]
[System.Web.Http.Route("api/some/saveTemplate/1.1/action")]
public async Task<IHttpActionResult> SaveSomething(string param1, string param2, int param3)
{
//some.code();
return Ok(json);
}
As you can see above, i just have an overload on the method.
How can i get the solution? I cannot change any from my endpoints/routes. I am supporting a system in production, but I need to generate documentation.
can i generate any type of exclusion to swagger get the first method as a "GET 1" an the second as a "GET 2" or similar?
thank you very much

Related

Generate response error in Swagger - .netCore Api

For handling all errors I use ErrorHandlingMiddleware. Anywhere where I want I throw exception (my implementation exception) where I define type (enum) and message of errors.
My question is how can I generate this type of error in swagger, becaouse swagger know only about controller return type values. And swagger generate only response (200) bcs in methods is only return Ok();
[HttpPost("login")]
public async Task<IActionResult> Test(LoginRequest req)
{
if (user.Banned)
{
throw new BadRequestHorseedoException(ErrorCode.BANNED, "User is banned");
}
return Ok();
}
You can use ProducesResponseType attribute on methods. Your code can look like:
[HttpPost("login")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> Test(LoginRequest req)

.Net Api - Parent route matched for invalid nested route

I have a .Net 5 API and some nested routes as follows:
[Route("api/v{version:apiVersion}/orders")]
[ApiVersion("1.0")]
[ApiController]
public class OrdersController: ControllerBase
{
[HttpGet("{userId:required}")]
public async Task<ActionResult> Get(string userId,
CancellationToken cancellationToken = default)
{
// return the orders corresponding to the userId
}
}
[Route("api/v{version:apiVersion}/orders/details")]
[ApiVersion("1.0")]
[ApiController]
public class OrdersDetailsController: ControllerBase
{
[HttpGet("{orderId:required}")]
public async Task<ActionResult> Get(string orderId,
CancellationToken cancellationToken = default)
{
// return the order details
}
}
Below is the list of responses I get when making requests to the API:
GET /orders/some_dummy_user_id returns the orders for userId="some_dummy_user_id", which is OK
GET /orders/details/some_dummy_order_id returns the details of orderId="some_dummy_order_id", which is OK
GET /orders/details/ tries to return the orders corresponding to userId="details" which is Not OK
The question is: is it possible to make the GET /orders/details/ request match the OrderDetailsController route and therefore return a 404 because of the missing orderId URL parameter?
try this
[Route("api/v{version:apiVersion}/orders")]
[ApiVersion("1.0")]
[ApiController]
public class OrdersController: ControllerBase
{
[HttpGet("{userId:required}")]
public async Task<ActionResult> Get(string userId,
CancellationToken cancellationToken = default)
{
if(userId.ToLower=="details") throw new HttpException(404, "not found");
// return the orders corresponding to the userId
}
}
It seems that unfortunately it's not possible to achieve the required scenario using only the .Net routing features. The possible solutions I see in this situation are:
As mentioned in Serge's answer, introduce a manual check and throw a 404 Exception if the userId matches the sub-route
Update the routes schema in order to prevent this scenario
As I'd like to achieve this using only the .Net routing features I've proceeded with the 2nd solution (especially as the impact on the overall routes schema wasn't major).

Routing controller with optional Id not in parameters

I'm currently struggling in a problem where I need to register a route like:
https://localhost:44300/oneController/{Guid}/method1?param1=10000&param2=stuff
What I've tried was something similar to this:
[HttpGet]
public async Task<ActionResult> method1(Guid id, int param1, string param2)
{
...
}
However, this does not seems to work, any ideas to resolve this issue?
You can use RouteAttribute, like
[Route("{id:guid}/method1"), HttpGet]
public async Task<ActionResult> method1(Guid id, int param1, string param2)
{
...
}
Good to read MSDN Documentation: Attribute Routing in ASP.NET

Handle WebApi Core methods with Id as string and int

I have two methods:
[HttpGet("{id}")]
public IActionResult GetTask([FromRoute] int id)
{
}
[HttpGet("{userId}")]
public IActionResult GetUserTask([FromRoute] string userId)
{
}
As you can see, i want to pass to my API routes like:
https://localhost:44365/Task/1
and
https://localhost:44365/Task/string
But my WebApi project cant handle it. When i pass route like this:
https://localhost:44365/Task/7dd2514618c4-4575b3b6f2e9731edd61
i get an 400 http and this response:
{
"id": [
"The value '7dd2514618c4-4575b3b6f2e9731edd61' is not valid."
]
}
While debugging, im not hitting any methods (when i pass string instead of int)
My question is, how to verload methods with one parameters with string or int? These methods do diffrent things
EDIT
When i pass something like:
https://localhost:44365/Task/dddd
I still get response with invalid id
You can define parameter type like [HttpGet("{id:int}")]. For more information refer below link.
https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#route-constraints
Your actions should be like below.
[HttpGet("{id:int}")]
public IActionResult GetTask([FromRoute] int id)
{
}
[HttpGet("{userId}")]
public IActionResult GetUserTask([FromRoute] string userId)
{
}
Use like this
[HttpGet("{id}")]
public IActionResult GetTask([FromRoute] int id)
{
}
[HttpGet("User/{userId}")]
public IActionResult GetUserTask([FromRoute] string userId)
{
}
and while calling api with guid/string use
https://localhost:44365/Task/User/7dd2514618c4-4575b3b6f2e9731edd61

web api 2 versioning using attribute routing

I'm having a problem using Attribute Routing where the compiler is showing the error "Type 'ValuesController' already defines a member called 'Get' with the same parameter types".
I have checked that config.MapHttpAttributeRoutes() is in the webapiConfig file
// GET api/values/5
[Route("api/values/{id}")]
public string Get(int id)
{
return "value";
}
[Route("api/v2/values/{id}")]
public string Get(int id)
{
return "value";
}
From reading http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 something like this should be possible.
Your code is invalid C#, even aside from Web API. You can't define the same function signature twice. If you rename the second Get to GetNew, or move it to a different controller, it should work.
The problem, as I see it, is that you have two methods with the same name and arguments, which isn't allowed in C# - if you really must have the same method names, you need to overload on the number of arguments, and/or their types:
public string Get(int id, int id2)
Or
public string Get(Guid id)

Categories