简体   繁体   English

webapi NetCore json 仅返回每个嵌套列表的最后一个元素

[英]webapi NetCore json returns only last element of each nested list

The problem: I am trying to match a given signature of an API to replace the underlaying implementation.问题:我正在尝试匹配 API 的给定签名来替换底层实现。

I created the proyect as dotnet new webapi and duplicated the controller.我将项目创建为 dotnet new webapi 并复制了 controller。

My new controller works when posting a simple object (first method), but second fails to populate a nested list: It adds only the last element of a nested item.我的新 controller 在发布简单的 object (第一种方法)时有效,但第二种方法无法填充嵌套列表:它仅添加嵌套项的最后一个元素

Using swagger, I post this json:使用 swagger,我发布了这个 json:

{
  "fileContent": "the file content",
  "metadata": [
    {
      "name": "alfa",      "value": ["aa"],
      "name": "beta",      "value": ["bb"],
      "name": "gama",      "value": ["gg"]
    },
{
      "name": "delta",      "value": ["dd"],
      "name": "epsilon",      "value": ["ee"],
      "name": "omega",      "value": ["oo"]
    }
  ]
}

I return the same object, but I just get:我返回相同的 object,但我得到:

{
  "fileContent": "the file content",
  "metadata": [
    {
      "name": "gama",
      "value": [
        "gg"
      ]
    },
    {
      "name": "omega",
      "value": [
        "oo"
      ]
    }
  ]
}

The full webapi controller:完整的 webapi controller:

[ApiController]
[Route("[controller]")]
public class CrappyController : ControllerBase
{  
    private readonly ILogger<CrappyController> _logger;

    public CrappyController(ILogger<CrappyController> logger)
    {
        _logger = logger;
    }

    [HttpPost(Name = "v1/CreateAttachment")]
    public CreateAttachmentV1 Create(CreateAttachmentV1 createAttachment)
    {
        _logger.LogInformation("CreateAttachment.V1");
            
        return createAttachment;
        // .ToArray();
    }


    [HttpPatch(Name = "v1/CreateAttachment")]
    public UpdateAttachmentV1 UpdateAttachment(UpdateAttachmentV1 data)
    {
        _logger.LogInformation("UpdateAttachment.V1");
            
        return data; // please notice I return the same object for debugging
    }
}

And the method classes和方法类

    public struct NameValueList<TKey, TArray>
    {
        public TKey Name { get; set; }
        public string[]? Value { get; set; }
    }

    public class UpdateAttachmentV1
    {
        public string? FileContent { get; set; }
        
        public List<NameValueList<string, string>>? Metadata { get; set; }
    }

    public class CreateAttachmentV1
    {
        public string FileCategory { get; set; }

        public string FileName { get; set; }
    }

I am puzzled.我很困惑。 It compiles, signature matches on swagger,but jumps over all but last element on each Metadata array.它编译 swagger 上的签名匹配,但跳过每个元数据数组上除最后一个元素之外的所有元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM