“List is empty for httppost in Swagger” 的中文翻译是:Swagger 中的 httppost 列表为空。

huangapple go评论138阅读模式
英文:

List is empty for httppost in Swagger

问题

目前我正在使用Swagger测试一个HttpPost方法。我的端点上除了列表字段之外,所有其他字段都已填充。

Document.cs

  1. public class DocumentAddRequest
  2. {
  3. public string Name { get; set; }
  4. public IFormFile File { get; set; }
  5. public List<Detail> Details { get; set; }
  6. }

Detail.cs

  1. public class Detail
  2. {
  3. public string PropertyName { get; set; }
  4. public string PropertyValue { get; set; }
  5. }

我正在向Details中添加两个JSON对象,但在控制器端点内检查时,它是空的。

我可以问一下,如何使用HttpPost传递列表。需要注意的一件事是,由于我正在上传文件,我使用了FromForm。

我已经搜索过,似乎Swagger UI 存在一些问题https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1107 想知道是否有其他人已经解决了这个问题。

英文:

currently I'm testing a httppost method with swagger. All the other fields are populated for my endpoint except the list.

Document.cs

  1. public class DocumentAddRequest
  2. {
  3. public string Name { get; set; }
  4. public IFormFile File { get; set; }
  5. public List&lt;Detail&gt; Details { get; set; }
  6. }

Detail.cs

  1. public class Detail
  2. {
  3. public string PropertyName { get; set; }
  4. public string PropertyValue { get; set; }
  5. }

“List is empty for httppost in Swagger” 的中文翻译是:Swagger 中的 httppost 列表为空。

I am adding two json objects into Details but when inspecting within my controller endpoint, it's empty.

May I ask how do I get a list to be passed using httppost. One thing to note is I am using FromForm since I'm uploading a file.

I've googled and it looks like swagger UI has a bit of an issue https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1107 Wondering if anyone else has been able to go around this.

答案1

得分: 1

以下是一个工作演示,您可以参考,希望能帮助您。

  1. 自定义模型绑定器如下:
  1. public class MetadataValueModelBinder : IModelBinder
  2. {
  3. public Task BindModelAsync(ModelBindingContext bindingContext)
  4. {
  5. if (bindingContext == null)
  6. throw new ArgumentNullException(nameof(bindingContext));
  7. var values = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
  8. if (values.Length == 0)
  9. return Task.CompletedTask;
  10. var options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
  11. var deserialized = JsonSerializer.Deserialize(values.FirstValue, bindingContext.ModelType, options);
  12. bindingContext.Result = ModelBindingResult.Success(deserialized);
  13. return Task.CompletedTask;
  14. }
  15. }
  1. 将模型绑定器添加到模型类:
  1. [ModelBinder(BinderType = typeof(MetadataValueModelBinder))]
  2. public class Detail
  3. {
  4. public string PropertyName { get; set; }
  5. public string PropertyValue { get; set; }
  6. }
  1. 结果:

“List is empty for httppost in Swagger” 的中文翻译是:Swagger 中的 httppost 列表为空。

英文:

Below is a work demo, you can refer it, hope it can help.

1.Custom model binder like below:

  1. public class MetadataValueModelBinder : IModelBinder
  2. {
  3. public Task BindModelAsync(ModelBindingContext bindingContext)
  4. {
  5. if (bindingContext == null)
  6. throw new ArgumentNullException(nameof(bindingContext));
  7. var values = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
  8. if (values.Length == 0)
  9. return Task.CompletedTask;
  10. var options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
  11. var deserialized = JsonSerializer.Deserialize(values.FirstValue, bindingContext.ModelType, options);
  12. bindingContext.Result = ModelBindingResult.Success(deserialized);
  13. return Task.CompletedTask;
  14. }
  15. }

2.Add the model binder to the model class:

  1. [ModelBinder(BinderType = typeof(MetadataValueModelBinder))]
  2. public class Detail
  3. {
  4. public string PropertyName { get; set; }
  5. public string PropertyValue { get; set; }
  6. }

3.Result:

“List is empty for httppost in Swagger” 的中文翻译是:Swagger 中的 httppost 列表为空。

答案2

得分: 0

以下是已翻译的内容:

你可以考虑这个建议,如果你有大量的详细数据,将多个对象输入到Swagger中可能需要一些时间。

我们可以将Detail作为JSON字符串处理,然后反序列化为"Detail"对象的列表。

例如:

[HttpPost(Name = "PostFormData1")]
public IEnumerable PostFormData([FromForm] DocumentAddRequest request)
{
var serializerOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };

  1. var listObject = JsonSerializer.Deserialize<List<Detail>>(request.Details, serializerOptions);
  2. return listObject;

}

public class DocumentAddRequest
{
public string Name { get; set; }
public IFormFile File { get; set; }
public string Details { get; set; }
}

public class Detail
{
public string PropertyName { get; set; }
public string PropertyValue { get; set; }
}

Swagger输入使用以下JSON字符串处理详细数据:

[{ "PropertyName": "test", "PropertyValue": "test2" }, { "PropertyName": "test3", "PropertyValue": "test4" }]

Swagger响应:(图片链接未翻译)

Debug:(图片链接未翻译)

英文:

You might consider this as a suggestion in case you have a ton of Detail data, inputting multiple objects into Swagger might take time.

We can take the Detail as a JSON string and then deserialize it into a list of "Detail" objects.

E.g:

  1. [HttpPost(Name = &quot;PostFormData1&quot;)]
  2. public IEnumerable&lt;Detail&gt; PostFormData([FromForm] DocumentAddRequest request)
  3. {
  4. var serializerOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
  5. var listObject = JsonSerializer.Deserialize&lt;List&lt;Detail&gt;&gt;(request.Details, serializerOptions);
  6. return listObject;
  7. }
  8. public class DocumentAddRequest
  9. {
  10. public string Name { get; set; }
  11. public IFormFile File { get; set; }
  12. public string Details { get; set; }
  13. }
  14. public class Detail
  15. {
  16. public string PropertyName { get; set; }
  17. public string PropertyValue { get; set; }
  18. }

Swagger Input using following JSON String for Detail data:

  1. [{ &quot;PropertyName&quot;: &quot;test&quot;, &quot;PropertyValue&quot;: &quot;test2&quot; }, { &quot;PropertyName&quot;: &quot;test3&quot;, &quot;PropertyValue&quot;: &quot;test4&quot; }]

“List is empty for httppost in Swagger” 的中文翻译是:Swagger 中的 httppost 列表为空。

Swagger Response:
“List is empty for httppost in Swagger” 的中文翻译是:Swagger 中的 httppost 列表为空。

Debug:
“List is empty for httppost in Swagger” 的中文翻译是:Swagger 中的 httppost 列表为空。

huangapple
  • 本文由 发表于 2023年5月25日 04:11:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定