Swagger ui - Query param
Asked Answered
C

4

13

I am using Swagger ui and Swagger core (1.3) for a jersey application. I have certain query parameters which I must send with every request like post, get, delete...

How can I default this ?

Curacy answered 1/7, 2015 at 19:5 Comment(1)
Could you choose a correct response ?Touchwood
A
15

You can use the annotation @ApiParam from the Swagger annotations in order to configure the Query param to be used from the Swagger-UI.

For example

@Path("/{username}")
@ApiOperation(value = "Updated user")
public Response updateUser(
  @ApiParam(value = "description for query-parameter") @QueryParam("username") String username
) {
...
}

Please, read more about this annotation in the following official documentation: https://github.com/swagger-api/swagger-core/wiki/Annotations#apiparam

Aloe answered 2/7, 2015 at 3:5 Comment(2)
ApiParam for queryparam can be used only if the operation has such parameter in its definition. But, here I want to send a Query parameter with the url that will be accessed when a post or get or delete operation is tried out. Hope you understand what I am looking forCuracy
In the scope of the function, check if the variable's value is null. If it is, that means the query parameter was not specified in the HTTP request. That is, you can do if (username == null) { username = "myDefaultValue"; }.Marmoset
S
3

For swagger version 3, you can use the annotation @Parameter

import io.swagger.v3.oas.annotations.Parameter

https://docs.swagger.io/swagger-core/v2.0.0-RC3/apidocs/io/swagger/v3/oas/annotations/Parameter.html

Smithery answered 14/9, 2022 at 20:44 Comment(0)
T
1

You can't, but since swagger 2.0 (I don't know if this is supported by swagger-code/swagger-ui), you can defines parameters to be reuse across operations.

For example :

{
  "parameters": {
    "pageParam": {
      "name": "page",
      "in": "query",
      "description": "page number to get",
      "required": false,
      "type": "integer",
      "format": "int32"
    }
  },
  "paths": {
    "/customers": {
      "get":  {
        "description": "Retrive list of customers",
        "parameters": {
          "$ref": "#/parameters/pageParam"
        },
        ...
      }
    }
  },
  ...
}
Touchwood answered 1/7, 2015 at 21:5 Comment(0)
C
1

Building on @Ilya Y's answer here's a snippet from a controller I had using io.swagger.v3.oas.annotations.Parameter;

@PostMapping("/report")
public FileItem generateReport(
        @RequestParam String projectId,
        @RequestParam @DateTimeFormat(iso = ISO.DATE) @Parameter(schema = @Schema(type = "string", description = "Date in ISO format (yyyy-MM-dd)")) LocalDate date,
        @RequestParam(defaultValue = "UTC") String zoneId //
) {
...
}
Collectivity answered 8/8, 2023 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.