Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.introspect.AnnotationMap;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;

import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.ParameterProcessor;
import io.swagger.v3.jaxrs2.ext.AbstractOpenAPIExtension;
Expand All @@ -17,7 +19,6 @@

import javax.ws.rs.BeanParam;
import javax.ws.rs.CookieParam;
import javax.ws.rs.FormParam;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
Expand All @@ -30,11 +31,10 @@
import java.util.Set;

public class DefaultParameterExtension extends AbstractOpenAPIExtension {
private static String QUERY_PARAM = "query";
private static String HEADER_PARAM = "header";
private static String COOKIE_PARAM = "cookie";
private static String PATH_PARAM = "path";
private static String FORM_PARAM = "form";
private static final String QUERY_PARAM = "query";
private static final String HEADER_PARAM = "header";
private static final String COOKIE_PARAM = "cookie";
private static final String PATH_PARAM = "path";

final ObjectMapper mapper = Json.mapper();

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

// Gather the field's details
if (field != null) {
paramType = field.getType();

for (final Annotation fieldAnnotation : field.annotations()) {
if (!paramAnnotations.contains(fieldAnnotation)) {
paramAnnotations.add(fieldAnnotation);
AnnotationMap annotationMap = field.getAllAnnotations();
if (annotationMap != null) {
for (final Annotation fieldAnnotation : annotationMap.annotations()) {
if (!paramAnnotations.contains(fieldAnnotation)) {
paramAnnotations.add(fieldAnnotation);
}
}
}
}
Expand All @@ -191,10 +193,12 @@ private boolean handleAdditionalAnnotation(List<Parameter> parameters, List<Para
// paramType will stay null if there is no parameter
paramType = setter.getParameterType(0);
}

for (final Annotation fieldAnnotation : setter.annotations()) {
if (!paramAnnotations.contains(fieldAnnotation)) {
paramAnnotations.add(fieldAnnotation);
AnnotationMap annotationMap = setter.getAllAnnotations();
if (annotationMap != null) {
for (final Annotation fieldAnnotation : annotationMap.annotations()) {
if (!paramAnnotations.contains(fieldAnnotation)) {
paramAnnotations.add(fieldAnnotation);
}
}
}
}
Expand All @@ -205,10 +209,12 @@ private boolean handleAdditionalAnnotation(List<Parameter> parameters, List<Para
if (paramType == null) {
paramType = getter.getType();
}

for (final Annotation fieldAnnotation : getter.annotations()) {
if (!paramAnnotations.contains(fieldAnnotation)) {
paramAnnotations.add(fieldAnnotation);
AnnotationMap annotationMap = getter.getAllAnnotations();
if (annotationMap != null) {
for (final Annotation fieldAnnotation : annotationMap.annotations()) {
if (!paramAnnotations.contains(fieldAnnotation)) {
paramAnnotations.add(fieldAnnotation);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ public static Optional<RequestBody> getRequestBody(io.swagger.v3.oas.annotations
if (requestBody.extensions().length > 0) {
Map<String, Object> extensions = AnnotationsUtils.getExtensions(requestBody.extensions());
if (extensions != null) {
for (String ext : extensions.keySet()) {
requestBodyObject.addExtension(ext, extensions.get(ext));
}
extensions.forEach(requestBodyObject::addExtension);
}
isEmpty = false;
}
Expand Down Expand Up @@ -82,9 +80,7 @@ public static Optional<ApiResponses> getApiResponses(final io.swagger.v3.oas.ann
if (response.extensions().length > 0) {
Map<String, Object> extensions = AnnotationsUtils.getExtensions(response.extensions());
if (extensions != null) {
for (String ext : extensions.keySet()) {
apiResponseObject.addExtension(ext, extensions.get(ext));
}
extensions.forEach(apiResponseObject::addExtension);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -130,9 +129,7 @@ public OpenAPI read(Class<?> cls) {
* @return the generated OpenAPI definition
*/
public OpenAPI read(Set<Class<?>> classes) {
Set<Class<?>> sortedClasses = new TreeSet<>(new Comparator<Class<?>>() {
@Override
public int compare(Class<?> class1, Class<?> class2) {
Set<Class<?>> sortedClasses = new TreeSet<>((class1, class2) -> {
if (class1.equals(class2)) {
return 0;
} else if (class1.isAssignableFrom(class2)) {
Expand All @@ -141,8 +138,7 @@ public int compare(Class<?> class1, Class<?> class2) {
return 1;
}
return class1.getName().compareTo(class2.getName());
}
});
});
sortedClasses.addAll(classes);

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

@Override
public OpenAPI read(Set<Class<?>> classes, Map<String, Object> resources) {
return read(classes);
}
Expand All @@ -213,9 +210,9 @@ protected String resolveApplicationPath() {
// look for inner application, e.g. ResourceConfig
try {
Application innerApp = application;
Method m = application.getClass().getMethod("getApplication", null);
Method m = application.getClass().getMethod("getApplication");
while (m != null) {
Application retrievedApp = (Application) m.invoke(innerApp, null);
Application retrievedApp = (Application) m.invoke(innerApp);
if (retrievedApp == null) {
break;
}
Expand All @@ -229,7 +226,7 @@ protected String resolveApplicationPath() {
return applicationPath.value();
}
}
m = innerApp.getClass().getMethod("getApplication", null);
m = innerApp.getClass().getMethod("getApplication");
}
} catch (NoSuchMethodException e) {
// no inner application found
Expand Down Expand Up @@ -340,8 +337,8 @@ public OpenAPI read(Class<?> cls,
.getTags(apiTags, false).ifPresent(tags ->
tags
.stream()
.map(t -> t.getName())
.forEach(t -> classTags.add(t))
.map(Tag::getName)
.forEach(classTags::add)
);
}

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

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

// iterate class methods
Method methods[] = cls.getMethods();
Method[] methods = cls.getMethods();
for (Method method : methods) {
if (isOperationHidden(method)) {
continue;
Expand Down Expand Up @@ -529,7 +526,7 @@ public OpenAPI read(Class<?> cls,
}
}
// if we have form parameters, need to merge them into single schema and use as request body..
if (formParameters.size() > 0) {
if (!formParameters.isEmpty()) {
Schema mergedSchema = new ObjectSchema();
for (Parameter formParam: formParameters) {
mergedSchema.addProperties(formParam.getName(), formParam.getSchema());
Expand All @@ -549,7 +546,7 @@ public OpenAPI read(Class<?> cls,
jsonViewAnnotationForRequestBody);

}
if (operationParameters.size() > 0) {
if (!operationParameters.isEmpty()) {
for (Parameter operationParameter : operationParameters) {
operation.addParametersItem(operationParameter);
}
Expand Down Expand Up @@ -908,7 +905,7 @@ protected Operation parseMethod(
if (apiTags != null) {
apiTags.stream()
.filter(t -> operation.getTags() == null || (operation.getTags() != null && !operation.getTags().contains(t.name())))
.map(t -> t.name())
.map(io.swagger.v3.oas.annotations.tags.Tag::name)
.forEach(operation::addTagsItem);
AnnotationsUtils.getTags(apiTags.toArray(new io.swagger.v3.oas.annotations.tags.Tag[apiTags.size()]), true).ifPresent(tags -> openApiTags.addAll(tags));
}
Expand Down Expand Up @@ -961,7 +958,7 @@ protected Operation parseMethod(
}

// apiResponses
if (apiResponses != null && apiResponses.size() > 0) {
if (apiResponses != null && !apiResponses.isEmpty()) {
OperationParser.getApiResponses(
apiResponses.toArray(new io.swagger.v3.oas.annotations.responses.ApiResponse[apiResponses.size()]),
classProduces,
Expand Down Expand Up @@ -1074,7 +1071,7 @@ private boolean shouldIgnoreClass(String className) {
rawClassName = className.replace("[simple type, class ", "");
rawClassName = rawClassName.substring(0, rawClassName.length() -1);
}
ignore = ignore || rawClassName.startsWith("javax.ws.rs.");
ignore = rawClassName.startsWith("javax.ws.rs.");
ignore = ignore || rawClassName.equalsIgnoreCase("void");
ignore = ignore || ModelConverters.getInstance().isRegisteredAsSkippedClass(rawClassName);
return ignore;
Expand Down Expand Up @@ -1171,11 +1168,10 @@ private void setOperationObjectFromApiOperationAnnotation(
operation.setDeprecated(apiOperation.deprecated());
}

ReaderUtils.getStringListFromStringArray(apiOperation.tags()).ifPresent(tags -> {
ReaderUtils.getStringListFromStringArray(apiOperation.tags()).ifPresent(tags ->
tags.stream()
.filter(t -> operation.getTags() == null || (operation.getTags() != null && !operation.getTags().contains(t)))
.forEach(operation::addTagsItem);
});
.forEach(operation::addTagsItem));

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

// RequestBody in Operation
if (apiOperation != null && apiOperation.requestBody() != null && operation.getRequestBody() == null) {
if (apiOperation.requestBody() != null && operation.getRequestBody() == null) {
OperationParser.getRequestBody(apiOperation.requestBody(), classConsumes, methodConsumes, components, jsonViewAnnotation).ifPresent(
requestBodyObject -> operation.setRequestBody(requestBodyObject));
operation::setRequestBody);
}

// Extensions in Operation
if (apiOperation.extensions().length > 0) {
Map<String, Object> extensions = AnnotationsUtils.getExtensions(apiOperation.extensions());
if (extensions != null) {
for (String ext : extensions.keySet()) {
operation.addExtension(ext, extensions.get(ext));
}
extensions.forEach(operation::addExtension);
}
}
}
Expand Down Expand Up @@ -1262,7 +1256,7 @@ protected Optional<List<Parameter>> getParametersListFromAnnotation(io.swagger.v
ResolvedParameter resolvedParameter = getParameters(ParameterProcessor.getParameterType(parameter), Collections.singletonList(parameter), operation, classConsumes, methodConsumes, jsonViewAnnotation);
parametersObject.addAll(resolvedParameter.parameters);
}
if (parametersObject.size() == 0) {
if (parametersObject.isEmpty()) {
return Optional.empty();
}
return Optional.of(parametersObject);
Expand All @@ -1279,8 +1273,7 @@ protected ResolvedParameter getParameters(Type type, List<Annotation> annotation
final OpenAPIExtension extension = chain.next();
LOGGER.debug("trying extension {}", extension);

final ResolvedParameter extractParametersResult = extension.extractParameters(annotations, type, typesToSkip, components, classConsumes, methodConsumes, true, jsonViewAnnotation, chain);
return extractParametersResult;
return extension.extractParameters(annotations, type, typesToSkip, components, classConsumes, methodConsumes, true, jsonViewAnnotation, chain);
}

private Set<String> extractOperationIdFromPathItem(PathItem path) {
Expand Down Expand Up @@ -1395,10 +1388,7 @@ protected boolean ignoreOperationPath(String path, String parentPath) {
path = path.substring(0, path.length() - 1);
}
}
if (path.equals(parentPath)) {
return true;
}
return false;
return path.equals(parentPath);
}

protected Class<?> getSubResourceWithJaxRsSubresourceLocatorSpecs(Method method) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ public static Optional<SecuritySchemePair> getSecurityScheme(io.swagger.v3.oas.a
if (securityScheme.extensions().length > 0) {
Map<String, Object> extensions = AnnotationsUtils.getExtensions(securityScheme.extensions());
if (extensions != null) {
for (String ext : extensions.keySet()) {
securitySchemeObject.addExtension(ext, extensions.get(ext));
}
extensions.forEach(securitySchemeObject::addExtension);
}
}

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

Expand Down Expand Up @@ -137,9 +133,7 @@ public static Optional<OAuthFlow> getOAuthFlow(io.swagger.v3.oas.annotations.sec
if (oAuthFlow.extensions().length > 0) {
Map<String, Object> extensions = AnnotationsUtils.getExtensions(oAuthFlow.extensions());
if (extensions != null) {
for (String ext : extensions.keySet()) {
oAuthFlowObject.addExtension(ext, extensions.get(ext));
}
extensions.forEach(oAuthFlowObject::addExtension);
}
}

Expand Down Expand Up @@ -183,10 +177,7 @@ private static boolean isEmpty(io.swagger.v3.oas.annotations.security.OAuthFlows
if (!isEmpty(oAuthFlows.password())) {
return false;
}
if (oAuthFlows.extensions().length > 0) {
return false;
}
return true;
return oAuthFlows.extensions().length == 0;
}

private static boolean isEmpty(io.swagger.v3.oas.annotations.security.OAuthFlow oAuthFlow) {
Expand All @@ -205,17 +196,11 @@ private static boolean isEmpty(io.swagger.v3.oas.annotations.security.OAuthFlow
if (!isEmpty(oAuthFlow.scopes())) {
return false;
}
if (oAuthFlow.extensions().length > 0) {
return false;
}
return true;
return oAuthFlow.extensions().length == 0;
}

private static boolean isEmpty(OAuthScope[] scopes) {
if (scopes == null || scopes.length == 0) {
return true;
}
return false;
return scopes == null || scopes.length == 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public boolean isWriteable(Class type, Type genericType, Annotation[] annotation

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

@Override
Expand All @@ -58,7 +58,7 @@ public void writeTo(OpenAPI data,
}
} else if (mediaType.isCompatible(MediaType.APPLICATION_XML_TYPE)) {
headers.remove("Content-Type");
headers.add("Content-Type", "application/json");
headers.add("Content-Type", MediaType.APPLICATION_JSON);
if (prettyPrint) {
out.write(Json.pretty().writeValueAsBytes(data));
} else {
Expand Down
Loading