Allow custom templates to be provided for a specific template format

Previously, custom snippet templates were loaded from
org/springframework/restdocs/templates and the default templates were
loaded from org/springframework/restdocs/templates/{$formatId}.
Without relying on the ordering of the classpath, this made it
impossible to provide a custom template for a specific format.

This commit updates the locations that are checked for snippet
templates. The following locations are now checked in order:

1. org/springframework/restdocs/templates/${formatId}/${name}.snippet
2. org/springframework/restdocs/templates/${name}.snippet
3. org/springframework/restdocs/templates/${formatId}/default-${name}.snippet

The second location is provided largely for backwards compatibility
with 1.0. Users are expected to use the first location to provide
any custom templates, with Spring REST Docs provided templates for
all of the built-in snippets in the third location.

Closes gh-196
Closes gh-197
This commit is contained in:
Andy Wilkinson
2016-02-08 13:41:24 +00:00
parent 83b72f7962
commit 6578f42730
26 changed files with 108 additions and 41 deletions

View File

@@ -627,10 +627,14 @@ generated snippets.
templates] are provided for each of the snippets that Spring REST Docs can produce. To
customize a snippet's content, you can provide your own template.
Templates are loaded from the classpath in the `org.springframework.restdocs.templates`
package and each template is named after the snippet that it will produce. For example, to
Templates are loaded from the classpath from an `org.springframework.restdocs.templates`
subpackage. The name of the subpackage is determined by the ID of the template format
that is in use. The default template format, Asciidoctor, has the ID `asciidoctor` so
snippets are loaded from `org.springframework.restdocs.templates.asciidoctor`. Each
template is named after the snippet that it will produce. For example, to
override the template for the `curl-request.adoc` snippet, create a template named
`curl-request.snippet` in `src/test/resources/org/springframework/restdocs/templates`.
`curl-request.snippet` in
`src/test/resources/org/springframework/restdocs/templates/asciidoctor`.