swagger标签对象的动态值

问题描述 投票:0回答:1

我想在 swagger 的 tags 部分提供属性文件中的值,例如:tags = "${metric.tags}",但无法从属性文件中提取。值工作正常,值 = "${metric.value}"。

spring-boot swagger swagger-ui swagger-2.0
1个回答
0
投票
I have made plugin configuration in swagger configuration file and it started working as per my requirement.

@Bean
    public TranslationOperationBuilderPlugin translationPlugin() {
        return new TranslationOperationBuilderPlugin();
    }

    @Order(Ordered.LOWEST_PRECEDENCE)
    public static class TranslationOperationBuilderPlugin implements OperationBuilderPlugin {


        @Autowired
        Environment environment;

        @Override
        public boolean supports(DocumentationType delimiter) {
            return true;
        }

        @Override
        public void apply(OperationContext context) {

            String summary = context.operationBuilder().build().getSummary();
            String notes = context.operationBuilder().build().getNotes();
            Set<String>tags = context.operationBuilder().build().getTags();
            Set<String>translatedTags= new HashSet<>();
            for(String tag:tags) {
                if(environment.getProperty(tag)!=null) {
                    translatedTags.add(environment.getProperty(tag));
                }else {
                    translatedTags.add(tag);
                }
            }

            ModelReference modelReference= context.operationBuilder().build().getResponseModel();
            AllowableListValues allowableValues=(AllowableListValues) modelReference.getAllowableValues();
            if(allowableValues!=null && allowableValues.getValues()!=null) {
                List<String> translatedAllowables=new ArrayList<>();
                for(String value:allowableValues.getValues()) {
                    if(environment.getProperty(value)!=null) {
                        translatedAllowables.add(environment.getProperty(value));
                    }else {
                        translatedAllowables.add(value);
                    }
                }
                allowableValues.getValues().removeAll(allowableValues.getValues());
                allowableValues.getValues().addAll(translatedAllowables);
            }

            //String summaryTranslated = apiDescriptionPropertiesReader.getProperty(summary);
            //String notesTranslated = apiDescriptionPropertiesReader.getProperty(notes);
            //context.operationBuilder().summary(summaryTranslated);
            //context.operationBuilder().notes(notesTranslated);
            context.operationBuilder().tags(translatedTags);

        }
© www.soinside.com 2019 - 2024. All rights reserved.