如何在C#中为一个请求对象定义多个请求示例?

问题描述 投票:0回答:1

对于请求,在Swagger 2.0规范中,所有API端点上定义的每个请求对象只有一个架构。因此,如果我要在多个API端点中给出相同的请求对象,即在这样的多个控制器操作上:

DeliveryOptions.cs
[SwaggerRequestExample(typeof(DeliveryOptionsSearchModel), typeof(DeliveryOptionsSearchModelExample1))]
public async Task<IHttpActionResult> DeliveryOptionsForAddress(DeliveryOptionsSearchModel search)
...    

// maybe in some other controller, e.g. Search.cs
[SwaggerRequestExample(typeof(DeliveryOptionsSearchModel), typeof(DeliveryOptionsSearchModelExample2))]        
public async Task<IHttpActionResult> Search(DeliveryOptionsSearchModel search)


// for Example 
public class DeliveryOptionsSearchModel
{
  public string name {get; set;}
}

public class DeliveryOptionsSearchModelExample1 : Swashbuckle.Examples.IExamplesProvider 
{
    public object GetExamples()
    {
        return new DeliveryOptionsSearchModel
        {
            name= "abc"
        };
    }
}

public class DeliveryOptionsSearchModelExample2 : Swashbuckle.Examples.IExamplesProvider 
{
    public object GetExamples()
    {
        return new DeliveryOptionsSearchModel
        {
            name= "xyz"
        };
    }
}

DeliveryOptionsSearchModel对象在整个Swagger文档中仅定义一次。

如何在C#asp .net中迅速地为一个请求对象(DeliveryOptionsSearchModel)定义多个请求示例?

问题是它没有用草率的方式呈现DeliveryOptionsSearchModel对象的两个不同示例。 Swagger UI仅显示所有API端点的一个示例类(例如-DeliveryOptionsSearchModelExample2)。

还有其他解决方法吗?

I am using the following packages

c# swagger-ui swagger-2.0 swashbuckle
1个回答
0
投票

<a href="https://c2n.me/46JxgoT"><img src="https://c2n.me/46JxgoT.png" alt="Swagger UI - Google Chrome"/></a>

我有同样的问题,我以这种方式进行了排序。我尝试通过CreateSomethinkExample : IExampleProvider<CreateSomethink>但似乎Swagger核心doe不支持多个请求示例。

我以这种方式解决了这个问题,请看///Doc

 /// <summary>
    /// Resident creation endpoint. Creating new Resident in DB and returns created item
    /// </summary>
    /// <response code="201">Success-full creation returns created item</response>
    /// <response code="400">Failed creation returns status and list of errors</response>
    /// <response code="404">House or Flat not found</response>
    /// <response code="500">Server error</response>
    /// /// <remarks>
    /// Sample request:
    ///
    ///     POST /Todo
    ///     {
    ///         "flatId": "62a05ac8-f131-44c1-8e48-f23744289e55",
    ///         "name": "Name",
    ///         "surname": "Surname",
    ///         "personalCode": "12345",
    ///         "dateOfBirth": "2020-03-30T00:00:00",
    ///         "phoneNumber": "+37122345678",
    ///         "email": "[email protected]"
    ///     }
    ///
    ///     POST /Todo
    ///     {
    ///         "name": "Name",
    ///         "surname": "Surname",
    ///         "personalCode": "12345",
    ///         "dateOfBirth": "2020-03-30T00:00:00",
    ///         "phoneNumber": "+37122345678",
    ///         "email": "[email protected]"
    ///     }
    ///
    /// </remarks>
    [AllowAnonymous]
    [ProducesResponseType(typeof(SuccessResidentCreationResponse), 201)]
    [ProducesResponseType(typeof(FailedResidentCreationResponse), 400)]
    [HttpPost(ApiRoutes.ResidentRoute.ResidentV1)]
    public async Task<IActionResult> CreateFlat([FromServices] Iconfiguration configuration, [FromBody] CreateResidentRequest request)
    {
        //Some logic
    }
© www.soinside.com 2019 - 2024. All rights reserved.