Skip to content

Commit 4848bbd

Browse files
committed
Improve RESTAssure documentation
1 parent 9edc991 commit 4848bbd

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

README.md

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ We do not like enriching our production code with this information and clutter i
3737
We agree with Spring REST Docs that the test-driven way to produce accurate API documentation is the way to go.
3838
This is why we came up with this project.
3939

40-
<!-- TOC depthFrom:2 -->
4140

4241
- [Motivation](#motivation)
4342
- [Getting started](#getting-started)
4443
- [Project structure](#project-structure)
4544
- [Build configuration](#build-configuration)
4645
- [Gradle](#gradle)
4746
- [Maven](#maven)
48-
- [Usage with Spring REST Docs - MockMvc](#usage-with-spring-rest-docs---mockmvc)
49-
- [Usage with Spring REST Docs - RestAssured](#usage-with-spring-rest-docs---rest-assured)
47+
- [Usage with Spring REST Docs](#usage-with-spring-rest-docs)
5048
- [Documenting Bean Validation constraints](#documenting-bean-validation-constraints)
5149
- [Migrate existing Spring REST Docs tests](#migrate-existing-spring-rest-docs-tests)
50+
- [MockMvc based tests](#mockmvc-based-tests)
51+
- [REST Assured based tests](#rest-assured-based-tests)
5252
- [Security Definitions in OpenAPI](#security-definitions-in-openapi)
5353
- [Running the gradle plugin](#running-the-gradle-plugin)
5454
- [OpenAPI 2.0](#openapi-20)
@@ -59,20 +59,16 @@ This is why we came up with this project.
5959
- [OpenAPI 3.0.1](#openapi-301-1)
6060
- [Generate an HTML-based API reference from OpenAPI](#generate-an-html-based-api-reference-from-openapi)
6161
- [RAML](#raml)
62-
- [Limitations](#limitations)
63-
- [Rest Assured](#rest-assured)
64-
65-
<!-- /TOC -->
6662

6763
## Getting started
6864

6965
### Project structure
7066

7167
The project consists of the following main components:
7268

73-
- [restdocs-api-spec](restdocs-api-spec) - contains the actual Spring REST Docs extension.
74-
This is most importantly the [ResourceDocumentation](restdocs-api-spec/src/main/kotlin/com/epages/restdocs/apispec/ResourceDocumentation.kt) which is the entrypoint to use the extension in your tests.
75-
The [ResourceSnippet](restdocs-api-spec/src/main/kotlin/com/epages/restdocs/apispec/ResourceSnippet.kt) is the snippet used to produce a json file `resource.json` containing all the details about the documented resource.
69+
- [restdocs-api-spec](restdocs-api-spec) - contains the actual Spring REST Docs extension.
70+
This is most importantly the [ResourceDocumentation](restdocs-api-spec/src/main/kotlin/com/epages/restdocs/apispec/ResourceDocumentation.kt) which is the entrypoint to use the extension in your tests.
71+
The [ResourceSnippet](restdocs-api-spec/src/main/kotlin/com/epages/restdocs/apispec/ResourceSnippet.kt) is the snippet used to produce a json file `resource.json` containing all the details about the documented resource.
7672
- [restdocs-api-spec-mockmvc](restdocs-api-spec-mockmvc) - contains a wrapper for `MockMvcRestDocumentation` for easier migration to `restdocs-api-spec` from MockMvc tests using plain `spring-rest-docs-mockmvc`.
7773
- [restdocs-api-spec-restassured](restdocs-api-spec-restassured) - contains a wrapper for `RestAssuredRestDocumentation` for easier migration to `restdocs-api-spec` from RestAssured tests using plain `spring-rest-docs-restassured`.
7874
- [restdocs-api-spec-gradle-plugin](restdocs-api-spec-gradle-plugin) - adds a gradle plugin that aggregates the `resource.json` files produced by `ResourceSnippet` into an API specification file for the whole project.
@@ -139,7 +135,7 @@ See the [build.gradle](samples/restdocs-api-spec-sample/build.gradle) for the se
139135
The root project does not provide a maven plugin.
140136
But you can find a plugin that works with `restdocs-api-spec` at [BerkleyTechnologyServices/restdocs-spec](https://github.com/BerkleyTechnologyServices/restdocs-spec).
141137

142-
### Usage with Spring REST Docs - MockMvc
138+
### Usage with Spring REST Docs
143139

144140
The class [ResourceDocumentation](restdocs-api-spec/src/main/kotlin/com/epages/restdocs/apispec/ResourceDocumentation.kt) contains the entry point for using the [ResourceSnippet](restdocs-api-spec/src/main/kotlin/com/epages/restdocs/apispec/ResourceSnippet.kt).
145141

@@ -151,7 +147,7 @@ mockMvc
151147
.andDo(document("carts-create", resource("Create a cart")));
152148
```
153149

154-
This test will produce the `resource.json` file in the snippets directory.
150+
This test will produce the `resource.json` file in the snippets directory.
155151
This file just contains all the information that we can collect about the resource.
156152
The format of this file is not specific to an API specification.
157153

@@ -217,50 +213,30 @@ Please see the [CartIntegrationTest](samples/restdocs-api-spec-sample/src/test/j
217213

218214
**:warning: Use `template URIs` to refer to path variables in your request**
219215

220-
Note how we use the `urlTemplate` to build the request with [`RestDocumentationRequestBuilders`](https://docs.spring.io/spring-restdocs/docs/current/api/org/springframework/restdocs/mockmvc/RestDocumentationRequestBuilders.html#get-java.lang.String-java.lang.Object...-).
221-
This makes the `urlTemplate` available in the snippet and we can depend on the non-expanded template when generating the OpenAPI file.
216+
Note how we use the `urlTemplate` to build the request with [`RestDocumentationRequestBuilders`](https://docs.spring.io/spring-restdocs/docs/current/api/org/springframework/restdocs/mockmvc/RestDocumentationRequestBuilders.html#get-java.lang.String-java.lang.Object...-).
217+
This makes the `urlTemplate` available in the snippet and we can depend on the non-expanded template when generating the OpenAPI file.
222218

223219
```java
224220
mockMvc.perform(get("/carts/{id}", cartId)
225221
```
226222

227-
### Usage with Spring REST Docs - REST Assured
228-
The usage for REST Assured is similar to MockMVC, except that [com.epages.restdocs.apispec.RestAssuredRestDocumentationWrapper](restdocs-api-spec/src/main/kotlin/com/epages/restdocs/apispec/RestAssuredRestDocumentationWrapper.kt) is used instead of [com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper](restdocs-api-spec/src/main/kotlin/com/epages/restdocs/apispec/MockMvcRestDocumentationWrapper.kt).
223+
### Documenting Bean Validation constraints
229224

230-
To use the ``RestAssuredRestDocumentationWrapper``, you have to add a dependency to [restdocs-api-spec-restassured](restdocs-api-spec-restassured) to your build.
231-
```java
232-
RestAssured.given(this.spec)
233-
.filter(RestAssuredRestDocumentationWrapper.document("{method-name}",
234-
"The API description",
235-
requestParameters(
236-
parameterWithName("param").description("the param")
237-
),
238-
responseFields(
239-
fieldWithPath("doc.timestamp").description("Creation timestamp")
240-
)
241-
))
242-
.when()
243-
.queryParam("param", "foo")
244-
.get("/restAssuredExample")
245-
.then()
246-
.statusCode(200);
247-
```
248-
249-
### Documenting Bean Validation constraints
250-
251-
Similar to the way Spring REST Docs allows to use [bean validation constraints](https://docs.spring.io/spring-restdocs/docs/current/reference/html5/#documenting-your-api-constraints) to enhance your documentation, you can also use the constraints from your model classes to let `restdocs-api-spec` enrich the generated JsonSchemas.
252-
`restdocs-api-spec` provides the class [com.epages.restdocs.apispec.ConstrainedFields](restdocs-api-spec/src/main/kotlin/com/epages/restdocs/apispec/ConstrainedFields.kt) to generate `FieldDescriptor`s that contain information about the constraints on this field.
225+
Similar to the way Spring REST Docs allows to use [bean validation constraints](https://docs.spring.io/spring-restdocs/docs/current/reference/html5/#documenting-your-api-constraints) to enhance your documentation, you can also use the constraints from your model classes to let `restdocs-api-spec` enrich the generated JsonSchemas.
226+
`restdocs-api-spec` provides the class [com.epages.restdocs.apispec.ConstrainedFields](restdocs-api-spec/src/main/kotlin/com/epages/restdocs/apispec/ConstrainedFields.kt) to generate `FieldDescriptor`s that contain information about the constraints on this field.
253227

254228
Currently the following constraints are considered when generating JsonSchema from `FieldDescriptor`s that have been created via `com.epages.restdocs.apispec.ConstrainedFields`
255229
- `NotNull`, `NotEmpty`, and `NotBlank` annotated fields become required fields in the JsonSchema
256230
- for String fields annotated with `NotEmpty`, and `NotBlank` the `minLength` constraint in JsonSchema is set to 1
257231
- for String fields annotated with `Length` the `minLength` and `maxLength` constraints in JsonSchema are set to the value of the corresponding attribute of the annotation
258232

259-
If you already have your own `ConstraintFields` implementation you can also add the logic from `com.epages.restdocs.apispec.ConstrainedFields` to your own class.
233+
If you already have your own `ConstraintFields` implementation you can also add the logic from `com.epages.restdocs.apispec.ConstrainedFields` to your own class.
260234
Here it is important to add the constraints under the key `validationConstraints` into the attributes map if the `FieldDescriptor`.
261235

262236
### Migrate existing Spring REST Docs tests
263237

238+
#### MockMvc based tests
239+
264240
For convenience when applying `restdocs-api-spec` to an existing project that uses Spring REST Docs, we introduced [com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper](restdocs-api-spec/src/main/kotlin/com/epages/restdocs/apispec/MockMvcRestDocumentationWrapper.kt).
265241

266242
In your tests you can just replace calls to `MockMvcRestDocumentation.document` with the corresponding variant of `MockMvcRestDocumentationWrapper.document`.
@@ -289,6 +265,30 @@ resultActions
289265
This will do exactly what `MockMvcRestDocumentation.document` does.
290266
Additionally it will add a `ResourceSnippet` with the descriptors you provided in the `RequestFieldsSnippet`, `ResponseFieldsSnippet`, and `LinksSnippet`.
291267

268+
#### REST Assured based tests
269+
270+
Also for REST Assured we offer a convenience wrapper similar to `MockMvcRestDocumentationWrapper`.
271+
The usage for REST Assured is also similar to MockMVC, except that [com.epages.restdocs.apispec.RestAssuredRestDocumentationWrapper](restdocs-api-spec/src/main/kotlin/com/epages/restdocs/apispec/RestAssuredRestDocumentationWrapper.kt) is used instead of [com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper](restdocs-api-spec/src/main/kotlin/com/epages/restdocs/apispec/MockMvcRestDocumentationWrapper.kt).
272+
273+
To use the `RestAssuredRestDocumentationWrapper`, you have to add a dependency to [restdocs-api-spec-restassured](restdocs-api-spec-restassured) to your build.
274+
```java
275+
RestAssured.given(this.spec)
276+
.filter(RestAssuredRestDocumentationWrapper.document("{method-name}",
277+
"The API description",
278+
requestParameters(
279+
parameterWithName("param").description("the param")
280+
),
281+
responseFields(
282+
fieldWithPath("doc.timestamp").description("Creation timestamp")
283+
)
284+
))
285+
.when()
286+
.queryParam("param", "foo")
287+
.get("/restAssuredExample")
288+
.then()
289+
.statusCode(200);
290+
```
291+
292292
### Security Definitions in OpenAPI
293293

294294
The project has limited support for describing security requirements of an API.

0 commit comments

Comments
 (0)