Ocultar método soportado por endpoint de terceros. Spring y Swagger.

A veces, queremos ocultar que uno de los métodos soportados por un endpoint que no controlamos no aparezca en Swagger.

No he visto que exista ninguna función para hacer esto.

Este es el Predicate que he implementado.

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.common.base.Predicate;

import springfox.documentation.RequestHandler;

public class IsRequestMethodPredicate implements Predicate<RequestHandler> {

	private final String path;
	private final RequestMethod method;

	public IsRequestMethodPredicate(String path, RequestMethod method) {
		this.path = path;
		this.method = method;
	}

	public static IsRequestMethodPredicate isRequestMethod(String path, RequestMethod method) {
		return new IsRequestMethodPredicate(path, method);
	}

	public static Predicate<RequestHandler> isNotRequestMethod(String path, RequestMethod method) {
		return not(isRequestMethod(path, method));
	}

	@Override
	public boolean apply(RequestHandler input) {
		RequestMapping mapping = AnnotationUtils
				.findAnnotation(input.getHandlerMethod().getMethod(), RequestMapping.class);
		if (mapping != null && mapping.path().length > 0) {
			return hasPath(mapping, path) && hasMethod(mapping, method);
		}
		return false;
	}

	private static boolean hasPath(RequestMapping mapping, String path) {
		for (String mappingPath : mapping.path()) {
			if (mappingPath != null && mappingPath.equals(path)) {
				return true;
			}
		}
		return false;
	}

	private static boolean hasMethod(RequestMapping mapping, RequestMethod method) {
		for (RequestMethod mappingMethod : mapping.method()) {
			if (mappingMethod == method) {
				return true;
			}
		}
		return false;
	}
}

Para utilizarlo:

import static IsRequestMethodPredicate.isNotRequestMethod;

@Bean
public Docket api() {
	return new Docket(DocumentationType.SWAGGER_2)
			.select()
			.apis(isNotRequestMethod("/some/third/party/endpoint", RequestMethod.GET))
			.paths(paths())
			.build();
}

Deja un comentario