Skip to content

Commit 2a98385

Browse files
authored
Merge pull request #3506 from croudet/cleanUp
Minor code clean-up.
2 parents ee02a39 + 100df4a commit 2a98385

18 files changed

+89
-134
lines changed

modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/DefaultParameterExtension.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import com.fasterxml.jackson.databind.ObjectMapper;
66
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
77
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
8+
import com.fasterxml.jackson.databind.introspect.AnnotationMap;
89
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
10+
911
import io.swagger.v3.core.util.Json;
1012
import io.swagger.v3.core.util.ParameterProcessor;
1113
import io.swagger.v3.jaxrs2.ext.AbstractOpenAPIExtension;
@@ -17,7 +19,6 @@
1719

1820
import javax.ws.rs.BeanParam;
1921
import javax.ws.rs.CookieParam;
20-
import javax.ws.rs.FormParam;
2122
import javax.ws.rs.HeaderParam;
2223
import javax.ws.rs.PathParam;
2324
import javax.ws.rs.QueryParam;
@@ -30,11 +31,10 @@
3031
import java.util.Set;
3132

3233
public class DefaultParameterExtension extends AbstractOpenAPIExtension {
33-
private static String QUERY_PARAM = "query";
34-
private static String HEADER_PARAM = "header";
35-
private static String COOKIE_PARAM = "cookie";
36-
private static String PATH_PARAM = "path";
37-
private static String FORM_PARAM = "form";
34+
private static final String QUERY_PARAM = "query";
35+
private static final String HEADER_PARAM = "header";
36+
private static final String COOKIE_PARAM = "cookie";
37+
private static final String PATH_PARAM = "path";
3838

3939
final ObjectMapper mapper = Json.mapper();
4040

@@ -169,17 +169,19 @@ private boolean handleAdditionalAnnotation(List<Parameter> parameters, List<Para
169169
final AnnotatedField field = propDef.getField();
170170
final AnnotatedMethod setter = propDef.getSetter();
171171
final AnnotatedMethod getter = propDef.getGetter();
172-
final List<Annotation> paramAnnotations = new ArrayList<Annotation>();
172+
final List<Annotation> paramAnnotations = new ArrayList<>();
173173
final Iterator<OpenAPIExtension> extensions = OpenAPIExtensions.chain();
174174
Type paramType = null;
175175

176176
// Gather the field's details
177177
if (field != null) {
178178
paramType = field.getType();
179-
180-
for (final Annotation fieldAnnotation : field.annotations()) {
181-
if (!paramAnnotations.contains(fieldAnnotation)) {
182-
paramAnnotations.add(fieldAnnotation);
179+
AnnotationMap annotationMap = field.getAllAnnotations();
180+
if (annotationMap != null) {
181+
for (final Annotation fieldAnnotation : annotationMap.annotations()) {
182+
if (!paramAnnotations.contains(fieldAnnotation)) {
183+
paramAnnotations.add(fieldAnnotation);
184+
}
183185
}
184186
}
185187
}
@@ -191,10 +193,12 @@ private boolean handleAdditionalAnnotation(List<Parameter> parameters, List<Para
191193
// paramType will stay null if there is no parameter
192194
paramType = setter.getParameterType(0);
193195
}
194-
195-
for (final Annotation fieldAnnotation : setter.annotations()) {
196-
if (!paramAnnotations.contains(fieldAnnotation)) {
197-
paramAnnotations.add(fieldAnnotation);
196+
AnnotationMap annotationMap = setter.getAllAnnotations();
197+
if (annotationMap != null) {
198+
for (final Annotation fieldAnnotation : annotationMap.annotations()) {
199+
if (!paramAnnotations.contains(fieldAnnotation)) {
200+
paramAnnotations.add(fieldAnnotation);
201+
}
198202
}
199203
}
200204
}
@@ -205,10 +209,12 @@ private boolean handleAdditionalAnnotation(List<Parameter> parameters, List<Para
205209
if (paramType == null) {
206210
paramType = getter.getType();
207211
}
208-
209-
for (final Annotation fieldAnnotation : getter.annotations()) {
210-
if (!paramAnnotations.contains(fieldAnnotation)) {
211-
paramAnnotations.add(fieldAnnotation);
212+
AnnotationMap annotationMap = getter.getAllAnnotations();
213+
if (annotationMap != null) {
214+
for (final Annotation fieldAnnotation : annotationMap.annotations()) {
215+
if (!paramAnnotations.contains(fieldAnnotation)) {
216+
paramAnnotations.add(fieldAnnotation);
217+
}
212218
}
213219
}
214220
}

modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/OperationParser.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ public static Optional<RequestBody> getRequestBody(io.swagger.v3.oas.annotations
4141
if (requestBody.extensions().length > 0) {
4242
Map<String, Object> extensions = AnnotationsUtils.getExtensions(requestBody.extensions());
4343
if (extensions != null) {
44-
for (String ext : extensions.keySet()) {
45-
requestBodyObject.addExtension(ext, extensions.get(ext));
46-
}
44+
extensions.forEach(requestBodyObject::addExtension);
4745
}
4846
isEmpty = false;
4947
}
@@ -82,9 +80,7 @@ public static Optional<ApiResponses> getApiResponses(final io.swagger.v3.oas.ann
8280
if (response.extensions().length > 0) {
8381
Map<String, Object> extensions = AnnotationsUtils.getExtensions(response.extensions());
8482
if (extensions != null) {
85-
for (String ext : extensions.keySet()) {
86-
apiResponseObject.addExtension(ext, extensions.get(ext));
87-
}
83+
extensions.forEach(apiResponseObject::addExtension);
8884
}
8985
}
9086

modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/Reader.java

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import java.util.ArrayList;
5858
import java.util.Arrays;
5959
import java.util.Collections;
60-
import java.util.Comparator;
6160
import java.util.HashMap;
6261
import java.util.HashSet;
6362
import java.util.Iterator;
@@ -130,9 +129,7 @@ public OpenAPI read(Class<?> cls) {
130129
* @return the generated OpenAPI definition
131130
*/
132131
public OpenAPI read(Set<Class<?>> classes) {
133-
Set<Class<?>> sortedClasses = new TreeSet<>(new Comparator<Class<?>>() {
134-
@Override
135-
public int compare(Class<?> class1, Class<?> class2) {
132+
Set<Class<?>> sortedClasses = new TreeSet<>((class1, class2) -> {
136133
if (class1.equals(class2)) {
137134
return 0;
138135
} else if (class1.isAssignableFrom(class2)) {
@@ -141,8 +138,7 @@ public int compare(Class<?> class1, Class<?> class2) {
141138
return 1;
142139
}
143140
return class1.getName().compareTo(class2.getName());
144-
}
145-
});
141+
});
146142
sortedClasses.addAll(classes);
147143

148144
Map<Class<?>, ReaderListener> listeners = new HashMap<>();
@@ -192,6 +188,7 @@ public void setConfiguration(OpenAPIConfiguration openApiConfiguration) {
192188
}
193189
}
194190

191+
@Override
195192
public OpenAPI read(Set<Class<?>> classes, Map<String, Object> resources) {
196193
return read(classes);
197194
}
@@ -213,9 +210,9 @@ protected String resolveApplicationPath() {
213210
// look for inner application, e.g. ResourceConfig
214211
try {
215212
Application innerApp = application;
216-
Method m = application.getClass().getMethod("getApplication", null);
213+
Method m = application.getClass().getMethod("getApplication");
217214
while (m != null) {
218-
Application retrievedApp = (Application) m.invoke(innerApp, null);
215+
Application retrievedApp = (Application) m.invoke(innerApp);
219216
if (retrievedApp == null) {
220217
break;
221218
}
@@ -229,7 +226,7 @@ protected String resolveApplicationPath() {
229226
return applicationPath.value();
230227
}
231228
}
232-
m = innerApp.getClass().getMethod("getApplication", null);
229+
m = innerApp.getClass().getMethod("getApplication");
233230
}
234231
} catch (NoSuchMethodException e) {
235232
// no inner application found
@@ -340,8 +337,8 @@ public OpenAPI read(Class<?> cls,
340337
.getTags(apiTags, false).ifPresent(tags ->
341338
tags
342339
.stream()
343-
.map(t -> t.getName())
344-
.forEach(t -> classTags.add(t))
340+
.map(Tag::getName)
341+
.forEach(classTags::add)
345342
);
346343
}
347344

@@ -355,7 +352,7 @@ public OpenAPI read(Class<?> cls,
355352
// servers
356353
final List<io.swagger.v3.oas.models.servers.Server> classServers = new ArrayList<>();
357354
if (apiServers != null) {
358-
AnnotationsUtils.getServers(apiServers).ifPresent(servers -> classServers.addAll(servers));
355+
AnnotationsUtils.getServers(apiServers).ifPresent(classServers::addAll);
359356
}
360357

361358
// class external docs
@@ -374,7 +371,7 @@ public OpenAPI read(Class<?> cls,
374371
globalParameters.addAll(ReaderUtils.collectFieldParameters(cls, components, classConsumes, null));
375372

376373
// iterate class methods
377-
Method methods[] = cls.getMethods();
374+
Method[] methods = cls.getMethods();
378375
for (Method method : methods) {
379376
if (isOperationHidden(method)) {
380377
continue;
@@ -529,7 +526,7 @@ public OpenAPI read(Class<?> cls,
529526
}
530527
}
531528
// if we have form parameters, need to merge them into single schema and use as request body..
532-
if (formParameters.size() > 0) {
529+
if (!formParameters.isEmpty()) {
533530
Schema mergedSchema = new ObjectSchema();
534531
for (Parameter formParam: formParameters) {
535532
mergedSchema.addProperties(formParam.getName(), formParam.getSchema());
@@ -549,7 +546,7 @@ public OpenAPI read(Class<?> cls,
549546
jsonViewAnnotationForRequestBody);
550547

551548
}
552-
if (operationParameters.size() > 0) {
549+
if (!operationParameters.isEmpty()) {
553550
for (Parameter operationParameter : operationParameters) {
554551
operation.addParametersItem(operationParameter);
555552
}
@@ -908,7 +905,7 @@ protected Operation parseMethod(
908905
if (apiTags != null) {
909906
apiTags.stream()
910907
.filter(t -> operation.getTags() == null || (operation.getTags() != null && !operation.getTags().contains(t.name())))
911-
.map(t -> t.name())
908+
.map(io.swagger.v3.oas.annotations.tags.Tag::name)
912909
.forEach(operation::addTagsItem);
913910
AnnotationsUtils.getTags(apiTags.toArray(new io.swagger.v3.oas.annotations.tags.Tag[apiTags.size()]), true).ifPresent(tags -> openApiTags.addAll(tags));
914911
}
@@ -961,7 +958,7 @@ protected Operation parseMethod(
961958
}
962959

963960
// apiResponses
964-
if (apiResponses != null && apiResponses.size() > 0) {
961+
if (apiResponses != null && !apiResponses.isEmpty()) {
965962
OperationParser.getApiResponses(
966963
apiResponses.toArray(new io.swagger.v3.oas.annotations.responses.ApiResponse[apiResponses.size()]),
967964
classProduces,
@@ -1074,7 +1071,7 @@ private boolean shouldIgnoreClass(String className) {
10741071
rawClassName = className.replace("[simple type, class ", "");
10751072
rawClassName = rawClassName.substring(0, rawClassName.length() -1);
10761073
}
1077-
ignore = ignore || rawClassName.startsWith("javax.ws.rs.");
1074+
ignore = rawClassName.startsWith("javax.ws.rs.");
10781075
ignore = ignore || rawClassName.equalsIgnoreCase("void");
10791076
ignore = ignore || ModelConverters.getInstance().isRegisteredAsSkippedClass(rawClassName);
10801077
return ignore;
@@ -1171,11 +1168,10 @@ private void setOperationObjectFromApiOperationAnnotation(
11711168
operation.setDeprecated(apiOperation.deprecated());
11721169
}
11731170

1174-
ReaderUtils.getStringListFromStringArray(apiOperation.tags()).ifPresent(tags -> {
1171+
ReaderUtils.getStringListFromStringArray(apiOperation.tags()).ifPresent(tags ->
11751172
tags.stream()
11761173
.filter(t -> operation.getTags() == null || (operation.getTags() != null && !operation.getTags().contains(t)))
1177-
.forEach(operation::addTagsItem);
1178-
});
1174+
.forEach(operation::addTagsItem));
11791175

11801176
if (operation.getExternalDocs() == null) { // if not set in root annotation
11811177
AnnotationsUtils.getExternalDocumentation(apiOperation.externalDocs()).ifPresent(operation::setExternalDocs);
@@ -1206,18 +1202,16 @@ private void setOperationObjectFromApiOperationAnnotation(
12061202
}
12071203

12081204
// RequestBody in Operation
1209-
if (apiOperation != null && apiOperation.requestBody() != null && operation.getRequestBody() == null) {
1205+
if (apiOperation.requestBody() != null && operation.getRequestBody() == null) {
12101206
OperationParser.getRequestBody(apiOperation.requestBody(), classConsumes, methodConsumes, components, jsonViewAnnotation).ifPresent(
1211-
requestBodyObject -> operation.setRequestBody(requestBodyObject));
1207+
operation::setRequestBody);
12121208
}
12131209

12141210
// Extensions in Operation
12151211
if (apiOperation.extensions().length > 0) {
12161212
Map<String, Object> extensions = AnnotationsUtils.getExtensions(apiOperation.extensions());
12171213
if (extensions != null) {
1218-
for (String ext : extensions.keySet()) {
1219-
operation.addExtension(ext, extensions.get(ext));
1220-
}
1214+
extensions.forEach(operation::addExtension);
12211215
}
12221216
}
12231217
}
@@ -1262,7 +1256,7 @@ protected Optional<List<Parameter>> getParametersListFromAnnotation(io.swagger.v
12621256
ResolvedParameter resolvedParameter = getParameters(ParameterProcessor.getParameterType(parameter), Collections.singletonList(parameter), operation, classConsumes, methodConsumes, jsonViewAnnotation);
12631257
parametersObject.addAll(resolvedParameter.parameters);
12641258
}
1265-
if (parametersObject.size() == 0) {
1259+
if (parametersObject.isEmpty()) {
12661260
return Optional.empty();
12671261
}
12681262
return Optional.of(parametersObject);
@@ -1279,8 +1273,7 @@ protected ResolvedParameter getParameters(Type type, List<Annotation> annotation
12791273
final OpenAPIExtension extension = chain.next();
12801274
LOGGER.debug("trying extension {}", extension);
12811275

1282-
final ResolvedParameter extractParametersResult = extension.extractParameters(annotations, type, typesToSkip, components, classConsumes, methodConsumes, true, jsonViewAnnotation, chain);
1283-
return extractParametersResult;
1276+
return extension.extractParameters(annotations, type, typesToSkip, components, classConsumes, methodConsumes, true, jsonViewAnnotation, chain);
12841277
}
12851278

12861279
private Set<String> extractOperationIdFromPathItem(PathItem path) {
@@ -1395,10 +1388,7 @@ protected boolean ignoreOperationPath(String path, String parentPath) {
13951388
path = path.substring(0, path.length() - 1);
13961389
}
13971390
}
1398-
if (path.equals(parentPath)) {
1399-
return true;
1400-
}
1401-
return false;
1391+
return path.equals(parentPath);
14021392
}
14031393

14041394
protected Class<?> getSubResourceWithJaxRsSubresourceLocatorSpecs(Method method) {

modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/SecurityParser.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ public static Optional<SecuritySchemePair> getSecurityScheme(io.swagger.v3.oas.a
8585
if (securityScheme.extensions().length > 0) {
8686
Map<String, Object> extensions = AnnotationsUtils.getExtensions(securityScheme.extensions());
8787
if (extensions != null) {
88-
for (String ext : extensions.keySet()) {
89-
securitySchemeObject.addExtension(ext, extensions.get(ext));
90-
}
88+
extensions.forEach(securitySchemeObject::addExtension);
9189
}
9290
}
9391

@@ -107,9 +105,7 @@ public static Optional<OAuthFlows> getOAuthFlows(io.swagger.v3.oas.annotations.s
107105
if (oAuthFlows.extensions().length > 0) {
108106
Map<String, Object> extensions = AnnotationsUtils.getExtensions(oAuthFlows.extensions());
109107
if (extensions != null) {
110-
for (String ext : extensions.keySet()) {
111-
oAuthFlowsObject.addExtension(ext, extensions.get(ext));
112-
}
108+
extensions.forEach(oAuthFlowsObject::addExtension);
113109
}
114110
}
115111

@@ -137,9 +133,7 @@ public static Optional<OAuthFlow> getOAuthFlow(io.swagger.v3.oas.annotations.sec
137133
if (oAuthFlow.extensions().length > 0) {
138134
Map<String, Object> extensions = AnnotationsUtils.getExtensions(oAuthFlow.extensions());
139135
if (extensions != null) {
140-
for (String ext : extensions.keySet()) {
141-
oAuthFlowObject.addExtension(ext, extensions.get(ext));
142-
}
136+
extensions.forEach(oAuthFlowObject::addExtension);
143137
}
144138
}
145139

@@ -183,10 +177,7 @@ private static boolean isEmpty(io.swagger.v3.oas.annotations.security.OAuthFlows
183177
if (!isEmpty(oAuthFlows.password())) {
184178
return false;
185179
}
186-
if (oAuthFlows.extensions().length > 0) {
187-
return false;
188-
}
189-
return true;
180+
return oAuthFlows.extensions().length == 0;
190181
}
191182

192183
private static boolean isEmpty(io.swagger.v3.oas.annotations.security.OAuthFlow oAuthFlow) {
@@ -205,17 +196,11 @@ private static boolean isEmpty(io.swagger.v3.oas.annotations.security.OAuthFlow
205196
if (!isEmpty(oAuthFlow.scopes())) {
206197
return false;
207198
}
208-
if (oAuthFlow.extensions().length > 0) {
209-
return false;
210-
}
211-
return true;
199+
return oAuthFlow.extensions().length == 0;
212200
}
213201

214202
private static boolean isEmpty(OAuthScope[] scopes) {
215-
if (scopes == null || scopes.length == 0) {
216-
return true;
217-
}
218-
return false;
203+
return scopes == null || scopes.length == 0;
219204
}
220205

221206
}

modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/SwaggerSerializers.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public boolean isWriteable(Class type, Type genericType, Annotation[] annotation
3131

3232
@Override
3333
public long getSize(OpenAPI data, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
34-
return -1;
34+
return -1L;
3535
}
3636

3737
@Override
@@ -58,7 +58,7 @@ public void writeTo(OpenAPI data,
5858
}
5959
} else if (mediaType.isCompatible(MediaType.APPLICATION_XML_TYPE)) {
6060
headers.remove("Content-Type");
61-
headers.add("Content-Type", "application/json");
61+
headers.add("Content-Type", MediaType.APPLICATION_JSON);
6262
if (prettyPrint) {
6363
out.write(Json.pretty().writeValueAsBytes(data));
6464
} else {

0 commit comments

Comments
 (0)