繁体   English   中英

如何在Spring REST Docs中记录树

[英]how to document trees in Spring REST Docs

我有一棵返回以下结构的树:

[{
 "data":
  {
      "id": 15,
      "permissionId": "perm1",
      "name": "Events"
  },
  "children": [
  {
      "data":
      {
          "id": 16,
          "permissionId": "perm2",
          "name": "Report",
          "parentRightDictionaryItemId": 15
      },
      "children": [
      {
          "data":
          {
              "id": 17,
              "permissionId": "perm3",
              "name": "Construct",
              "parentRightDictionaryItemId": 16
          }
      }],

  }]

}]

而且我不知道如何记录这棵树的字段,因为它可能很深。 我正在尝试做的事情,此函数返回记录的字段的结构:

protected List<FieldDescriptor> getResponseFieldDescriptor(String prefix) {
    List<FieldDescriptor> fields = new ArrayList<>();

    fields.add(fieldWithPath(prefix + "data").description("data").type(OBJECT));
    fields.add(fieldWithPath(prefix + "data.id").description("id").type(NUMBER));
    fields.add(fieldWithPath(prefix + "data.permissionId").description("permissionId").type(STRING));
    fields.add(fieldWithPath(prefix + "data.name").description("name").type(STRING));
    fields.add(fieldWithPath(prefix + "children").description("children").type(ARRAY).optional());  // I want this to be enough, but that's not enough

    return fields;
}

如果孩子人数为空,我的功能将正常运行。 但是在有孩子的情况下,返回一个错误,我没有记录整个树结构。 好多 怎么做?

如果您只想记录响应的一部分并且不希望未记录部分的测试失败,那么我会看到至少两种方法。 响应的一部分可以用subsectionWithPath记录下来:

如果您不想提供所有字段的详细文档,则可以记录有效负载的整个子部分。

一种替代方法是使用relaxedResponseFields

字段也可以宽松模式记录,其中任何未记录的字段都不会导致测试失败。 为此,请在org.springframework.restdocs.payload.PayloadDocumentation上使用relaxedRequestFields和relaxedResponseFields方法。

有关更多详细信息,请参见https://docs.spring.io/spring-restdocs/docs/2.0.3.RELEASE/reference/html5/#documenting-your-api-request-response-payloads-fields

暂无
暂无

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

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