首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取SnippetException:未记录负载的以下部分:数组元素出错

获取SnippetException:未记录负载的以下部分:数组元素出错
EN

Stack Overflow用户
提问于 2019-06-24 17:23:16
回答 1查看 3K关注 0票数 0

我正在尝试使用Spring-RestDocs来记录我的REST服务。但是到目前为止,我还不能记录数组元素。

测试方法:

代码语言:javascript
复制
@Test
public void listAll() throws Exception {

  MockHttpServletRequestBuilder requestBuilder =
            RestDocumentationRequestBuilders.post("/diagnosis/search/{term}", "headache")
                    .header("Authorization",TestHelper.TOKEN).with(csrf());
    TestHelper.httpRequestBuilder(requestBuilder, new SearchEntity("5b55aabd0550de0021097b64",Arrays.asList("PL01", "PL02"),true));

    MvcResult result = mockMvc.perform(requestBuilder)
            .andDo(DiagnosisDocument.documentSearchTerm())
            .andExpect(status().isOk())
            .andReturn();
    MockHttpServletResponse response = result.getResponse();
    System.out.println(response.getContentAsString());
    assertEquals(HttpStatus.OK.value(), response.getStatus());
}

文档化方法:

代码语言:javascript
复制
 public static ResultHandler documentSearchTerm() {
    return document("search-diagnosis", pathParameters(
   parameterWithName("term").description("Term")),

   requestFields(fieldWithPath("clinicId").description("bla bla")),

   requestFields(fieldWithPath("isGlobalSearch").description("bla bla")),

   requestFields(subsectionWithPath("[].planIds").description("bla bla")),
   responseAPI(true));

}

SearchEntity类:

代码语言:javascript
复制
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
public class DiagnosisSearchEntiry {

   private String clinicId;
   private List<String> planIds = new ArrayList<>();
   private boolean isGlobalSearch;

}
But in this implementation, im getting following exception and the test fails.

org.springframework.restdocs.snippet.SnippetException: The following parts of the payload were not documented:
{
  "planIds" : [ "PL01", "PL02" ],
  "globalSearch" : true
}

我得到这个错误有什么特殊的原因吗?我记录错了吗?提前感谢

EN

回答 1

Stack Overflow用户

发布于 2019-06-25 16:53:57

DiagnosisSearchEntiry被序列化为JSON时,isGlobalSearch字段被映射到JSON中名为globalSearch的字段。您的请求字段路径需要更新以反映这一点:

代码语言:javascript
复制
requestFields(fieldWithPath("globalSearch").description("bla bla"))

路径[].planIds正在查找具有planIds字段的对象数组。它将像这样匹配JSON:

代码语言:javascript
复制
[
  {
    "planIds": ["PL01", "PL02"]
  },
  {
    "planIds": ["PL03", "PL04"]
  }
]

您正在记录的JSON的结构如下:

代码语言:javascript
复制
{
  "clinicId": "the clinic id",
  "planIds": [ "PL01", "PL02" ],
  "globalSearch": true  
}

要记录计划ID数组,路径应为planIds.[]

代码语言:javascript
复制
requestFields(subsectionWithPath("planIds.[]").description("bla bla"))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56733518

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档