为什么Swashbuckle/Swagger在参数中包含变量名?

46qrfjad  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(58)

对于一个老的net framework项目,我使用Swashbuckle 5.6.0。当我使用一个类作为get参数时,会发生一件奇怪的事情。它将变量名添加到参数中。
这意味着API的使用者必须在每个参数的前面添加“record.”。非常不方便。
对这种行为的解释是什么?


的数据



这是POCO类:

public class AssetTransactionAllAssetsSearchRecord2
    {
        public AssetTransactionAllAssetsSearchRecord2() { }
        
        public long Id { get; set; }
        public DateTime FromCreated { get; set; }
        public DateTime ToCreated { get; set; }
        public long? DocumentId { get; set; }

        public long AssetId { get; set; }
        public long HostingAssetId { get; set; }
        public long SystemAssetId { get; set; }
        public long CustomerAssetId { get; set; }

        public string AssetCode { get; set; }
        public string HostingAssetCode { get; set; }
        public string SystemAssetCode { get; set; }
        public string CustomerAssetCode { get; set; }

        public string AssetName { get; set; }
        public string HostingAssetName { get; set; }
        public string SystemAssetName { get; set; }
        public string CustomerAssetName { get; set; }

    }

字符串
下面是Swagger配置:

using System.Web.Http;
using WebActivatorEx;
using VI_Web;
using Swashbuckle.Application;
using Swashbuckle.Swagger;
using System.Collections.Generic;
using System.Web.Http.Description;

[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]

namespace VI_Web
{
    class AuthTokenOperation : IDocumentFilter
    {
        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
        {
            swaggerDoc.paths.Add("/token", new PathItem
            {
                post = new Operation
                {
                    tags = new List<string> { "UserAuth" },
                    consumes = new List<string>
                {
                    "application/x-www-form-urlencoded"
                },
                    parameters = new List<Parameter> {
                    new Parameter
                    {
                        type = "string",
                        name = "grant_type",
                        required = true,
                        @in = "formData",
                        @default = "password"
                    },
                    new Parameter
                    {
                        type = "string",
                        name = "client_id",
                        required = true,
                        @in = "formData"
                    },
                    new Parameter
                    {
                        type = "string",
                        name = "client_secret",
                        required = true,
                        @in = "formData"
                    },
                    new Parameter
                    {
                        type = "string",
                        name = "database",
                        required = true,
                        @in = "formData"
                    }

                }
                }
            });
        }
    }

    public class SwaggerConfig
    {
        public static void Register()
        {
            var thisAssembly = typeof(SwaggerConfig).Assembly;

            GlobalConfiguration.Configuration
                .EnableSwagger(c =>
                    {
                        c.SingleApiVersion("v1", "VI_Web");
                        c.ApiKey("Token")
                        .Description("Filling bearer token here")
                        .Name("Authorization")
                        .In("header");
                        c.DocumentFilter<AuthTokenOperation>();
                    })
                .EnableSwaggerUi(c =>
                    {
                        c.EnableApiKeySupport("Authorization", "header");
                    });
        }
    }
}

lmvvr0a8

lmvvr0a81#

我认为这不是Swashbuckle问题(假设查询端点实际上有效),而是ASP.NET设置如何处理GET请求处理程序,并将“复杂”类型作为参数(record)传递。您可以尝试将类型扁平化为单独的端点参数:

... GetWithAssets(long id, DateTime fromCreated, ...)

字符串
或者使用ASP.NET Web API文档中的参数绑定中提到的FromUriAttribute,根据示例,它应该扁平化没有参数名称前缀的类型:

... GetWithAssets([FromUri] AssetTransactionAllAssetsSearchRecord2 record)


你也可以将你的端点转换为POST并接受JSON主体,这是一种非常流行的搜索类端点方法。

相关问题