SmartSwaggerApiModelEnumPlugin.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. *
  3. * Copyright 2015 the original author or authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. *
  18. */
  19. package net.lab1024.smartadmin.common.swagger;
  20. import net.lab1024.smartadmin.common.anno.ApiModelPropertyEnum;
  21. import net.lab1024.smartadmin.common.domain.BaseEnum;
  22. import com.google.common.base.Function;
  23. import com.google.common.base.Optional;
  24. import org.apache.commons.lang3.StringUtils;
  25. import org.springframework.core.annotation.AnnotationUtils;
  26. import springfox.documentation.spi.DocumentationType;
  27. import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
  28. import springfox.documentation.spi.schema.contexts.ModelPropertyContext;
  29. import springfox.documentation.swagger.common.SwaggerPluginSupport;
  30. import java.lang.reflect.AnnotatedElement;
  31. import static springfox.documentation.schema.Annotations.findPropertyAnnotation;
  32. /**
  33. * swagger 用于说明枚举类字段说明
  34. * SWAGGER_PLUGIN_ORDER+1 是将此配置放在原来的后面执行
  35. *
  36. * @author listen
  37. * @date 2019年5月16日 15:36:56
  38. */
  39. public class SmartSwaggerApiModelEnumPlugin implements ModelPropertyBuilderPlugin {
  40. @Override
  41. public void apply(ModelPropertyContext context) {
  42. Optional<ApiModelPropertyEnum> enumOptional = Optional.absent();
  43. if (context.getAnnotatedElement().isPresent()) {
  44. enumOptional = enumOptional.or(findApiModePropertyAnnotation(context.getAnnotatedElement().get()));
  45. }
  46. if (context.getBeanPropertyDefinition().isPresent()) {
  47. enumOptional = enumOptional.or(findPropertyAnnotation(context.getBeanPropertyDefinition().get(), ApiModelPropertyEnum.class));
  48. }
  49. if (enumOptional.isPresent()) {
  50. ApiModelPropertyEnum anEnum = enumOptional.get();
  51. String enumInfo = BaseEnum.getInfo(anEnum.value());
  52. context.getBuilder()
  53. .required(enumOptional.transform(toIsRequired()).or(false))
  54. .description(anEnum.enumDesc() + ":" + enumInfo)
  55. .example(enumOptional.transform(toExample()).orNull())
  56. .isHidden(anEnum.hidden());
  57. }
  58. }
  59. @Override
  60. public boolean supports(DocumentationType delimiter) {
  61. return SwaggerPluginSupport.pluginDoesApply(delimiter);
  62. }
  63. static Function<ApiModelPropertyEnum, Boolean> toIsRequired() {
  64. return annotation -> annotation.required();
  65. }
  66. public static Optional<ApiModelPropertyEnum> findApiModePropertyAnnotation(AnnotatedElement annotated) {
  67. return Optional.fromNullable(AnnotationUtils.getAnnotation(annotated, ApiModelPropertyEnum.class));
  68. }
  69. static Function<ApiModelPropertyEnum, String> toExample() {
  70. return annotation -> {
  71. String example = annotation.example();
  72. if (StringUtils.isBlank(example)) {
  73. return "";
  74. }
  75. return example;
  76. };
  77. }
  78. }