-
-
Notifications
You must be signed in to change notification settings - Fork 469
/
DataRestRequestBuilder.java
219 lines (202 loc) · 9.23 KB
/
DataRestRequestBuilder.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
*
* *
* * *
* * * * Copyright 2019-2020 the original author or authors.
* * * *
* * * * Licensed under the Apache License, Version 2.0 (the "License");
* * * * you may not use this file except in compliance with the License.
* * * * You may obtain a copy of the License at
* * * *
* * * * https://www.apache.org/licenses/LICENSE-2.0
* * * *
* * * * Unless required by applicable law or agreed to in writing, software
* * * * distributed under the License is distributed on an "AS IS" BASIS,
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * * See the License for the specific language governing permissions and
* * * * limitations under the License.
* * *
* *
*
*
*/
package org.springdoc.data.rest.core;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import org.apache.commons.lang3.ArrayUtils;
import org.springdoc.core.AbstractRequestBuilder;
import org.springdoc.core.DelegatingMethodParameter;
import org.springdoc.core.GenericParameterBuilder;
import org.springdoc.core.MethodAttributes;
import org.springdoc.core.ParameterInfo;
import org.springdoc.core.RequestBodyBuilder;
import org.springdoc.core.RequestBodyInfo;
import org.springdoc.core.SpringDocAnnotationsUtils;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.data.rest.core.mapping.ResourceMetadata;
import org.springframework.data.rest.webmvc.PersistentEntityResource;
import org.springframework.data.rest.webmvc.support.BackendId;
import org.springframework.data.rest.webmvc.support.DefaultedPageable;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.method.HandlerMethod;
/**
* The type Data rest request builder.
* @author bnasslahsen
*/
public class DataRestRequestBuilder {
/**
* The Local spring doc parameter name discoverer.
*/
private LocalVariableTableParameterNameDiscoverer localSpringDocParameterNameDiscoverer;
/**
* The Parameter builder.
*/
private GenericParameterBuilder parameterBuilder;
/**
* The Request body builder.
*/
private RequestBodyBuilder requestBodyBuilder;
/**
* The Request builder.
*/
private AbstractRequestBuilder requestBuilder;
/**
* Instantiates a new Data rest request builder.
*
* @param localSpringDocParameterNameDiscoverer the local spring doc parameter name discoverer
* @param parameterBuilder the parameter builder
* @param requestBodyBuilder the request body builder
* @param requestBuilder the request builder
*/
public DataRestRequestBuilder(LocalVariableTableParameterNameDiscoverer localSpringDocParameterNameDiscoverer, GenericParameterBuilder parameterBuilder,
RequestBodyBuilder requestBodyBuilder, AbstractRequestBuilder requestBuilder) {
this.localSpringDocParameterNameDiscoverer = localSpringDocParameterNameDiscoverer;
this.parameterBuilder = parameterBuilder;
this.requestBodyBuilder = requestBodyBuilder;
this.requestBuilder = requestBuilder;
}
/**
* Build parameters.
*
* @param domainType the domain type
* @param openAPI the open api
* @param handlerMethod the handler method
* @param requestMethod the request method
* @param methodAttributes the method attributes
* @param operation the operation
* @param resourceMetadata the resource metadata
*/
public void buildParameters(Class<?> domainType, OpenAPI openAPI, HandlerMethod handlerMethod, RequestMethod requestMethod, MethodAttributes methodAttributes, Operation operation, ResourceMetadata resourceMetadata) {
String[] pNames = this.localSpringDocParameterNameDiscoverer.getParameterNames(handlerMethod.getMethod());
MethodParameter[] parameters = handlerMethod.getMethodParameters();
if (!resourceMetadata.isPagingResource()) {
Optional<MethodParameter> methodParameterPage = Arrays.stream(parameters).filter(methodParameter -> DefaultedPageable.class.equals(methodParameter.getParameterType())).findFirst();
if (methodParameterPage.isPresent())
parameters = ArrayUtils.removeElement(parameters, methodParameterPage.get());
}
String[] reflectionParametersNames = Arrays.stream(handlerMethod.getMethod().getParameters()).map(java.lang.reflect.Parameter::getName).toArray(String[]::new);
if (pNames == null || Arrays.stream(pNames).anyMatch(Objects::isNull))
pNames = reflectionParametersNames;
buildCommonParameters(domainType, openAPI, requestMethod, methodAttributes, operation, pNames, parameters);
}
/**
* Build common parameters.
*
* @param domainType the domain type
* @param openAPI the open api
* @param requestMethod the request method
* @param methodAttributes the method attributes
* @param operation the operation
* @param pNames the p names
* @param parameters the parameters
*/
public void buildCommonParameters(Class<?> domainType, OpenAPI openAPI, RequestMethod requestMethod, MethodAttributes methodAttributes, Operation operation, String[] pNames, MethodParameter[] parameters) {
parameters = DelegatingMethodParameter.customize(pNames, parameters, parameterBuilder.getDelegatingMethodParameterCustomizer());
for (MethodParameter methodParameter : parameters) {
final String pName = methodParameter.getParameterName();
ParameterInfo parameterInfo = new ParameterInfo(pName, methodParameter);
if (isParamToIgnore(methodParameter)) {
if (PersistentEntityResource.class.equals(methodParameter.getParameterType())) {
Schema<?> schema = SpringDocAnnotationsUtils.resolveSchemaFromType(domainType, openAPI.getComponents(), null, methodParameter.getParameterAnnotations());
parameterInfo.setParameterModel(new Parameter().schema(schema));
}
else if (methodParameter.getParameterAnnotation(BackendId.class) != null) {
parameterInfo.setParameterModel(new Parameter().name("id").in(ParameterIn.PATH.toString()).schema(new StringSchema()));
}
Parameter parameter;
io.swagger.v3.oas.annotations.Parameter parameterDoc = AnnotatedElementUtils.findMergedAnnotation(
AnnotatedElementUtils.forAnnotations(methodParameter.getParameterAnnotations()),
io.swagger.v3.oas.annotations.Parameter.class);
if (parameterDoc != null) {
if (parameterDoc.hidden() || parameterDoc.schema().hidden())
continue;
parameter = parameterBuilder.buildParameterFromDoc(parameterDoc, openAPI.getComponents(), methodAttributes.getJsonViewAnnotation());
parameterInfo.setParameterModel(parameter);
}
parameter = requestBuilder.buildParams(parameterInfo, openAPI.getComponents(), requestMethod, null);
addParameters(openAPI, requestMethod, methodAttributes, operation, methodParameter, parameterInfo, parameter);
}
}
}
/**
* Is param to ignore boolean.
*
* @param methodParameter the method parameter
* @return the boolean
*/
private boolean isParamToIgnore(MethodParameter methodParameter) {
return !requestBuilder.isParamToIgnore(methodParameter)
&& !isHeaderToIgnore(methodParameter)
&& !"property".equals(methodParameter.getParameterName());
}
/**
* Add parameters.
*
* @param openAPI the open api
* @param requestMethod the request method
* @param methodAttributes the method attributes
* @param operation the operation
* @param methodParameter the method parameter
* @param parameterInfo the parameter info
* @param parameter the parameter
*/
private void addParameters(OpenAPI openAPI, RequestMethod requestMethod, MethodAttributes methodAttributes, Operation operation, MethodParameter methodParameter, ParameterInfo parameterInfo, Parameter parameter) {
List<Annotation> parameterAnnotations = Arrays.asList(methodParameter.getParameterAnnotations());
if (requestBuilder.isValidParameter(parameter)) {
requestBuilder.applyBeanValidatorAnnotations(parameter, parameterAnnotations);
operation.addParametersItem(parameter);
}
else if (!RequestMethod.GET.equals(requestMethod)) {
RequestBodyInfo requestBodyInfo = new RequestBodyInfo();
if (operation.getRequestBody() != null)
requestBodyInfo.setRequestBody(operation.getRequestBody());
requestBodyBuilder.calculateRequestBodyInfo(openAPI.getComponents(), methodAttributes,
parameterInfo, requestBodyInfo);
requestBuilder.applyBeanValidatorAnnotations(requestBodyInfo.getRequestBody(), parameterAnnotations, methodParameter.isOptional());
operation.setRequestBody(requestBodyInfo.getRequestBody());
}
}
/**
* Is header to ignore boolean.
*
* @param methodParameter the method parameter
* @return the boolean
*/
private boolean isHeaderToIgnore(MethodParameter methodParameter) {
RequestHeader requestHeader = methodParameter.getParameterAnnotation(RequestHeader.class);
return requestHeader != null && HttpHeaders.ACCEPT.equals(requestHeader.value());
}
}