Commit Graph

154 Commits

Author SHA1 Message Date
Andy Wilkinson
fece9547ea Automatically align version of samples' dependency with main version
This commit enhances the tasks that build the samples to
automatically update the version of their spring-restdocs dependency
to match the version of the main project.
2015-09-08 11:25:06 +01:00
Andy Wilkinson
c2da4c912a Make the built-in snippets more extensible
The commit opens up all of the default snippets so that it's easier
to extend them and modify their behaviour. All of the built-in
snippets are now public with protected constructors. Also, where
appropriate the models used by the snippets have been made more
fine-grained.

Closes gh-73
2015-09-08 10:30:50 +01:00
Andy Wilkinson
de095eb9fe Add package-info.java to each package
Closes gh-109
2015-09-07 13:20:03 +01:00
Andy Wilkinson
7b1d9e714c Allow preprocessing to be applied to every test
Following the reworking of the API, it was no longer possible to
apply preprocessing to every test and to still customize the snippets
used by that test.

This commit adds a new snippets() method to
RestDocumentationResultHandler that allows additional snippets to be
configured once the result handler has been created. The documentation
has been updated to describe how to apply preprocessing to every
test and Spring HATEOAS sample has been updated to illustrate the
approach.

Closes gh-88
2015-09-07 11:44:12 +01:00
Andy Wilkinson
570781832e Eclipse metadata for new spring-restdocs-mockmvc project 2015-09-07 10:35:09 +01:00
Andy Wilkinson
d80c0f751a Cope with different content length due to Windows line endings
Windows uses \r\n for line endings whereas Unix-like platforms us
\n. This causes the content length of a pretty-printed response to
be longer on Windows.
2015-09-03 15:15:38 +01:00
Andy Wilkinson
51051de4c4 Improve contents of generated pom files
Previously, the generated pom files were uncustomised and relied on
Gradle’s default output. Such pom files are problematic for a couple
of reasons: they include test dependencies and they don’t include the
metadata that’s required for publishing to Maven Central.

This commit customises the generated pom files to include the required
metadata and to remove any test dependencies.
2015-09-03 15:07:59 +01:00
Andy Wilkinson
7235a49dbc Update sample verification to look for snippets in correct location
Previously each sample was built with both Maven and Gradle and the
snippets produced by each build were verified. Now each sample is only
built with either Maven or Gradle. This commit updates the snippet
verification process to look for snippets in the location that’s
appropriate for the sample’s build system.
2015-09-03 15:05:45 +01:00
Andy Wilkinson
2b2b6fcd25 Isolate and reduce Spring Test dependencies
This commit splits Spring REST Docs into two projects –
spring-restdocs-core and spring-restdocs-mockmvc.

spring-restdocs-core contains the vast majority of the code but does not
depend on a specific test framework other than JUnit. The use of a
Spring Test TestExecutionListener has been replaced with a JUnit test
rule. The rule is declared once per test class and configured with
the output directory to which the generated snippets should be written.
This simplifies the implementation as thread local storage is no longer
required to transfer information about the test that’s running into
Spring REST Docs. Instead, this transfer is now handled by the new test
rule. It has also simplified the configuration as it’s no longer
necessary for users to provide a system property that configures the
output directory.

spring-restdocs-mockmvc contains code that’s specific to using Spring
REST Docs with Spring MVC Test’s MockMvc. This is currently the only
testing framework that’s supported, but it paves the way for adding
support for additional frameworks. REST Assured is one that users seem
particularly interested in (see gh-80 and gh-102).

Closes gh-107
2015-09-03 14:19:32 +01:00
Andy Wilkinson
9d8bbf0558 Add support for modifying a request prior to it being documented
Prior to this commit it was not possible to modify a request prior to
it being documented, only a response. This commit builds on the new
Operation abstraction to simplify the existing response modification
support and to add support for request modification.

Closes gh-84
2015-09-02 13:05:12 +01:00
Andy Wilkinson
3579b34a77 Introduce a new operation abstraction to reduce coupling with MockMvc
Prior to this commit, MockMvc-classes were used throughout Spring REST
Docs. For example, each Snippet was called with an MvcResult. This
has proven problematic for a few reasons:

1. The MockMvc APIs aren't very amenable to modifying a request or
   response before it's documented. This caused the existing support
   for response modification to rely on CGLib proxies and method
   interceptors. A similary complex solution for request modifiction
   would also have been necessary.
2. Things are harder to reason about than they need to be as the
   MockHttpServletRequest and MockHttpServletResponse classes
   expose more than is required when generating API documentation.
3. Supporting other test frameworks, such as Rest Assured, is hard

This commit introduces a new Operation abstract that encapsulates
all of the information required to document the request that was sent
and the response that was received when performing an operation on a
RESTful service. The new abstraction uses types from Spring's Web
support, such as HttpHeaders, RequestMethod, and MediaType, but does
not rely on MockMvc.

Closes gh-108
2015-09-02 12:47:19 +01:00
Andy Wilkinson
65e37a6276 Update tests to cope with constraint ordering being unspecified 2015-08-25 15:28:17 +01:00
Andy Wilkinson
ff822bd88d Provide an API to ease documenting a request field’s constraints
Previously, users that wanted to document a request field’s constraints
had to roll their own solution. This commit introduces a new API that
makes it easier to document constraints. Support is provided for
discovering Bean Validation constraints and resolving descriptions for
them. The constraint descriptions can then be used as required. For
example, they can included in a field’s description or in an additional
constraints attribute that’s included in an additional table column via
the use of a custom snippet template.

Closes gh-42
2015-08-25 15:00:40 +01:00
Andy Wilkinson
bc5a9c3714 Update the samples to use Spring Boot 1.2.5 2015-08-19 18:01:25 +01:00
Andy Wilkinson
2842d5f75f Update Travis CI configuration to run on new infrastructure 2015-08-19 17:43:31 +01:00
Andy Wilkinson
32bb724a3d Update the samples to align with recent API changes 2015-08-19 17:25:56 +01:00
Andy Wilkinson
4199a21bfd Documentation polishing 2015-08-19 17:15:36 +01:00
Andy Wilkinson
3fd65791d1 Add support for documenting fields in XML payloads
Closes gh-46
2015-08-19 17:08:34 +01:00
Andy Wilkinson
76dd0cc579 Include request’s path (without query string) in path parameters snippet
The ordering of parameters in the path is important so, without seeing
the path and the order in which the parameters appear, documentation for
the individual parameters is of limited use.

This commit enhances the path parameters snippet to include the
request’s path, minus any query string, in the snippet. By default,
the path is rendered as the title of the parameters table. As with
other snippets, this can be customized by providing a custom template
for the snippet.

Closes gh-103
2015-08-18 11:33:42 +01:00
Andy Wilkinson
d3e1a0d1b6 Provide obvious support for documenting all request parameters
Previously, the snippet for documenting a request’s parameters was
referred to as the query parameters snippet. This was misleading as the
snippet would actually document anything in the request’s parameters
map, not just parameters from the request’s query string.

This commit renames the snippet and the associated templates, etc so
that it is now known as the request parameters snippet. This provides
a more accurate reflection of it being able to document all of a
request’s parameters, not just those from its query string.

Closes gh-104
2015-08-18 10:11:03 +01:00
Andy Wilkinson
dfd65f9f10 Polishing: rename test classes following refactoring in 0c8a7137 2015-08-18 09:15:50 +01:00
Andy Wilkinson
0c8a71370b Rework the API to improve readability and extensibility
This commit updates the API to improve its extensibility and
readability.

SnippetWritingResultHandler has been replaced with a  more general
purpose Snippet interface. Snippets are now provided to the main
document method using varargs rather than the various with… methods
that were previously used. As a result a custom Snippet implementation
can now be used in exactly the same way as any of the built-in
snippets:

this.mockMvc.perform(get("/"))
        .andExpect(status().isOk())
        .andDo(document("index-example",
                links(
                        linkWithRel("notes").description("…"),
                        linkWithRel("tags").description("…")),
                responseFields(
                        fieldWithPath("_links").description("…")),
                yourCustomSnippet()));

Control of the snippets that are generated by default is now available
via RestDocumentationConfigurer:

this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
        .apply(documentationConfiguration().snippets()
               .withDefaults(curlRequest(), yourCustomSnippet()))
        .build();

See gh-73
2015-08-10 12:06:32 +01:00
Andy Wilkinson
aaf8aab77e Polishing 2015-07-29 16:31:29 +01:00
Andy Wilkinson
458718c443 Improve test coverage of RestDocumentationResultHandler 2015-07-29 16:27:30 +01:00
Marcel Overdijk
dc30f1bd63 Fix RestDocumentationResultHandler.withPathParameters
Closes gh-101
2015-07-29 16:26:57 +01:00
Andy Wilkinson
8e2fc7f45d Provide support for documenting path parameters
This commit adds support for documenting a request's path parameters.
For example:

mockMvc.perform(
		get("/locations/{latitude}/{longitude}", 51.5072, 0.1275))
			.andExpect(status().isOk())
			.andDo(document("locations").withPathParameters(
					parameterWithName("latitude")
							.description("The location's latitude"),
					parameterWithName("longitude")
							.description("The location's longitude")
			));

Closes gh-86
2015-07-29 14:50:49 +01:00
Andy Wilkinson
e5c992c249 Upgrade to Spring Framework 4.1.7.RELEASE 2015-07-28 17:44:50 +01:00
Andy Wilkinson
1175ca91e2 Fix package tangles 2015-07-28 16:16:46 +01:00
Andy Wilkinson
ccc241860a Isolate use of TemplateEngine into existing common base class 2015-07-28 16:08:38 +01:00
Andy Wilkinson
c3a9bdfa94 Allow users to control the writer that is used to produce each snippet
Previously, SnippetWritingResultHandler hard-coded its own logic for
creating the Writer that used to write its snippet. This made it
impossible to take complete control over the output location, filename
suffix, etc.

This commit introduces a new interface, WriterResolver, and a default
implementation, StandardWriterResolver. It provides the same
functionality as before, but SnippetWritingResultHandler now
retrieves an instance of WriterResolver from the request's attributes.
This allows users to provide their own WriterResolver implementation.

Closes gh-100
2015-07-28 15:46:00 +01:00
Andy Wilkinson
e91e8c8490 Upgrade to Gradle 2.5 2015-07-28 11:58:04 +01:00
Andy Wilkinson
fcd0492da2 Fix compilation error in PayloadDocumentationTests 2015-07-28 11:10:30 +01:00
Andy Wilkinson
02c6c89172 Allow documentation of extra attributes that apply to the whole snippet
This commit adds support for associating custom attributes with the
generation of a particular snippet. The attributes are included in
the model during snippet rendering allowing them to be referenced from
a custom snippet template. Among other things, this makes it possible
to provide a configurable title for snippets that produce a code
block.

Closes gh-77
2015-07-28 11:05:28 +01:00
Andy Wilkinson
70824aa509 Allow documentation of extra attributes on fields, links, and query parameters
This commit adds support for associating custom attributes with field,
link, and query parameter descriptors. These attributes are then
included in the model during snippet rendering. Coupled with a custom
snippet template, this enables the inclusion of extra column(s) in the
generated tables.

Closes gh-70
2015-07-28 11:05:22 +01:00
Andy Wilkinson
4f850cddd9 Use a template engine to produce the documentation snippets
This commit introduces a new TemplateEngine abstraction that is used
to produce the documentation snippets. A default JMustache-based
implementation is provided. JMustache has been repackaged and embedded
to prevent unwanted conflicts and side-effects.

By default, snippet templates are loaded from the classpath in the
org.springframework.restdocs.templates package. Default snippet
templates are provided for all of the snippets that can be generated.
Each of these templates is named after the snippet that it will
produce – the snippet {name}.adoc is produced by the snippet template
default-{name}.snippet. A snippet named {name}.snippet, if present,
will be used in preference to the default snippet, thereby allowing
the default snippets to be overriden.
2015-07-28 10:54:07 +01:00
izeye
6f4fe4ea64 Polish documentation
Closes gh-98
2015-07-27 11:51:04 +01:00
Andy Wilkinson
b1a6840b52 Test that a response that is an array of primitives can be documented
Closes gh-96
2015-07-22 15:06:29 +01:00
Andy Wilkinson
f481e2fc8a Fix request parameter handling in multipart http request snippet
Previously, request parameters for a POST were form encoded into the
body, irrespective of the request’s type. This was incorrect for a
multipart request where each request parameter should be included in a
separate request part.

This commit updates HttpDocumentation to treat request parameters
differently when dealing with a multipart request. In such cases each
request parameter is now included in the body of the request in a
separate request part.

Fixes gh-94
2015-07-20 16:29:31 +01:00
Andy Wilkinson
4c31dc0216 Fix request parameters in curl snippet for multipart requests
Previously, request parameters would be provided via -d. This is
incorrect for a multipart request that is also using -F. This commit
updates the generation of the curl request snippet to use -F for request
parameters when the request is a multipart request.

Closes gh-93
2015-07-20 16:03:26 +01:00
Andy Wilkinson
833a01d6a5 Add a Host: header to HTTP request snippets
HTTP 1.1 requires a Host: header in all requests. This commit updates
the HTTP request snippet to ensure that such a header is always
present.

Closes gh-85
2015-07-13 18:05:39 +01:00
Andy Wilkinson
5d44015e7d Improve description of when fields are considered to be documented
Closes gh-82
2015-07-13 17:46:21 +01:00
Andy Wilkinson
5d0428b9d0 Add support for documenting multipart requests
This commit adds support for documenting multipart requests in both curl
and HTTP request snippets.

Closes gh-48
2015-07-13 17:32:38 +01:00
Andy Wilkinson
88e26ba88b Improve diagnostics when no type is provided for absent optional field
Previously, if an optional field was being documented and that field
was not present in the payload a failure would occur with the message
"The payload does not contain a field with the path 'the.field.path'".
This isn't very helpful as it doesn't explain why the field was being
looked for (to resolve its type).

This commit improves the diagnostics to improve the message to explain
that a field's type could not be determined as it didn't exist in the
payload and to suggest the use of FieldDescriptor.type(FieldType) to
provide a type.

Closes gh-83
2015-07-13 12:26:29 +01:00
Andy Wilkinson
cdcddcf91b Update the samples to use Spring Boot 1.2.4.RELEASE 2015-06-24 14:36:47 +01:00
Andy Wilkinson
f705656595 Polishing
- Make inner classes static where possible
 - Declare classes as final where appropriate
 - Use try-with-resources
 - Improve readability by breaking up some large methods
 - Do not throw Throwable where possible
2015-06-24 14:25:31 +01:00
Andy Wilkinson
8a0b07576d Update README and reference docs to avoid duplication of content 2015-06-24 11:45:30 +01:00
Andy Wilkinson
1ae014dc53 Update build script examples in docs to use dynamic dependency version 2015-06-24 09:38:58 +01:00
Andy Wilkinson
9824e8a198 Include the version in the documentation's header 2015-06-23 18:00:37 +01:00
Andy Wilkinson
b6c64dfd26 Update the samples to build against correct version of main project
This should have been done as part of 0e5151e91.
2015-06-23 14:05:07 +01:00
Andy Wilkinson
7cdbf0cea9 Add a section to the docs that provides tips on working with Asciidoctor
Closes gh-75
2015-06-23 14:02:18 +01:00