dwickern / swagger-play

Swagger API docs for Play Framework
Apache License 2.0
22 stars 4 forks source link

Support for @ApiParam(hidden = true) with SwaggerSpecFilter #11

Open Villy21 opened 2 years ago

Villy21 commented 2 years ago

"swagger-play2.8" % "3.1.0" did not support @ApiParam(hidden = true) so I implemented parameter hide with SwaggerSpecFilter.

[WARN] SwaggerSpecFilter work only in this swagger-play but not with sbt-swagger-play plugin because of Issue 11.

To use SwaggerSpecFilter to hide parameters:

  1. Add to your project class:
package filters;

import io.swagger.core.filter.AbstractSpecFilter;
import io.swagger.model.ApiDescription;
import io.swagger.models.Operation;
import io.swagger.models.parameters.Parameter;
import play.Logger;
import java.util.List;
import java.util.Map;

public class InternalSwaggerFilter extends AbstractSpecFilter
{
    protected final Logger.ALogger logger = Logger.of("swagger");

    @Override
    public boolean isParamAllowed(Parameter parameter, Operation operation, ApiDescription api, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
        String access  = parameter.getAccess();
        if ((access != null) && access.equals("hidden")) {
            logger.debug(String.format("InternalSwaggerFilter:isParamAllowed %s %s parameter:%s access=hidden -> false", operation.getOperationId() ,api.getPath() ,parameter.getName()));
            return false;
        }
        return true;
    }
}
  1. In application.conf add:
swagger.filter="filters.InternalSwaggerFilter"
play.modules.enabled += "play.modules.swagger.SwaggerModule"
  1. Now for parameters you can use @ApiParam(access = "hidden") to hide parameters you not need.

Like:

@Api(value = "Base Api", produces = "application/json")
public class APIController extends Controller {
    @Path("/users")
    @ApiOperation(
            tags={ "user" },
            value = "Get users list."
    )
    public CompletionStage<Result> getUsersList(
            @ApiParam(access = "hidden") Http.Request request){
        return getList(request, User.class);
    }
}
  1. Library dependencies I used for test.
    libraryDependencies ++= Seq(
    // API annotations
    "io.swagger" % "swagger-annotations" % "1.6.6",
    "io.swagger" % "swagger-core" % "1.6.6",
    // The following is only necessary if you're having trouble resolving dependencies
    "javax.ws.rs"          % "jsr311-api"           % "1.1.1",
    "com.github.dwickern" %% "swagger-play2.8" % "3.1.0",
    "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.11.1"
    )