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?
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.
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.
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();
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With