-
Notifications
You must be signed in to change notification settings - Fork 737
Description
I understand that this project was partially created out of the authors frustration with tools like Swagger and annotation processing. I also like the test-driven approach to producing documentation.
I'd like to propose an enhancement to allow for a limited amount of annotation processing. Specifically, for the Bean Validation API and Hibernate Validator annotations.
When documenting an API using the "test-driven" approach I think it's potentially tricky to try and document what the request body should look like and what the constraints on each field are.
One can, and should, write tests that verify that the app does reject invalid values for each field, e.g. invalid email format, missing required fields, too long/short data etc... It would also be a good idea to test an example of a "good" request (and therefore automatically have it documented). However, when producing documentation, showing people all the possible ways a request could be wrong doesn't sound like the best way. It would be nicer to show an example of a "good" request and then explain what the basic validation rules are (clearly there will often be validation which isn't expressed using the bean validation API and these may warrant explicit hand written documentation).
The problem with writing this sort of information into the asciidoc file directly, is that as soon as any validation rules change the docs are immediately out-of-date (DRY/SSOT). This seems a shame given that often a lot of the basic rules are easily expressed declaratively with the bean validation annotations.
I don't necessarily know what the "best" way of doing this would be. But to prevent having to discover which Objects need to be parsed the feature could require the documentation code to pass the class object (TestRequest.class in the example below) to some kind of processor in spring-restdoc. It could produce an asciidoc file in the snippets output directory that contained a table describing the rules.
Example
Controller Method
@RequestMapping(value = ... , method = ..., consumes = ...)
public ResponseEntity<TestResult> test(@RequestBody TestRequest requestBody) {
...
}
Request Body
public class TestRequest {
@NotBlank
@Length(min = 1, max = 2_000)
private String field1;
@Email
@NotBlank
@Length(max = 254)
private String field2;
@Length(min = 5, max = 20)
private String field3;
@NotNull
private Boolean field4;
// constructors, getters, setters etc...
}
Sample Output
|===
|Field |Mandatory |Validation Rules
|field1
|Yes
|Length 1 - 2,000
|field2
|Yes
|Valid email format, max length 254
|field3
|No
|Length 5 - 20
|field4
|Yes
|true/false
|===
Such a snippet could then be included in the documentation as usual include::{generated}/request-body.asciidoc[]
I don't suggest processing the controller to try and "auto-discover" the correct endpoint, supported media types, HTTP methods, response codes or which objects form the request body and so on.