This repository has been archived by the owner on Oct 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
RamlDocumentation.java
178 lines (152 loc) · 10.7 KB
/
RamlDocumentation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.epages.restdocs.raml;
import static com.epages.restdocs.raml.RamlResourceDocumentation.ramlResource;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import org.springframework.restdocs.headers.HeaderDescriptor;
import org.springframework.restdocs.headers.RequestHeadersSnippet;
import org.springframework.restdocs.headers.ResponseHeadersSnippet;
import org.springframework.restdocs.hypermedia.LinkDescriptor;
import org.springframework.restdocs.hypermedia.LinksSnippet;
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation;
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
import org.springframework.restdocs.operation.preprocess.OperationRequestPreprocessor;
import org.springframework.restdocs.operation.preprocess.OperationResponsePreprocessor;
import org.springframework.restdocs.payload.FieldDescriptor;
import org.springframework.restdocs.payload.RequestFieldsSnippet;
import org.springframework.restdocs.payload.ResponseFieldsSnippet;
import org.springframework.restdocs.request.ParameterDescriptor;
import org.springframework.restdocs.request.PathParametersSnippet;
import org.springframework.restdocs.request.RequestParametersSnippet;
import org.springframework.restdocs.snippet.Snippet;
/**
* Convenience class to migrate to restdocs-raml in a non-invasive way.
* It it a wrapper and replacement for MockMvcRestDocumentation that transparently adds a RamlResourceSnippet with the descriptors provided in the given snippets.
*/
public class RamlDocumentation {
public static RestDocumentationResultHandler document(String identifier,
Snippet... snippets) {
return document(identifier, "", false, null, null, Function.identity(), snippets);
}
public static RestDocumentationResultHandler document(String identifier,
OperationRequestPreprocessor requestPreprocessor,
Snippet... snippets) {
return document(identifier, "", false, requestPreprocessor, null, Function.identity(), snippets);
}
public static RestDocumentationResultHandler document(String identifier,
OperationResponsePreprocessor responsePreprocessor,
Snippet... snippets) {
return document(identifier, "", false, null, responsePreprocessor, Function.identity(), snippets);
}
public static RestDocumentationResultHandler document(String identifier,
OperationRequestPreprocessor requestPreprocessor,
OperationResponsePreprocessor responsePreprocessor,
Snippet... snippets) {
return document(identifier, "", false, requestPreprocessor, responsePreprocessor, Function.identity(), snippets);
}
public static RestDocumentationResultHandler document(String identifier,
boolean privateResource,
OperationRequestPreprocessor requestPreprocessor,
Snippet... snippets) {
return document(identifier, "", privateResource, requestPreprocessor, null, Function.identity(), snippets);
}
public static RestDocumentationResultHandler document(String identifier,
boolean privateResource,
OperationResponsePreprocessor responsePreprocessor,
Snippet... snippets) {
return document(identifier, "", privateResource, null, responsePreprocessor, Function.identity(), snippets);
}
public static RestDocumentationResultHandler document(String identifier,
String description,
boolean privateResource,
Snippet... snippets) {
return document(identifier, description, privateResource, null, null, Function.identity(), snippets);
}
public static RestDocumentationResultHandler document(String identifier,
String description,
boolean privateResource,
OperationRequestPreprocessor requestPreprocessor,
OperationResponsePreprocessor responsePreprocessor,
Snippet... snippets) {
return document(identifier, description, privateResource, requestPreprocessor, responsePreprocessor, Function.identity(), snippets);
}
public static RestDocumentationResultHandler document(String identifier,
String description,
boolean privateResource,
OperationRequestPreprocessor requestPreprocessor,
OperationResponsePreprocessor responsePreprocessor,
Function<List<Snippet>, List<Snippet>> snippetFilter,
Snippet... snippets) {
Snippet[] enhancedSnippets = enhanceSnippetsWithRaml(description, privateResource, snippetFilter, snippets);
if (requestPreprocessor != null && responsePreprocessor != null) {
return MockMvcRestDocumentation.document(identifier, requestPreprocessor, responsePreprocessor, enhancedSnippets);
} else if (requestPreprocessor != null) {
return MockMvcRestDocumentation.document(identifier, requestPreprocessor, enhancedSnippets);
} else if (responsePreprocessor != null) {
return MockMvcRestDocumentation.document(identifier, responsePreprocessor, enhancedSnippets);
}
return MockMvcRestDocumentation.document(identifier, enhancedSnippets);
}
protected static Snippet[] enhanceSnippetsWithRaml(String description,
boolean privateResource,
Function<List<Snippet>, List<Snippet>> snippetFilter,
Snippet... snippets) {
List<RequestFieldsSnippet> requestFieldsSnippets = new ArrayList<>();
List<ResponseFieldsSnippet> responseFieldsSnippets = new ArrayList<>();
List<LinksSnippet> linkSnippets = new ArrayList<>();
List<RequestParametersSnippet> requestParametersSnippets = new ArrayList<>();
List<PathParametersSnippet> pathParametersSnippets = new ArrayList<>();
List<RequestHeadersSnippet> requestHeadersSnippets = new ArrayList<>();
List<ResponseHeadersSnippet> responseHeadersSnippets = new ArrayList<>();
List<Snippet> ramlSnippets = new ArrayList<>();
List<Snippet> otherSnippets = new ArrayList<>();
for (Snippet snippet : snippets) {
if (snippet instanceof RequestFieldsSnippet) {
requestFieldsSnippets.add((RequestFieldsSnippet) snippet);
} else if (snippet instanceof ResponseFieldsSnippet) {
responseFieldsSnippets.add((ResponseFieldsSnippet) snippet);
} else if (snippet instanceof LinksSnippet) {
linkSnippets.add((LinksSnippet) snippet);
} else if (snippet instanceof RequestParametersSnippet) {
requestParametersSnippets.add((RequestParametersSnippet) snippet);
} else if (snippet instanceof PathParametersSnippet) {
pathParametersSnippets.add((PathParametersSnippet) snippet);
} else if (snippet instanceof RequestHeadersSnippet) {
requestHeadersSnippets.add((RequestHeadersSnippet) snippet);
} else if (snippet instanceof ResponseHeadersSnippet) {
responseHeadersSnippets.add((ResponseHeadersSnippet) snippet);
} else if (snippet instanceof RamlResourceSnippet) {
ramlSnippets.add(snippet);
} else {
otherSnippets.add(snippet);
}
}
List<Snippet> enhancedSnippets = new ArrayList<>();
enhancedSnippets.addAll(requestFieldsSnippets);
enhancedSnippets.addAll(responseFieldsSnippets);
enhancedSnippets.addAll(linkSnippets);
enhancedSnippets.addAll(requestParametersSnippets);
enhancedSnippets.addAll(pathParametersSnippets);
enhancedSnippets.addAll(requestHeadersSnippets);
enhancedSnippets.addAll(responseHeadersSnippets);
enhancedSnippets.addAll(otherSnippets);
if (ramlSnippets.isEmpty()) { // No RamlResourceSnippet, so we configure our own based on the info of the other snippets
RamlResourceSnippetParameters ramlParameters = RamlResourceSnippetParameters.builder()
.description(description)
.privateResource(privateResource)
.requestFields(requestFieldsSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(FieldDescriptor[]::new))
.responseFields(responseFieldsSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(FieldDescriptor[]::new))
.links(linkSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(LinkDescriptor[]::new))
.requestParameters(requestParametersSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(ParameterDescriptor[]::new))
.pathParameters(pathParametersSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(ParameterDescriptor[]::new))
.requestHeaders(requestHeadersSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(HeaderDescriptor[]::new))
.responseHeaders(responseHeadersSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(HeaderDescriptor[]::new))
.build();
enhancedSnippets.add(ramlResource(ramlParameters));
} else {
enhancedSnippets.addAll(ramlSnippets);
}
enhancedSnippets = snippetFilter.apply(enhancedSnippets);
return enhancedSnippets.toArray(new Snippet[0]);
}
}