Commit Graph

132 Commits

Author SHA1 Message Date
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
Andy Wilkinson
ba5ed62605 Merge branch 'gh-74' 2015-06-17 13:47:09 +01:00
Andy Wilkinson
aeb88eb313 Update the documentation to reflect that links can now be optional
Closes gh-74
2015-06-17 13:44:26 +01:00
Kellen Dye
0699cdd068 Allow links to be optional
Previously, if a link was documented then it had to be present in the
response payload for the test to pass. This was unnecessarily
restrictive and caused problems when a link was omitted due to the
application's current state.

This commit allows a link to be declared as optional. If a link is
optional then the test will pass irrespective of the link's presence
in the response.

See gh-74
2015-06-17 13:40:04 +01:00
Andy Wilkinson
0e5151e91b Bump the version to 1.0.0.BUILD-SNAPSHOT 2015-06-16 19:55:41 +01:00
Andy Wilkinson
e5840cd48f Set the group on the root project so the docs are published correctly
See gh-37
2015-06-16 17:30:09 +01:00
Andy Wilkinson
833f0eabb8 Polishing: replace spaces with tabs in doc examples
See gh-37
2015-06-16 17:21:43 +01:00
Andy Wilkinson
8307fb5428 Add reference documentation and create docs zip
Add reference documentation for the project and add a docs artifact
that contains both the reference documentation and the javadoc.

See gh-37
2015-06-16 17:14:31 +01:00
Andy Wilkinson
f98bec317e Add support for documenting fields in array payloads
764daf7 added support for documenting fields in payloads that contain
arrays, but the array had to be nested within a map. This commit builds
on that support to allow fields in an array payload to be
documented. The fields in the payload:

[
    {
        "a": {
            "b": 5
        }
    },
    {
        "a": {
            "c": "charlie"
        }
    }
]

Can be documented using:

[]a.b
[]a.c
[]a

A dot separator can, optionally, be used between the [] and the map key:

[].a.b
[].a.c
[].a

Closes gh-69
2015-05-15 21:05:13 +01:00
Andy Wilkinson
0c0d3b89bd Default snippet encoding to UTF-8 and make it configurable
Previously, snippets were written to disk using the JVM’s default
encoding. While this could be controlled by the file.encoding system
property it was not ideal as, if the proper was not always set, it could
lead to platform-dependent output being generated. Furthermore,
Asciidoctor uses UTF-8 encoding by default so there was a risk of the
snippets being generated in a different encoding to the encoding
expected by Asciidoctor.

This commit updates the configuration so that, by default, snippets are
encoded as UTF-8. The RestDocumentationConfigurer API has been enhanced
to allow this default to be configured as part of building the
MockMvc instance. This enhancement as brought with it a more fluent
configuration API that separates configuration that is related to URIs
from configuration that is related to snippets.

Closes gh-67
2015-05-13 11:18:56 +01:00
Andy Wilkinson
68198b3145 Increase MaxPermSize so that the build passes when using Java 7 2015-05-05 13:16:21 +01:00
Andy Wilkinson
90bbb5ab9e Update samples to make them compatible with Java 7
The main project supports Java 7 or later, but, prior to this commit,
the Spring HATEOAS sample required Java 8. This commit updates updates
the Spring HATEOAS sample to make it compatible with Java 7 and
updates the build configuration of both samples to specify Java 7.

Closes gh-51
2015-05-05 12:09:45 +01:00
Dewet Diener
84fe379f64 Add a response post processor that performs generic pattern replacement
This commit introduces a new ResponsePostProcessor,
PatternReplacingResponsePostProcessor, that modifies the content of the
response by replacing occurrences of a regular expression with a
configurable replacement. The existing LinkMaskingResponsePostProcessor,
which was already performing pattern replacement, has been updated to
subclass PatternReplacingResponsePostProcessor.

Closes gh-65
2015-05-05 11:39:13 +01:00
Jonathan Pearlin
ca375c919c Improve support for documenting PUT requests
Previously, no special consideration was given to PUT requests. This
meant that curl and HTTP request snippets would not include the
request’s parameters/query string and that HTTP request snippets would
not set the content type to application/x-www-form-urlencoded.

This commit addresses these limitations by updating both
HttpDocumentation and CurlDocumentation to treat PUT requests in the
same way as POST requests.

Closes gh-62
2015-04-30 14:50:44 +01:00
Andy Wilkinson
e5f35c98f3 Add support for post-processing a response before it is documented
This commit adds support for post-processing a response before it is
documented. Post-processors are provided to pretty-print JSON and XML
responses, remove headers from the response, and mask the hrefs of
Atom- and HAL-formatted links in JSON responses.

Response post-processing is configured prior to configuring the
documentation. For example,

mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
		.andDo(modifyResponseTo(prettyPrintContent(), removeHeaders("a"),
				maskLinks()).andDocument("post-processed"));

Closes gh-61
Closes gh-31
Closes gh-54
2015-04-30 14:06:40 +01:00
Andy Wilkinson
b3eee08b5d Add support for documenting a request’s query parameters
This commit adds support for documenting a request's query parameters.
For example:

mockMvc.perform(
        get("/issues").param("page", "2").param("limit", "20"))
                .andDo(documentQueryParameters("list-issues",
                        parameterWithName("page")
                                .description("Page number to return"),
                        parameterWithName("limit")
                                .description("Maximum page size")));

Closes gh-39
2015-04-29 10:31:04 +01:00
Andy Wilkinson
b362c493ff Use HttpHeaders to simplify HTTP request documentation 2015-04-29 10:21:41 +01:00
Andy Wilkinson
bff26b1cab Order documented links use the order of the provided descriptors 2015-04-29 10:18:29 +01:00
Andy Wilkinson
9749ec18eb Remove package tangle caused by RestDocumentationException 2015-04-29 10:09:21 +01:00
Andy Wilkinson
37f9f02580 Make the ordering of extracted links match the ordering in the payload
Closes gh-63
2015-04-29 10:05:42 +01:00
Andy Wilkinson
bad5bdabaa Improve test failure output to ease diagnosis of Travis CI failures 2015-04-29 09:34:52 +01:00
Andy Wilkinson
da7efa3e3a Improve testing of the generated documentation snippets 2015-04-28 18:17:39 +01:00
Andy Wilkinson
764daf7c99 Add support for documenting fields in payloads that contain arrays
Previously the path used to document a field had to be dot-separated,
with each segment of the path being used as a key in a map. This made
it impossible to document fields in a payload within an array.

This commit adds support for using [] in a field’s path to represent an
array. For example, the fields in following JSON payload:

{
    "id": 67,
    “date”: "2015-01-20",
    "assets": [
        {
            "id":356,
            "name": "sample"
        }
    ]
}

Can now be documented using the following paths:

 - id
 - date
 - assets[].id
 - assets[].name

Closes gh-60
2015-04-27 16:43:50 +01:00