Commit Graph

172 Commits

Author SHA1 Message Date
Paul-Christian Volkmer
d329e2da5d Use -u 'username:password' for basic auth in the curl request snippet
Previously, when a request was made that used basic auth, the curl
snippet would configure the authentication header with the
Base64-encoded header. This commit updates the snippet to use the
more human-friendly -u option to provide the username and password
in place of the authentication header.

Closes gh-122
2015-09-16 15:44:31 -04:00
Andy Wilkinson
26adbb10ae Add missing copyright header 2015-09-13 12:01:41 -04:00
Andy Wilkinson
14ad59d434 Make descriptions more flexible
Previously, the description of a link, parameter, or field had to be a
String. Given the use of Mustache templates, this was unnecessarily
restrictive. It prevented the use of a richer object, the components of
which could then be accessed in a template.

This commit changes the description of links, parameters, and fields to
be an Object, thereby allowing richer objects to be used. The default
template will call toString on the description during rendering. Custom
templates can be used to make more sophisticated use of the description.

To ensure that the description is consistent across all descriptor
types, it has been moved up onto the AbstractDescriptor superclass.

Closes gh-123
2015-09-13 11:58:22 -04:00
Andy Wilkinson
df40f63238 Add support for using the class name in parameterised operation name
This commit adds support for three new placeholders that can be used
when specifying the name of an operation: {ClassName}, {class-name},
and {class_name}. This allows the operation name to be configured once
in an abstract superclass and reused across multiple test classes.

Closes gh-116
2015-09-13 16:39:54 +01:00
Andy Wilkinson
ebab967e43 Make ContentModifier part of the public API
Previously, ContentModifier and ContentModifyingOperationPreprocessor
were package-private. This meant that anyone wishing to perform content
modification during preprocessing had to reimplement much of the
existing content modifying preprocessor, that than just having to
implement ContentModifier.

This commits make both ContentModifier and
ContentModifyingOperationPreprocessor public so that all custom
content modification is now a matter of implementing a custom
ContentModifier and creating an instance of
ContentModifyingOperationPreprocessor.

Closes gh-121
2015-09-13 16:02:19 +01:00
Andy Wilkinson
dc9af9599e Include documentation samples in the main docs zip
Previously, to see an example of the documentation that can be
produced by Spring REST Docs, users had to clone the repository and
build one of the samples.

This commit makes it easier to see what Spring REST Docs can do by
updating the docs zip that's automatically published to docs.spring.io
to include both the API Guide and the Getting Started Guide from the
RESTful notes sample.

Closes gh-112
2015-09-10 10:10:05 +01:00
Andy Wilkinson
11c0945d10 Polish the documentation produced by the samples
- Correct the location of the Git repository
- Correct the location of the sample within the repository
- Update the build instructions for the Data REST sample to use Maven
- Move the table of contents to the left
- Add section links

Closes gh-113
2015-09-10 10:00:18 +01:00
Andy Wilkinson
ed3d5f0a59 Add javadoc to protected methods added in 5dc28b9 2015-09-09 11:47:19 +01:00
Paul-Christian Volkmer
6dcfdf7206 Update getting started documentation to use new class names
Closes gh-118
2015-09-09 11:46:18 +01:00
Andy Wilkinson
5dc28b9bb0 Improve extensibility of RestDocumentationContextPlaceholderResolver
Previously, RestDocumentationContextPlaceholderResolver could be
subclassed, but it didn’t provide access to its state (the
RestDocumentationContext).

This commit opens up RestDocumentationContextPlaceholderResolver by
adding a getter method for its RestDocumentationContext. It also makes
the kebab-base and snake_case conversion methods protected (rather than
private) so that they can be used by subclasses.

See gh-116
2015-09-09 10:08:28 +01:00
Andy Wilkinson
1e53502958 Make milestone builds are available to the samples 2015-09-08 16:07:15 +01:00
Andy Wilkinson
fe84aef912 Polish the documentation 2015-09-08 15:48:11 +01:00
Andy Wilkinson
63e7466304 Fix javadoc in AbstractParametersSnippet 2015-09-08 15:01:32 +01:00
Andy Wilkinson
94d5e2d94f Improve the extensibility of the descriptor classes
This commit improves the extensibility of the various descriptor
classes. The following changes have been made:

 - Constructors have been made protected to allow them to be called by
   subclasses in a different package
 - Model creation has been moved to the relevant snippet class. This
   improves the separation of concerns as a descriptor should not be
   aware of template-based rendering and the need for a model. The
   method is protected to allow it to be overridden by a custom subclass
   that uses a custom descriptor subclass.
 - Methods that should not be overridden have been made final.

See gh-73
2015-09-08 14:52:07 +01:00
Andy Wilkinson
cbbdbcb201 Allow subclasses to access a snippet’s state
This commit updates each of the built-in snippet implementations to
provide protected getter methods for their fields. This allows
subclasses to access and work with the snippet’s state.

See gh-73
2015-09-08 13:51:13 +01:00
Andy Wilkinson
36dfaecffe Improve unit testing of StandardTemplateResourceResolver 2015-09-08 12:54:30 +01:00
Andy Wilkinson
d09950ecaa Polish MockMvcOperationRequestFactory 2015-09-08 12:29:36 +01:00
Andy Wilkinson
875dde3b6f Polish exception handling in snippet implementations 2015-09-08 11:40:57 +01:00
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