Search code examples
javaswaggerswagger-uiswagger-2.0

How to extract parameters from an object to show in parameters in documentation


I have the following API endpoint:

@ApiResponses(
    value = {
        @ApiResponse(code = 200, message = "OK",
            responseHeaders = {
                @ResponseHeader(name = "X-RateLimit-Limit", description = "The defined maximum number of requests available to the consumer for this API.", response = Integer.class),
                @ResponseHeader(name = "X-RateLimit-Remaining", description = "The number of calls remaining before the limit is enforced and requests are bounced.", response = Integer.class),
                @ResponseHeader(name = "X-RateLimit-Reset", description = "The time, in seconds, until the limit expires and another request will be allowed in. This header will only be present if the limit is being enforced.", response = Integer.class)
            }
        )
    }
)
@ApiOperation(httpMethod = "GET", hidden = false, nickname = "Get Network Availability in JSON", value = "Get network availability for a product", response = AvailableToPromise.class, position = 1)
@RequestMapping(value = "/{product_id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> networkAvailabilityJsonResponse(
    @RequestHeader HttpHeaders headers,
    @PathVariable("product_id") String productId,
    @Valid NetworkAvailabilityCmd cmd,                  //query params
    BindingResult result)
    throws Exception {}
}

Certain parameters, such as key are taken from the query and mapped into this object through Spring MVC.

However, in the parameters section of my endpoint in the swagger-ui, it's showing me a few odd things:

None of the variables that are in NetworkAvailabilityCmd show in this parameters list, and cmd itself shows as being located in the request body (it's actually located in the query). Is there a way to hide cmd and extract the params inside this object to show on the params list? I'd like the params list to look like this (with more params):

I'm able to do this if I use @ApiImplicitParams on the method endpoint, and write out each of the params. However, this NetworkAvailabilityCmd is used for many endpoints, and having the list of params on each endpoint is very messy. Being able to extract the variables from in the object would be far cleaner, and would prevent people from forgetting to add the entire list to new endpoints.

I imagine that it requires an annotation on NetworkAvailabilityCmd cmd, and potentially something on the variables in that class, but I can't seem to find what I'm looking for in the docs.

Thanks!


Solution

  • I found out that adding @ModelAttribute worked magically. This annotation is from Spring.