Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize the value of operationId generated in api spec with swagger?

I have configured my spring project using springfox 2.0. I am able to generate the open api spec with it.

 "paths": {
    "/test/testinfo": {
      "post": {
        "tags": [
          "test-controller"
        ],
        "summary": "getTestInfo",
        "operationId": "getTestInfoInfoUsingGET",
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ]

As you can see the value of operationId is of format

[java_method_name_here]Using[HTTP_verb_here]

ex. getPetsUsingGET

This operationId is used while generating clients using swagger-codegen. Does anyone know how to customize it? I know this can be done per api using @ApiOperation but is there a more generic way to define this format for all apis?

like image 882
Ganesh Avatar asked Aug 08 '16 05:08

Ganesh


People also ask

How do I change the default value in Swagger UI?

Use the default keyword in the parameter schema to specify the default value for an optional parameter. The default value is the one that the server uses if the client does not supply the parameter value in the request. The value type must be the same as the parameter's data type.

What is the use of operationId in Swagger?

operationId is an optional unique string used to identify an operation. If provided, these IDs must be unique among all operations described in your API. Some common use cases for operationId are: Some code generators use this value to name the corresponding methods in code.


2 Answers

The simplest option would be implementing your own name generator, extending Springfox's one, like so:

public class SwaggerOperationIdTrimmer implements OperationNameGenerator {

private static final String DEFAULT_SPRINGFOX_PATTERN_REGEX = "Using(GET|POST|PUT|DELETE)(_[0-9])?";

@Override
public String startingWith(String operationId) {
    //Trimming of default names generated by Springfox, eliminating the extra appended information
    return operationId.replaceAll(DEFAULT_SPRINGFOX_PATTERN_REGEX, "");
}

}

The regex is key here :)

The only thing left would be instantiating it as a bean in your configuration file, adding it as primary to override the default CachingOperationNameGenerator of Springfox:

@Bean
@Primary
public SwaggerOperationIdTrimmer customOperationNameGenerator() {
    return new SwaggerOperationIdTrimmer();
}
like image 138
Miguel Ruiz Avatar answered Sep 18 '22 20:09

Miguel Ruiz


We can also use this approach of Nickname which overrides default operationId.

@ApiOperation(value = "", nickname = "getMeAllThePetsPlease")
@RequestMapping(value = "/pets", method = RequestMethod.GET)
public Model getAllThePets() {
    ...
}

so we will be overriding operationId: getAllThePetsByGet with getMeAllThePetsPlease.

Note: Nickname will overrides the operationId so this can be customized accordingly.

like image 30
AVINASH M Avatar answered Sep 19 '22 20:09

AVINASH M