How to documentation form list parameter?
Controller
@PostMapping("/sample")
public void post(@ModelAttribute PostRequest request) {
// ...
}
@Data
public static class PostRequest {
private MultipartFile file;
private List<PostRow> rows;
}
@Data
public static class PostRow {
private Long id;
}
My test code
mockMvc.perform(
multipart("/sample")
.file(new MockMultipartFile("file", "level.zip", "application/zip", "level.zip".getBytes()))
.param("rows[0].id", "1")
.param("rows[1].id", "2")
.contentType(MediaType.MULTIPART_FORM_DATA)
)
.andDo(document(
"Sample",
requestParts(
partWithName("file").description("level zip file")
),
requestParameters(
parameterWithName("rows[].id").description("row id")
)
)
);
I tried this code, but it throws following error
Request parameters with the following names were not documented: [rows[1].id, rows[0].id]. Request parameters with the following names were not found in the request: [rows[].id]
org.springframework.restdocs.snippet.SnippetException: Request parameters with the following names were not documented: [rows[1].id, rows[0].id]. Request parameters with the following names were not found in the request: [rows[].id]
at org.springframework.restdocs.request.RequestParametersSnippet.verificationFailed(RequestParametersSnippet.java:107)
at org.springframework.restdocs.request.AbstractParametersSnippet.verifyParameterDescriptors(AbstractParametersSnippet.java:108)
at org.springframework.restdocs.request.AbstractParametersSnippet.createModel(AbstractParametersSnippet.java:74)
at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:78)
at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:191)
at org.springframework.restdocs.mockmvc.RestDocumentationResultHandler.handle(RestDocumentationResultHandler.java:52)
at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:201)
I don't want to use "rows[0].id" and "rows[1].id".
What can I do instead?
There's nothing that you can do with REST Docs' current built-in capabilities. REST Docs has no understanding of parameter names and knows nothing of your naming scheme. That means that it can't tell that parameters that look like rows[n].id
are all the same kind of parameter and should only be documented once.