You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[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)
52
52
-[Security Definitions in OpenAPI](#security-definitions-in-openapi)
53
53
-[Running the gradle plugin](#running-the-gradle-plugin)
54
54
-[OpenAPI 2.0](#openapi-20)
@@ -59,20 +59,16 @@ This is why we came up with this project.
59
59
-[OpenAPI 3.0.1](#openapi-301-1)
60
60
-[Generate an HTML-based API reference from OpenAPI](#generate-an-html-based-api-reference-from-openapi)
61
61
-[RAML](#raml)
62
-
-[Limitations](#limitations)
63
-
-[Rest Assured](#rest-assured)
64
-
65
-
<!-- /TOC -->
66
62
67
63
## Getting started
68
64
69
65
### Project structure
70
66
71
67
The project consists of the following main components:
72
68
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.
76
72
-[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`.
77
73
-[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`.
78
74
-[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
139
135
The root project does not provide a maven plugin.
140
136
But you can find a plugin that works with `restdocs-api-spec` at [BerkleyTechnologyServices/restdocs-spec](https://github.com/BerkleyTechnologyServices/restdocs-spec).
141
137
142
-
### Usage with Spring REST Docs - MockMvc
138
+
### Usage with Spring REST Docs
143
139
144
140
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).
145
141
@@ -151,7 +147,7 @@ mockMvc
151
147
.andDo(document("carts-create", resource("Create a cart")));
152
148
```
153
149
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.
155
151
This file just contains all the information that we can collect about the resource.
156
152
The format of this file is not specific to an API specification.
157
153
@@ -217,50 +213,30 @@ Please see the [CartIntegrationTest](samples/restdocs-api-spec-sample/src/test/j
217
213
218
214
**:warning: Use `template URIs` to refer to path variables in your request**
219
215
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.
222
218
223
219
```java
224
220
mockMvc.perform(get("/carts/{id}", cartId)
225
221
```
226
222
227
-
### Usage with SpringRESTDocs-RESTAssured
228
-
The usage forRESTAssured 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
+
### DocumentingBeanValidation constraints
229
224
230
-
To use the ``RestAssuredRestDocumentationWrapper``, you have to add a dependency to [restdocs-api-spec-restassured](restdocs-api-spec-restassured) to your build.
Similar to the way SpringRESTDocs 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 SpringRESTDocs 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.
253
227
254
228
Currently the following constraints are considered when generating JsonSchema from `FieldDescriptor`s that have been created via `com.epages.restdocs.apispec.ConstrainedFields`
255
229
- `NotNull`, `NotEmpty`, and `NotBlank` annotated fields become required fields in the JsonSchema
256
230
-forString fields annotated with `NotEmpty`, and `NotBlank` the `minLength` constraint in JsonSchema is set to 1
257
231
-forString fields annotated with `Length` the `minLength` and `maxLength` constraints in JsonSchema are set to the value of the corresponding attribute of the annotation
258
232
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.
260
234
Here it is important to add the constraints under the key `validationConstraints` into the attributes map if the `FieldDescriptor`.
261
235
262
236
### Migrate existing SpringRESTDocs tests
263
237
238
+
#### MockMvc based tests
239
+
264
240
For convenience when applying `restdocs-api-spec` to an existing project that uses SpringRESTDocs, we introduced [com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper](restdocs-api-spec/src/main/kotlin/com/epages/restdocs/apispec/MockMvcRestDocumentationWrapper.kt).
265
241
266
242
In your tests you can just replace calls to `MockMvcRestDocumentation.document` with the corresponding variant of `MockMvcRestDocumentationWrapper.document`.
@@ -289,6 +265,30 @@ resultActions
289
265
This will do exactly what `MockMvcRestDocumentation.document` does.
290
266
Additionally it will add a `ResourceSnippet` with the descriptors you provided in the `RequestFieldsSnippet`, `ResponseFieldsSnippet`, and `LinksSnippet`.
291
267
268
+
#### RESTAssured based tests
269
+
270
+
AlsoforRESTAssured we offer a convenience wrapper similar to `MockMvcRestDocumentationWrapper`.
271
+
The usage forRESTAssured 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.
0 commit comments