如何在 Swagger 中禁用排序字典

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

我正在使用 niget

Swashbuckle.AspNetCore
版本 6.7。

我有代码:

public class DataResponse
{
    public Dictionary<string, decimal> Info { get; set; }
}

public class DataResponseExample : IExamplesProvider<DataResponse>
{
    public DataResponse GetExamples()
        => new DataResponse
        {
            Info = new Dictionary<string, decimal>
            {
                { "100", 842.123123m },
                { "40", 842.123123m },
                { "10", 842.123123m },
                { "1", 842.123123m },
                { "0.1", 842.123123m },
                { "0.01", 842.123123m },
                { "0.05", 842.123123m },
                { "0.001", 842.123123m },
            }
        };
}

等待在 swagger 页面上字典排序将按照相同的顺序,但事实上我看到:

{
  "info": {
    "1": 842.123123,
    "10": 842.123123,
    "40": 842.123123,
    "100": 842.123123,
    "0.1": 842.123123,
    "0.01": 842.123123,
    "0.05": 842.123123,
    "0.001": 842.123123
  }
}

Swagger 自动按键对字典进行排序,如何禁用排序?

c# .net swagger asp.net-core-webapi
1个回答
0
投票

Dictionary<TKey, TValue>
的行为是由哈希表实现驱动的,哈希表实现本质上是
unordered
。哈希表根据键的哈希值将元素分布到不同的桶中,并且哈希值和桶位置不考虑插入顺序。这就是字典中元素的顺序不可预测的根本原因。所以这个问题与Swagger无关。

所以我们可以使用

List<KeyValuePair<string, decimal>>
来保证数据的顺序作为这个问题的替代解决方案。

DataController.cs

using Microsoft.AspNetCore.Mvc;
using WebApplication11.Models;

namespace WebApplication11.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class DataController : ControllerBase
    {
        [HttpGet]
        public ActionResult<DataResponse> GetData()
        {
            var data = new DataResponse
            {
                Info = new List<KeyValuePair<string, decimal>>
                {
                    new KeyValuePair<string, decimal>("100", 842.123123m),
                    new KeyValuePair<string, decimal>("40", 842.123123m),
                    new KeyValuePair<string, decimal>("10", 842.123123m),
                    new KeyValuePair<string, decimal>("1", 842.123123m),
                    new KeyValuePair<string, decimal>("0.1", 842.123123m),
                    new KeyValuePair<string, decimal>("0.01", 842.123123m),
                    new KeyValuePair<string, decimal>("0.05", 842.123123m),
                    new KeyValuePair<string, decimal>("0.001", 842.123123m)
                }
            };
            return Ok(data);
        }
    }
}

数据响应.cs

using System.Text.Json.Serialization;

namespace WebApplication11.Models
{
    public class DataResponse
    {
        public List<KeyValuePair<string, decimal>>? Info { get; set; }
    }
}

数据响应示例.cs

using Swashbuckle.AspNetCore.Filters;

namespace WebApplication11.Models
{
    public class DataResponseExample : IExamplesProvider<DataResponse>
    {
        public DataResponse GetExamples()
        {
            return new DataResponse
            {
                Info = new List<KeyValuePair<string, decimal>>
            {
                new KeyValuePair<string, decimal>("100", 842.123123m),
                new KeyValuePair<string, decimal>("40", 842.123123m),
                new KeyValuePair<string, decimal>("10", 842.123123m),
                new KeyValuePair<string, decimal>("1", 842.123123m),
                new KeyValuePair<string, decimal>("0.1", 842.123123m),
                new KeyValuePair<string, decimal>("0.01", 842.123123m),
                new KeyValuePair<string, decimal>("0.05", 842.123123m),
                new KeyValuePair<string, decimal>("0.001", 842.123123m)
            }
            };
        }
    }
}

测试结果

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.