ePages-de / restdocs-api-spec

Adds API specification support to Spring REST Docs
MIT License
392 stars 103 forks source link

MockMvcRestDocumentationWrapper doesn't seem to deal with "relaxed" fields #120

Open pzieminski opened 4 years ago

pzieminski commented 4 years ago

This is probably a feature request more than anything.

When the MockMvcRestDocumentationWrapper.document drop in replacement is used in snippets like below, the tests start generating errors despite the relaxed* factories for snippets used

        this.getMockMvc().perform(
                RestDocumentationRequestBuilders.post(copyHref).headers(getMockMvcHeaders(super.getTestToken()))
                        .content(locatorjson).accept(APPLICATION_HAL_JSON)
                        .contentType(APPLICATION_HAL_JSON))
                .andDo(
                        document("file/copy_file", REQUEST_PREPROCESSOR, RESPONSE_PREPROCESSOR,
                                requestHeaders(
                                        headerWithName(HttpHeaders.AUTHORIZATION).description(""),
                                        headerWithName(HttpHeaders.ACCEPT).description("")
                                ),
                                responseHeaders(
                                        headerWithName(HttpHeaders.CONTENT_TYPE)
                                                .description(""),
                                        headerWithName(HttpHeaders.LOCATION).description("")
                                ),
                                relaxedRequestFields(
                                        fieldWithPath("href").description(""),
                                        fieldWithPath("name").optional().description("")
                                )
                        )
                )
                .andExpect(status().isCreated()).andReturn();

then with the request

Accept: application/hal+json
{
  "href" : "value1",
  "id" : null,
  "name" : "value2",
  "allRenditions" : false
}

I see the following error

The following parts of the payload were not documented:
{
  "id" : null,
  "allRenditions" : false
}

when undocumented fields that are in request in this case should be ignored (we have some defaults in the input beans that we don't care about and want omitted in this case).

The same happens for other factories like

org.springframework.restdocs.hypermedia.HypermediaDocumentation#relaxedLinks(org.springframework.restdocs.hypermedia.LinkDescriptor...)
org.springframework.restdocs.payload.PayloadDocumentation#relaxedResponseFields(org.springframework.restdocs.payload.FieldDescriptor...)

etc

mduesterhoeft commented 4 years ago

Indeed this is not implemented at the moment. I agree - we should add that.