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

46qrfjad  于 2024-01-08  发布在  其他
关注(0)|答案(1)|浏览(282)

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


的数据



这是POCO类:

  1. public class AssetTransactionAllAssetsSearchRecord2
  2. {
  3. public AssetTransactionAllAssetsSearchRecord2() { }
  4. public long Id { get; set; }
  5. public DateTime FromCreated { get; set; }
  6. public DateTime ToCreated { get; set; }
  7. public long? DocumentId { get; set; }
  8. public long AssetId { get; set; }
  9. public long HostingAssetId { get; set; }
  10. public long SystemAssetId { get; set; }
  11. public long CustomerAssetId { get; set; }
  12. public string AssetCode { get; set; }
  13. public string HostingAssetCode { get; set; }
  14. public string SystemAssetCode { get; set; }
  15. public string CustomerAssetCode { get; set; }
  16. public string AssetName { get; set; }
  17. public string HostingAssetName { get; set; }
  18. public string SystemAssetName { get; set; }
  19. public string CustomerAssetName { get; set; }
  20. }

字符串
下面是Swagger配置:

  1. using System.Web.Http;
  2. using WebActivatorEx;
  3. using VI_Web;
  4. using Swashbuckle.Application;
  5. using Swashbuckle.Swagger;
  6. using System.Collections.Generic;
  7. using System.Web.Http.Description;
  8. [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
  9. namespace VI_Web
  10. {
  11. class AuthTokenOperation : IDocumentFilter
  12. {
  13. public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
  14. {
  15. swaggerDoc.paths.Add("/token", new PathItem
  16. {
  17. post = new Operation
  18. {
  19. tags = new List<string> { "UserAuth" },
  20. consumes = new List<string>
  21. {
  22. "application/x-www-form-urlencoded"
  23. },
  24. parameters = new List<Parameter> {
  25. new Parameter
  26. {
  27. type = "string",
  28. name = "grant_type",
  29. required = true,
  30. @in = "formData",
  31. @default = "password"
  32. },
  33. new Parameter
  34. {
  35. type = "string",
  36. name = "client_id",
  37. required = true,
  38. @in = "formData"
  39. },
  40. new Parameter
  41. {
  42. type = "string",
  43. name = "client_secret",
  44. required = true,
  45. @in = "formData"
  46. },
  47. new Parameter
  48. {
  49. type = "string",
  50. name = "database",
  51. required = true,
  52. @in = "formData"
  53. }
  54. }
  55. }
  56. });
  57. }
  58. }
  59. public class SwaggerConfig
  60. {
  61. public static void Register()
  62. {
  63. var thisAssembly = typeof(SwaggerConfig).Assembly;
  64. GlobalConfiguration.Configuration
  65. .EnableSwagger(c =>
  66. {
  67. c.SingleApiVersion("v1", "VI_Web");
  68. c.ApiKey("Token")
  69. .Description("Filling bearer token here")
  70. .Name("Authorization")
  71. .In("header");
  72. c.DocumentFilter<AuthTokenOperation>();
  73. })
  74. .EnableSwaggerUi(c =>
  75. {
  76. c.EnableApiKeySupport("Authorization", "header");
  77. });
  78. }
  79. }
  80. }

lmvvr0a8

lmvvr0a81#

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

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

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

  1. ... GetWithAssets([FromUri] AssetTransactionAllAssetsSearchRecord2 record)


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

相关问题