Swagger @ApiModelProperty 注解的解析类

2023-04-09  本文已影响0人  何家小富富

swagger版本:3.0
由于之前项目采用@ApiModelProperty对响应属性进行说明,但是由于dataType的属性不规范,造成了swagger中的类型说明为object,所以跟踪了一下swagger的注解解析过程。

ApiModelPropertyPropertyBuilder.class关键代码

public void apply(ModelPropertyContext context) {
    Optional<ApiModelProperty> annotation = empty();

    if (context.getAnnotatedElement().isPresent()) {
      annotation =
          annotation.map(Optional::of)
                    .orElse(findApiModePropertyAnnotation(context.getAnnotatedElement().get()));
    }
    if (context.getBeanPropertyDefinition().isPresent()) {
      annotation = annotation.map(Optional::of).orElse(findPropertyAnnotation(
          context.getBeanPropertyDefinition().get(),
          ApiModelProperty.class));
    }
    if (annotation.isPresent()) {
      ModelSpecification modelSpecification =
          annotation.map(a -> {
            if (!a.dataType().isEmpty()) {
              // 关注点1:
              return modelSpecifications.create(context.getOwner(), toType(context.getResolver()).apply(a));
            }
            return null;
          })
          .orElse(null);
      Optional<ApiModelProperty> finalAnnotation = annotation;
      context.getSpecificationBuilder()
             .description(annotation.map(toDescription(descriptions)).orElse(null))
             .readOnly(annotation.map(ApiModelProperty::readOnly).orElse(false))
             .isHidden(annotation.map(ApiModelProperty::hidden).orElse(false))
             //注意此处修改了Type属性
             .type(modelSpecification)
             .position(annotation.map(ApiModelProperty::position).orElse(0))
             .required(annotation.map(ApiModelProperty::required).orElse(false))
             .example(annotation.map(toExample()).orElse(null))
             .enumerationFacet(e -> e.allowedValues(finalAnnotation.map(toAllowableValues()).orElse(null)));

      context.getBuilder()
             .allowableValues(annotation.map(toAllowableValues()).orElse(null))
             .required(annotation.map(ApiModelProperty::required).orElse(false))
             .readOnly(annotation.map(ApiModelProperty::readOnly).orElse(false))
             .description(annotation.map(toDescription(descriptions)).orElse(null))
             .isHidden(annotation.map(ApiModelProperty::hidden).orElse(false))
             .type(annotation.map(toType(context.getResolver())).orElse(null))
             .position(annotation.map(ApiModelProperty::position).orElse(0))
             .example(annotation.map(toExample()).orElse(null));
    }
  }

ApiModelProperties.class 关键代码2

static Function<ApiModelProperty, ResolvedType> toType(final TypeResolver resolver) {
    return annotation -> {
      try {
      //ApiModelProperty  的dataType 只能写 全局限域名
        return resolver.resolve(Class.forName(annotation.dataType()));
      } catch (ClassNotFoundException e) {
       // 如果找不到类对象,则会被覆盖为Object类型
        return resolver.resolve(Object.class);
      }
    };
  }

建议 @ApiModelProperty 注解的dataType不写,采用swagger默认的属性说明

上一篇 下一篇

猜你喜欢

热点阅读