简体   繁体   English

如何在Spring REST Docs中记录树

[英]how to document trees in Spring REST Docs

I have a tree that returns the following structure: 我有一棵返回以下结构的树:

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

  }]

}]

And I don't understand how to document the fields of this tree, since it can be very deep. 而且我不知道如何记录这棵树的字段,因为它可能很深。 What i'm trying to do, this function returns the structure of the documented fields: 我正在尝试做的事情,此函数返回记录的字段的结构:

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

My function works correctly if the empty number of children. 如果孩子人数为空,我的功能将正常运行。 But in the presence of children, an error is returned that I have not documented the entire tree structure. 但是在有孩子的情况下,返回一个错误,我没有记录整个树结构。 This is a lot. 好多 How to do? 怎么做?

I see at least two ways if you only want to document parts of the response and do not want the test to fail on the undocumented parts. 如果您只想记录响应的一部分并且不希望未记录部分的测试失败,那么我会看到至少两种方法。 A part of a response can be documented with subsectionWithPath : 响应的一部分可以用subsectionWithPath记录下来:

If you don't want to provide detailed documentation for all of the fields, an entire subsection of a payload can be documented. 如果您不想提供所有字段的详细文档,则可以记录有效负载的整个子部分。

An alternative is to use relaxedResponseFields : 一种替代方法是使用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. 为此,请在org.springframework.restdocs.payload.PayloadDocumentation上使用relaxedRequestFields和relaxedResponseFields方法。

See https://docs.spring.io/spring-restdocs/docs/2.0.3.RELEASE/reference/html5/#documenting-your-api-request-response-payloads-fields for more details. 有关更多详细信息,请参见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