ScaCap / spring-auto-restdocs

Spring Auto REST Docs is an extension to Spring REST Docs
https://scacap.github.io/spring-auto-restdocs/
Apache License 2.0
311 stars 86 forks source link

Unresolved directive with custom Result Handler #365

Closed saeidrastak closed 4 years ago

saeidrastak commented 4 years ago

I document with mockMvc and create a custom document method as below:

    public static ResultHandler document(String identifier) {
        List<String> snippets = new ArrayList<>();
        snippets.add("auto-links");
        snippets.add("auto-description");
        snippets.add("auto-method-path");
        snippets.add("auto-path-parameters");
        snippets.add("auto-request-fields");
        snippets.add("auto-request-parameters");
        snippets.add("auto-response-fields");
        snippets.add("curl-request");
        snippets.add("http-response");
        snippets.add("request-headers");
        return result -> {
            JacksonResultHandlers.prepareJackson(new ObjectMapper()).handle(result);
            MockMvcRestDocumentation.document(identifier,
                    Preprocessors.preprocessRequest(prettyPrint()),
                    Preprocessors.preprocessResponse(
                            ResponseModifyingPreprocessors.replaceBinaryContent(),
                            ResponseModifyingPreprocessors.limitJsonArrayLength(new ObjectMapper()),
                            prettyPrint()),
                    CliDocumentation.curlRequest(),
                    HttpDocumentation.httpRequest(),
                    HttpDocumentation.httpResponse(),
                    AutoDocumentation.requestFields(),
                    AutoDocumentation.requestHeaders(),
                    AutoDocumentation.responseFields(),
                    AutoDocumentation.pathParameters(),
                    AutoDocumentation.requestParameters(),
                    AutoDocumentation.description(),
                    AutoDocumentation.embedded(),
                    AutoDocumentation.authorization("Authorization"),
                    AutoDocumentation.links(),
                    AutoDocumentation.methodAndPath(),
                    AutoDocumentation.sectionBuilder().snippetNames(snippets).skipEmpty(true).build(),
                    requestHeaders(headerWithName("Authorization").description("Basic auth credentials"))
            ).handle(result);
        };
    }

Sample Generated section.adoc file for a sample request is:

[[resources-dpi-find-by-example]]
=== Find By Example

include::auto-method-path.adoc[]

include::auto-description.adoc[]

==== Example request

include::curl-request.adoc[]

==== Example response

include::http-response.adoc[]

==== Request headers

include::request-headers.adoc[]

which the request-parameters part is missing. when I change snippets.add("auto-request-parameters"); to snippets.add("request-parameters"); then generated section.adoc file will change like this:

[[resources-dpi-find-by-example]]
=== Find By Example

include::auto-method-path.adoc[]

include::auto-description.adoc[]

==== Query parameters

include::request-parameters.adoc[]

==== Example request

include::curl-request.adoc[]

==== Example response

include::http-response.adoc[]

==== Request headers

include::request-headers.adoc[]

As you see the Query parameters part is added but with wrong path to include::request-parameters.adoc[] instead of include::auto-request-parameters.adoc[]. (The file auto-request-parameters.adoc is created.)

jmisur commented 4 years ago

This line skips empty sections: AutoDocumentation.sectionBuilder().snippetNames(snippets).skipEmpty(true).build(). If you want to include them anyway, change it to false.

Regarding the include, if you add request-parameters to snippets collection, it means classic SRD snippet will be added to the section snippet (hence the include pointing to that, not to auto-request-parameters). That one doesn't use skipEmpty flag so it will be included every time. If you add auto-request-parameters to snippet collection, that one is SARD snippet and thus will honor global flags (such as skipEmpty) and you'll see that it was used ininclude:`.