Search code examples
mockmvcspring-auto-restdocs

How and Where to use failOnUndocumentedParams in Spring Auto REST Docs?


I am working with Spring Auto REST Docs, and want to know the use of failOnUndocumentedParams, and how to use it. I want that documentation should not be generated if I miss a field from the POJO. I believe using failOnUndocumentedParams is my solution. However, I don't know how and where to use it.

@Before
public void setUp() throws IOException {

    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
                .alwaysDo(JacksonResultHandlers.prepareJackson(objectMapper))
                .alwaysDo(MockMvcRestDocumentation.document("{class-name}/{method-name}",

                        Preprocessors.preprocessRequest(),
                        Preprocessors.preprocessResponse(
                                ResponseModifyingPreprocessors.replaceBinaryContent(),
                                ResponseModifyingPreprocessors.limitJsonArrayLength(objectMapper),
                                Preprocessors.prettyPrint())))
                .apply(MockMvcRestDocumentation.documentationConfiguration(this.restDocumentation)
                        .uris()
                        .withScheme("http")
                        .withHost("localhost")
                        .withPort(8080)
                        .and().snippets()

                        .withDefaults(CliDocumentation.curlRequest(),
                                HttpDocumentation.httpRequest(),
                                HttpDocumentation.httpResponse(),
                                AutoDocumentation.requestFields(),
                                AutoDocumentation.responseFields(),
                                AutoDocumentation.pathParameters(),
                                AutoDocumentation.requestParameters(),
                                AutoDocumentation.description(),
                                AutoDocumentation.methodAndPath(),
                                AutoDocumentation.section()))
                .build();
}

Here's how my mockMvc looks.


Solution

  • You can configure the feature when creating the request or response field snippets. In your case you want to enable the feature for all tests and thus when setting up Mock MVC:

    .withDefaults(
        ...
        AutoDocumentation.requestFields().failOnUndocumentedFields(true),
        AutoDocumentation.responseFields().failOnUndocumentedFields(true),
        ...)
    

    Alternatively one can enable the feature for a single test:

    document("some-path",
        AutoDocumentation.requestFields().failOnUndocumentedFields(true),
        AutoDocumentation.responseFields().failOnUndocumentedFields(true))