swagger-api / swagger-play

Apache License 2.0
330 stars 181 forks source link

Hidding query parameter from the controller method #206

Open dsnkostic opened 5 years ago

dsnkostic commented 5 years ago

In our project we have need to pass Request object to our controller. This is the routes file POST /check controllers.CheckConotroller.checkStatus(request: Request) Swagger is generating this output for the parameters object: "parameters": [ { "name": "request", "in": "query", "required": false, "type": "string" } ],

Is there any way to remove the request parameter from the swagger generation?

In swagger annotations document (https://github.com/swagger-api/swagger-core/wiki/Annotations-1.5.X#apiparam), there is a reference to the @ApiParam annotation and hidden flag, but that does not work. With @ApiParam you can control what metadata you want to generate for the given query parameter, but as soon as you set hidden to true, all metadata are ignored and parameter is still in documentation.

An example If you have controller method declaration like this: public CompletionStage<Result> checkStatus(@ApiParam(required = true, type = "1", value = "something") Http.Request request) then you get this in documentation: "parameters": [ { "name": "request", "in": "query", "description": "something", "required": true, "type": "1" } ], However, if you set hidden to true: public CompletionStage<Result> checkStatus(@ApiParam(required = true, type = "1", value = "something", hidden = true) Http.Request request) then all annotation metadata is ignored and parameter is reverted back (or it is indeed removed, but default one is added once again): "parameters": [ { "name": "request", "in": "query", "required": false, "type": "string" } ],

Adding an implicit parameter with the same name,, just adds another parameter without affecting the existing one.

Using Play for Java version: 2.7.0 and swagger-play2 version: 1.6.1

Rendrik commented 4 years ago

We've come across this issue moving to play 2.7, where the HTTP Request really needs to be added to the route as a parameter. Our solution is to intercept the request and just remove any route parameters with the name of "request".

      Map<String, Path> paths = swagger.getPaths();
      if(paths != null) {
        for(String key : paths.keySet()) {
          List<Operation> ops = paths.get(key).getOperations();
          for(Operation op : ops) {
            List<Parameter> params = op.getParameters();
            params.removeIf(f -> "request".equals(f.getName()));   
          }
        }
      }

This is an easy solution assuming that the route being added is always the same name, and that there aren't any real API parameters also named "route".