Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Villy21 opened this issue May 27, 2022 · 0 comments
Open

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

Villy21 opened this issue May 27, 2022 · 0 comments

Comments

@Villy21
Copy link

Villy21 commented May 27, 2022

"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"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant