Simplified the security references override logic
(2194)
package springfox.documentation.swagger.readers.operation;
import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
+import com.google.common.base.Strings;
+import com.google.common.collect.FluentIterable;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;
import io.swagger.annotations.AuthorizationScope;
@@ -27,13 +30,12 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
-import org.springframework.util.StringUtils;
import springfox.documentation.builders.AuthorizationScopeBuilder;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
-import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spi.service.contexts.OperationContext;
+import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.swagger.common.SwaggerPluginSupport;
import java.util.List;
@@ -46,6 +48,7 @@ import static com.google.common.collect.Lists.*;
public class OperationAuthReader implements OperationBuilderPlugin {
private static final Logger LOG = LoggerFactory.getLogger(OperationAuthReader.class);
+
@Override
public void apply(OperationContext context) {
@@ -61,11 +64,9 @@ public class OperationAuthReader implements OperationBuilderPlugin {
Optional<ApiOperation> apiOperationAnnotation = context.findAnnotation(ApiOperation.class);
if (apiOperationAnnotation.isPresent()) {
- Authorization[] authorizationAnnotations = apiOperationAnnotation.get().authorizations();
- if (authorizationAnnotations.length > 0 && StringUtils.hasText(authorizationAnnotations[0].value())) {
- securityReferences = newArrayList();
- for (Authorization authorization : authorizationAnnotations) {
+ List<SecurityReference> securityReferenceOverrides = newArrayList();
+ for (Authorization authorization : authorizationReferences(apiOperationAnnotation.get())) {
String value = authorization.value();
AuthorizationScope[] scopes = authorization.scopes();
List<springfox.documentation.service.AuthorizationScope> authorizationScopeList = newArrayList();
@@ -90,14 +91,27 @@ public class OperationAuthReader implements OperationBuilderPlugin {
.reference(value)
.scopes(authorizationScopes)
.build();
- securityReferences.add(securityReference);
+ securityReferenceOverrides.add(securityReference);
}
+ if (!securityReferenceOverrides.isEmpty()) {
+ securityReferences.clear();
+ securityReferences.addAll(securityReferenceOverrides);
}
}
LOG.debug("Authorization count {} for method {}", securityReferences.size(), context.getName());
context.operationBuilder().authorizations(securityReferences);
}
+ private Iterable<Authorization> authorizationReferences(ApiOperation apiOperationAnnotation) {
+ return FluentIterable.from(apiOperationAnnotation.authorizations())
+ .filter(new Predicate<Authorization>() {
+ @Override
+ public boolean apply(Authorization input) {
+ return !Strings.isNullOrEmpty(input.value());
+ }
+ });
+ }
+
@Override
public boolean supports(DocumentationType delimiter) {
return SwaggerPluginSupport.pluginDoesApply(delimiter);
