'How to add request body to swagger docs without specifying it as a method parameter?
I have a .net core 2.0 Web API that uses Swashbuckle to generate the swagger documentation. There is one POST endpoint I need to specify the request body in the swagger documentation without specifying it as a [FromBody] method parameter of the controller. Can't specify this as a method parameter since the request body changes depending on the order type. I just want to specify the BaseOrderModel as the request model in swagger documentation. How should I accomplish this?
The following code snippet will give some understanding of my code.
// Contoller
[HttpPost]
[Route("checkout")]
[ProducesResponseType(typeof(CheckoutOrderDto), (int)HttpStatusCode.Created)]
public async Task<IActionResult> CheckoutOrderAsync([FromQuery] OrderType type)
{
BaseOrderModel? order;
switch (type)
{
case OrderType.Pickup:
// Deserialize to PickupOrder model
break;
case OrderType.Delivery:
// Deserialize to DeliveryOrder model
break;
case OrderType.UberEats:
// Deserialize to UberEats model
break;
default:
return BadRequest("Invalid order type");
}
// Omitted rest of method for brevity
}
// Enum
public enum OrderType
{
Pickup,
Delivery,
UberEats
}
// Models
public abstract class BaseOrderModel
{
// Omitted props for brevity
}
public class PickupOrder : BaseOrderModel
{
// Omitted props for brevity
}
public class DeliveryOrder : BaseOrderModel
{
// Omitted props for brevity
}
public class UberEats : BaseOrderModel
{
// Omitted props for brevity
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
