如何在 Spring REST Docs 中记录树

how to document trees in Spring REST Docs

我有一个 returns 结构如下的树:

[{
 "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
          }
      }],

  }]

}]

而且我不知道如何记录这棵树的字段,因为它可能很深。 我想做的是,这个函数 returns 记录字段的结构:

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;
}

如果 children 的数字为空,我的函数将正常工作。 但是在children的情况下,返回一个错误,我没有记录整个树结构。这是很多。怎么办?

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

记录

If you don’t want to provide detailed documentation for all of the fields, an entire subsection of a payload can be documented.

另一种方法是使用 relaxedResponseFields:

Fields can also be documented in a relaxed mode where any undocumented fields will not cause a test failure. To do so, use the relaxedRequestFields and relaxedResponseFields methods on org.springframework.restdocs.payload.PayloadDocumentation.

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