Skip to content

Commit 6e71dde

Browse files
author
springdoc
committed
project review
1 parent 802045e commit 6e71dde

File tree

11 files changed

+24
-88
lines changed

11 files changed

+24
-88
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/AbstractParameterBuilder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,7 @@ private void setExamples(io.swagger.v3.oas.annotations.Parameter parameterDoc, P
238238
private void setExtensions(io.swagger.v3.oas.annotations.Parameter parameterDoc, Parameter parameter) {
239239
if (parameterDoc.extensions().length > 0) {
240240
Map<String, Object> extensionMap = AnnotationsUtils.getExtensions(parameterDoc.extensions());
241-
for (Map.Entry<String, Object> entry : extensionMap.entrySet()) {
242-
parameter.addExtension(entry.getKey(), entry.getValue());
243-
}
241+
extensionMap.entrySet().forEach(entry -> parameter.addExtension(entry.getKey(), entry.getValue()));
244242
}
245243
}
246244

springdoc-openapi-common/src/main/java/org/springdoc/core/AbstractResponseBuilder.java

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.lang.reflect.ParameterizedType;
2727
import java.lang.reflect.Type;
2828
import java.util.*;
29-
import java.util.Map.Entry;
3029
import java.util.stream.Collectors;
3130
import java.util.stream.Stream;
3231

@@ -46,24 +45,24 @@ protected AbstractResponseBuilder(OperationBuilder operationBuilder) {
4645

4746
public ApiResponses build(Components components, HandlerMethod handlerMethod, Operation operation,
4847
MethodAttributes methodAttributes) {
49-
ApiResponses apiResponses = operation.getResponses();
50-
if (apiResponses == null)
51-
apiResponses = new ApiResponses();
52-
48+
final ApiResponses apiResponses = getApiResponses(operation);
5349
// for each one build ApiResponse and add it to existing responses
54-
for (Entry<String, ApiResponse> entry : genericMapResponse.entrySet()) {
55-
apiResponses.addApiResponse(entry.getKey(), entry.getValue());
56-
}
50+
genericMapResponse.entrySet().forEach(entry -> apiResponses.addApiResponse(entry.getKey(), entry.getValue()));
5751
// Fill api Responses
5852
computeResponse(components, handlerMethod.getMethod(), apiResponses, methodAttributes, false);
53+
return apiResponses;
54+
}
5955

56+
private ApiResponses getApiResponses(Operation operation) {
57+
ApiResponses apiResponses = operation.getResponses();
58+
if (apiResponses == null)
59+
apiResponses = new ApiResponses();
6060
return apiResponses;
6161
}
6262

6363
public void buildGenericResponse(Components components, Map<String, Object> findControllerAdvice) {
6464
// ControllerAdvice
6565
List<Method> methods = getMethods(findControllerAdvice);
66-
6766
// for each one build ApiResponse and add it to existing responses
6867
for (Method method : methods) {
6968
if (!operationBuilder.isHidden(method)) {
@@ -74,9 +73,7 @@ public void buildGenericResponse(Components components, Map<String, Object> find
7473
}
7574
Map<String, ApiResponse> apiResponses = computeResponse(components, method, new ApiResponses(),
7675
new MethodAttributes(methodProduces), true);
77-
for (Map.Entry<String, ApiResponse> entry : apiResponses.entrySet()) {
78-
genericMapResponse.put(entry.getKey(), entry.getValue());
79-
}
76+
apiResponses.entrySet().forEach(entry -> genericMapResponse.put(entry.getKey(), entry.getValue()));
8077
}
8178
}
8279
}
@@ -106,11 +103,7 @@ private List<Method> getMethods(Map<String, Object> findControllerAdvice) {
106103
if (org.springframework.aop.support.AopUtils.isAopProxy(controllerAdvice)) {
107104
objClz = org.springframework.aop.support.AopUtils.getTargetClass(controllerAdvice);
108105
}
109-
for (Method m : objClz.getDeclaredMethods()) {
110-
if (m.isAnnotationPresent(ExceptionHandler.class)) {
111-
methods.add(m);
112-
}
113-
}
106+
Arrays.stream(objClz.getDeclaredMethods()).filter(m -> m.isAnnotationPresent(ExceptionHandler.class)).forEach(m -> methods.add(m));
114107
}
115108
return methods;
116109
}
@@ -128,18 +121,15 @@ private Map<String, ApiResponse> computeResponse(Components components, Method m
128121
apiResponsesOp.addApiResponse(apiResponseAnnotations.responseCode(), apiResponse);
129122
continue;
130123
}
131-
132124
apiResponse.setDescription(apiResponseAnnotations.description());
133125
io.swagger.v3.oas.annotations.media.Content[] contentdoc = apiResponseAnnotations.content();
134126
buildContentFromDoc(components, apiResponsesOp, methodAttributes,
135127
apiResponseAnnotations, apiResponse, contentdoc);
136-
137128
AnnotationsUtils.getHeaders(apiResponseAnnotations.headers(), methodAttributes.getJsonViewAnnotation())
138129
.ifPresent(apiResponse::headers);
139130
apiResponsesOp.addApiResponse(apiResponseAnnotations.responseCode(), apiResponse);
140131
}
141132
}
142-
143133
buildApiResponses(components, method, apiResponsesOp, methodAttributes, isGeneric);
144134
return apiResponsesOp;
145135
}
@@ -150,18 +140,12 @@ private void buildContentFromDoc(Components components, ApiResponses apiResponse
150140
io.swagger.v3.oas.annotations.media.Content[] contentdoc) {
151141
Optional<Content> optionalContent = SpringDocAnnotationsUtils.getContent(contentdoc, new String[0],
152142
methodAttributes.getAllProduces(), null, components, methodAttributes.getJsonViewAnnotation());
153-
154143
if (apiResponsesOp.containsKey(apiResponseAnnotations.responseCode())) {
155144
// Merge with the existing content
156145
Content existingContent = apiResponsesOp.get(apiResponseAnnotations.responseCode()).getContent();
157146
if (optionalContent.isPresent() && existingContent != null) {
158147
Content newContent = optionalContent.get();
159-
for (String mediaTypeStr : methodAttributes.getAllProduces()) {
160-
io.swagger.v3.oas.models.media.MediaType mediaType = newContent.get(mediaTypeStr);
161-
if (mediaType != null) {
162-
mergeSchema(existingContent, mediaType.getSchema(), mediaTypeStr);
163-
}
164-
}
148+
Arrays.stream(methodAttributes.getAllProduces()).filter(mediaTypeStr -> (newContent.get(mediaTypeStr) != null)).forEach(mediaTypeStr -> mergeSchema(existingContent, newContent.get(mediaTypeStr).getSchema(), mediaTypeStr));
165149
apiResponse.content(existingContent);
166150
}
167151
} else {
@@ -256,9 +240,7 @@ private Schema<?> calculateSchema(Components components, Type returnType, JsonVi
256240

257241
private void setContent(String[] methodProduces, Content content,
258242
io.swagger.v3.oas.models.media.MediaType mediaType) {
259-
for (String mediaTypeStr : methodProduces) {
260-
content.addMediaType(mediaTypeStr, mediaType);
261-
}
243+
Arrays.stream(methodProduces).forEach(mediaTypeStr -> content.addMediaType(mediaTypeStr, mediaType));
262244
}
263245

264246
private Schema calculateSchema(Components components, ParameterizedType parameterizedType, JsonView jsonView) {
@@ -287,7 +269,6 @@ private void buildApiResponses(Components components, Method method, ApiResponse
287269
apiResponse.setDescription(DEFAULT_DESCRIPTION);
288270
}
289271
}
290-
291272
if (apiResponse.getContent() != null
292273
&& ((isGeneric || methodAttributes.isMethodOverloaded()) && methodAttributes.isNoApiResponseDoc())) {
293274
// Merge with existing schema
@@ -298,7 +279,6 @@ private void buildApiResponses(Components components, Method method, ApiResponse
298279
Arrays.stream(methodAttributes.getAllProduces()).forEach(mediaTypeStr -> mergeSchema(existingContent, schemaN, mediaTypeStr));
299280
}
300281
}
301-
302282
apiResponsesOp.addApiResponse(httpCode, apiResponse);
303283
}
304284

springdoc-openapi-common/src/main/java/org/springdoc/core/MethodAttributes.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ public class MethodAttributes {
1919
private JsonView jsonViewAnnotation;
2020
private JsonView jsonViewAnnotationForRequestBody;
2121

22-
public MethodAttributes() {
23-
}
24-
2522
public MethodAttributes(String[] methodProducesNew) {
2623
this.methodProduces = methodProducesNew;
2724
}
2825

26+
public MethodAttributes() { }
27+
2928
public String[] getClassProduces() {
3029
return classProduces;
3130
}
@@ -46,7 +45,6 @@ public String[] getMethodProduces() {
4645
return methodProduces;
4746
}
4847

49-
5048
public String[] getMethodConsumes() {
5149
return methodConsumes;
5250
}
@@ -58,30 +56,25 @@ public void calculateConsumesProduces(Method method) {
5856
fillMethods(reqPostMappringMethod.produces(), reqPostMappringMethod.consumes());
5957
return;
6058
}
61-
6259
GetMapping reqGetMappringMethod = ReflectionUtils.getAnnotation(method, GetMapping.class);
6360
if (reqGetMappringMethod != null) {
6461
fillMethods(reqGetMappringMethod.produces(), reqGetMappringMethod.consumes());
6562
return;
6663
}
67-
6864
DeleteMapping reqDeleteMappringMethod = ReflectionUtils.getAnnotation(method, DeleteMapping.class);
6965
if (reqDeleteMappringMethod != null) {
7066
fillMethods(reqDeleteMappringMethod.produces(), reqDeleteMappringMethod.consumes());
7167
return;
7268
}
73-
7469
PutMapping reqPutMappringMethod = ReflectionUtils.getAnnotation(method, PutMapping.class);
7570
if (reqPutMappringMethod != null) {
7671
fillMethods(reqPutMappringMethod.produces(), reqPutMappringMethod.consumes());
7772
return;
7873
}
79-
8074
RequestMapping reqMappringMethod = ReflectionUtils.getAnnotation(method, RequestMapping.class);
8175
if (reqMappringMethod != null) {
8276
fillMethods(reqMappringMethod.produces(), reqMappringMethod.consumes());
8377
}
84-
8578
}
8679

8780
private void fillMethods(String[] produces, String[] consumes) {
@@ -130,5 +123,4 @@ public JsonView getJsonViewAnnotationForRequestBody() {
130123
public void setJsonViewAnnotationForRequestBody(JsonView jsonViewAnnotationForRequestBody) {
131124
this.jsonViewAnnotationForRequestBody = jsonViewAnnotationForRequestBody;
132125
}
133-
134126
}

springdoc-openapi-common/src/main/java/org/springdoc/core/OpenAPIBuilder.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,16 @@ else if (openAPI.getInfo() == null) {
8383
Info infos = new Info().title(DEFAULT_TITLE).version(DEFAULT_VERSION);
8484
openAPI.setInfo(infos);
8585
}
86-
8786
// default server value
8887
if (CollectionUtils.isEmpty(openAPI.getServers())) {
8988
Server server = new Server().url(serverBaseUrl).description(DEFAULT_SERVER_DESCRIPTION);
9089
openAPI.addServersItem(server);
9190
}
92-
9391
// add security schemes
9492
this.calculateSecuritySchemes(openAPI.getComponents());
9593
}
9694

9795
public Operation buildTags(HandlerMethod handlerMethod, Operation operation, OpenAPI openAPI) {
98-
9996
// class tags
10097
List<io.swagger.v3.oas.annotations.tags.Tag> classTags = ReflectionUtils
10198
.getRepeatableAnnotations(handlerMethod.getBeanType(), io.swagger.v3.oas.annotations.tags.Tag.class);

springdoc-openapi-common/src/main/java/org/springdoc/core/OperationBuilder.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,7 @@ private void setPathItemOperation(PathItem pathItemObject, String method, Operat
171171
private void buildExtensions(io.swagger.v3.oas.annotations.Operation apiOperation, Operation operation) {
172172
if (apiOperation.extensions().length > 0) {
173173
Map<String, Object> extensions = AnnotationsUtils.getExtensions(apiOperation.extensions());
174-
for (Map.Entry<String, Object> entry : extensions.entrySet()) {
175-
operation.addExtension(entry.getKey(), entry.getValue());
176-
}
174+
extensions.entrySet().forEach(entry -> operation.addExtension(entry.getKey(), entry.getValue()));
177175
}
178176
}
179177

@@ -184,9 +182,7 @@ private void buildTags(io.swagger.v3.oas.annotations.Operation apiOperation, Ope
184182
.filter(t -> operation.getTags() == null
185183
|| (operation.getTags() != null && !operation.getTags().contains(t)))
186184
.collect(Collectors.toList());
187-
for (String tagsItem : tags) {
188-
operation.addTagsItem(tagsItem);
189-
}
185+
tags.forEach(tagsItem -> operation.addTagsItem(tagsItem));
190186
}
191187
}
192188

@@ -329,9 +325,7 @@ private void setExtensions(io.swagger.v3.oas.annotations.responses.ApiResponse r
329325
ApiResponse apiResponseObject) {
330326
if (response.extensions().length > 0) {
331327
Map<String, Object> extensions = AnnotationsUtils.getExtensions(response.extensions());
332-
for (Map.Entry<String, Object> entry : extensions.entrySet()) {
333-
apiResponseObject.addExtension(entry.getKey(), entry.getValue());
334-
}
328+
extensions.entrySet().forEach(entry -> apiResponseObject.addExtension(entry.getKey(), entry.getValue()));
335329
}
336330
}
337331

springdoc-openapi-common/src/main/java/org/springdoc/core/ParameterInfo.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
class ParameterInfo {
66

77
private String pName;
8-
98
private final java.lang.reflect.Parameter parameter;
10-
119
private io.swagger.v3.oas.annotations.Parameter parameterDoc;
12-
1310
private io.swagger.v3.oas.models.parameters.Parameter parameterModel;
14-
1511
private int index;
1612

1713
public ParameterInfo(String pName, Parameter parameter,
@@ -57,5 +53,4 @@ public void setParameterModel(io.swagger.v3.oas.models.parameters.Parameter para
5753
public int getIndex() {
5854
return index;
5955
}
60-
6156
}

springdoc-openapi-common/src/main/java/org/springdoc/core/RequestBodyBuilder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ public Optional<RequestBody> buildRequestBodyFromDoc(
5353
}
5454
if (requestBody.extensions().length > 0) {
5555
Map<String, Object> extensions = AnnotationsUtils.getExtensions(requestBody.extensions());
56-
for (Map.Entry<String, Object> entry : extensions.entrySet()) {
57-
requestBodyObject.addExtension(entry.getKey(), entry.getValue());
58-
}
56+
extensions.entrySet().forEach(entry -> requestBodyObject.addExtension(entry.getKey(), entry.getValue()));
5957
isEmpty = false;
6058
}
6159

springdoc-openapi-common/src/main/java/org/springdoc/core/RequestInfo.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public class RequestInfo {
88
private final ParameterType paramType;
99

1010
public RequestInfo(ParameterType paramType, String value, boolean required, String defaultValue) {
11-
super();
1211
this.value = value;
1312
this.required = required;
1413
this.defaultValue = defaultValue;
@@ -33,13 +32,10 @@ public String type() {
3332

3433
public enum ParameterType {
3534
QUERY_PARAM("query"), HEADER_PARAM("header"), PATH_PARAM("path");
36-
3735
private final String value;
38-
3936
ParameterType(String s) {
4037
value = s;
4138
}
42-
4339
@Override
4440
public String toString() {
4541
return value;

springdoc-openapi-common/src/main/java/org/springdoc/core/SecurityParser.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ public Optional<SecuritySchemePair> getSecurityScheme(
154154

155155
if (securityScheme.extensions().length > 0) {
156156
Map<String, Object> extensions = AnnotationsUtils.getExtensions(securityScheme.extensions());
157-
for (Map.Entry<String, Object> entry : extensions.entrySet()) {
158-
securitySchemeObject.addExtension(entry.getKey(), entry.getValue());
159-
}
157+
extensions.entrySet().forEach(entry -> securitySchemeObject.addExtension(entry.getKey(), entry.getValue()));
160158
}
161159

162160
getOAuthFlows(securityScheme.flows()).ifPresent(securitySchemeObject::setFlows);
@@ -180,11 +178,8 @@ private Optional<OAuthFlows> getOAuthFlows(io.swagger.v3.oas.annotations.securit
180178
OAuthFlows oAuthFlowsObject = new OAuthFlows();
181179
if (oAuthFlows.extensions().length > 0) {
182180
Map<String, Object> extensions = AnnotationsUtils.getExtensions(oAuthFlows.extensions());
183-
for (Map.Entry<String, Object> entry : extensions.entrySet()) {
184-
oAuthFlowsObject.addExtension(entry.getKey(), entry.getValue());
185-
}
181+
extensions.entrySet().forEach(entry-> oAuthFlowsObject.addExtension(entry.getKey(), entry.getValue()));
186182
}
187-
188183
getOAuthFlow(oAuthFlows.authorizationCode()).ifPresent(oAuthFlowsObject::setAuthorizationCode);
189184
getOAuthFlow(oAuthFlows.clientCredentials()).ifPresent(oAuthFlowsObject::setClientCredentials);
190185
getOAuthFlow(oAuthFlows.implicit()).ifPresent(oAuthFlowsObject::setImplicit);
@@ -208,11 +203,8 @@ private Optional<OAuthFlow> getOAuthFlow(io.swagger.v3.oas.annotations.security.
208203
}
209204
if (oAuthFlow.extensions().length > 0) {
210205
Map<String, Object> extensions = AnnotationsUtils.getExtensions(oAuthFlow.extensions());
211-
for (Map.Entry<String, Object> entry : extensions.entrySet()) {
212-
oAuthFlowObject.addExtension(entry.getKey(), entry.getValue());
213-
}
206+
extensions.entrySet().forEach(entry -> oAuthFlowObject.addExtension(entry.getKey(), entry.getValue()));
214207
}
215-
216208
getScopes(oAuthFlow.scopes()).ifPresent(oAuthFlowObject::setScopes);
217209
return Optional.of(oAuthFlowObject);
218210
}
@@ -222,10 +214,7 @@ private Optional<Scopes> getScopes(OAuthScope[] scopes) {
222214
return Optional.empty();
223215
}
224216
Scopes scopesObject = new Scopes();
225-
226-
for (OAuthScope scope : scopes) {
227-
scopesObject.addString(scope.name(), scope.description());
228-
}
217+
Arrays.stream(scopes).forEach(scope -> scopesObject.addString(scope.name(), scope.description()));
229218
return Optional.of(scopesObject);
230219
}
231220

springdoc-openapi-common/src/main/java/org/springdoc/core/SpringDocAnnotationsUtils.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@ private static void addExtension(io.swagger.v3.oas.annotations.media.Content ann
115115
annotationContent.extensions();
116116
if (annotationContent.extensions().length > 0) {
117117
Map<String, Object> extensions = AnnotationsUtils.getExtensions(annotationContent.extensions());
118-
for (Map.Entry<String, Object> entry : extensions.entrySet()) {
119-
mediaType.addExtension(entry.getKey(), entry.getValue());
120-
}
118+
extensions.entrySet().forEach(entry -> mediaType.addExtension(entry.getKey(), entry.getValue()));
121119
}
122120
}
123121

0 commit comments

Comments
 (0)