Wednesday 21 October 2020

Customer operation unique id for springfox swagger lib

 when using swagger, each operation can have a id, by default this id is nickname of the annotation, for instance, 

@ApiOperation(value = "create a new user", nickname = "oper_code_create_new_user")

The oper_code_create_new_user will be used as unique id (not sure why the lib use nickname as a unique id but it does). How if you do not add this annotation on the method, for instance, you may have a parent Controller class, and the sub-Controller class just reuse the method without override it. in this case, the unique id will be oper_code_create_new_user_1 by default (as implementated in CachingOperationNameGenerator.java in springfox-spring-web library)

if you want to customize the unique id in the sub-class, for instance, I want to add the sub-class name or the value defined in controller lever tag name which is in @Api annotation, as the suffix,  you can reference below code sneppet.

@Component
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER + 1000)
public class OperationCustomizeUniqueIdReader implements OperationBuilderPlugin {

@Override
public void apply(OperationContext context) {
Optional<ApiOperation> apiOperation = context.findAnnotation(ApiOperation.class);

if (apiOperation.isPresent()) {
Optional<Api> apiOptional = context.findControllerAnnotation(Api.class);
if (apiOptional.isPresent()) {
String tagName = apiOptional.get().tags()[0];
ApiOperation operation = apiOperation.get();
String nickname = operation.nickname();

if (StringUtils.isNotEmpty(nickname) && StringUtils.isNotEmpty(tagName)) {
if (nickname.equalsIgnoreCase("oper_code_create_new_user") ) {
context.operationBuilder().uniqueId(nickname + "_" + tagName);
context.operationBuilder().codegenMethodNameStem(nickname + "_" + tagName);
}
}
}
}
}
@Override
public boolean supports(DocumentationType documentationType) {
return SwaggerPluginSupport.pluginDoesApply(documentationType);
}
}

if you have a sub class with @Api(tags = {"aSubResource"}), then you will get a unique name
for this method "oper_code_create_new_user_aSubResource"

3 comments:

  1. Within a few days my results were back and my diagnosis confirmed.... I had HSV2. I felt angry and betrayed because he had not been honest with me and I had a feeling that he knew he had it based on how he responded when I gave him the news.Fast forward to a year down the track and I finally broke free of my drug addiction and my extremely toxic relationship, but I felt tainted and overwhelmed now more than ever by this condition. So I sucked it up and decided I would not live like this - despite every person I talked to about it saying that herpes was incurable, once you had it you had it for the rest of your life, blah blah blah. I knew this was bullshit. Deep down I trusted my body and I wasn't about to let anyone tell me what was and was not possible for me to achieve once I set my mind to it. I placed an order on Herbalist RAZOR Herbal Medicine for Curing the virus Through His YouTube Channel @ HERBALIST RAZOR and it worked the magic, Herbalist RAZOR is a Godsent to mankind, Visit his website : https://herbalistrazorherb.wixsite.com/drrazorherbalhome . Email Herbalist RAZOR via herbalistrazorherbalhome@gmail.com WhatsApp/Call directly via +2349065420442

    ReplyDelete
  2. All thanks to Mr Anderson Carl for helping with my profits and making my fifth withdrawal possible. I'm here to share an amazing life changing opportunity with you. its called Bitcoin / Forex trading options. it is a highly lucrative business which can earn you as much as $2,570 in a week from an initial investment of just $200. I am living proof of this great business opportunity. If anyone is interested in trading on bitcoin or any cryptocurrency and want a successful trade without losing notify Mr Anderson Carl now on Whatsapp: +1(252)285-2093 Email: andersoncarlassettrade@gmail.com

    ReplyDelete
  3. MASSIVE THANKS TO HERBALIST DOCTOR KHAM MEDICINE FOR CURING ME FROM HERPES VIRUS, UNBELIEVABLE TWO WEEKS HERPES CURE BY HERBALIST KHAM. VISIT HERBALIST KHAM WEBSITE: https://herbalistdrkhamcaregiver.simdif.com/There are treatments to get rid of genital herpes/ disease with traditional herbs from Dr. Kham. I was positive about this infection (HERPES) and I lost hope because I was outcast and rejected even by my best friends. I searched online to know more on how to eradicate this virus and enquired about a cure for herpes and I got a testimony posted by Dr. Kham patient on how she was cured from HSV-2, So i decided to contact the same herbalist Dr. Kham because I know that nature has the power to heal everything, So I contacted him to know he can help me and he told me never to worry that he will help me out with his natural herbs created by God Almighty! After 2 days of contacting him, He told me that the cure has been prepared and he will send it to me via (UPS DELIVERY SERVICES) and it got to me in 5 days! I used the medicine as he instructed me(MORNING AND EVENING) and I was cured after taking the dosage for two weeks. It really looks like a dream to me but I am so happy now!! that is the reason I decided to also add more comment of Him to this blog because that was what I promise him that I would testify about his good work to the people if his herbal medicine would cure me, So if you need his help also, You can Email him on dr.khamcaregiver@gmail.com or You can Message him on ICQ or WhatsApp with his contact number +2348159922297 for more information.

    ReplyDelete