@ApiImplicitParams dataType file __ file swagger升级的一些坑 - OSCHINA - 中文开源技术交流社区

鸿蒙原生应用开发者激励计划发布!最高获百万现金!点击立即参与

项目中有个 controller 的方法的 DTO 中有个 field 的属性是 MultipartFile

@ApiModel(description = "材料上传form")
public class MeterialUploadForm {
    
    @ApiModelProperty(value = "工单id")
    private Long orderId;
    
    @ApiModelProperty(value = "公司id")
    private Long companyId;
    
    @ApiModelProperty("材料文件")
    private MultipartFile file;
    
    ... getter and setter...
}

然后需要用 @ApiImplicaitParam 来转一下,file 才能在 swagger 上正常使用

   @PostMapping("/fileUpload")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType="file", name = "file",
            value = "材料文件啊", required = false)})
    public String fileUpload(MeterialUploadForm form) {
        return form.getFile() == null ? "file is null" : form.getFile().getOriginalFilename();
    }

2.6.1 页面如图所示

然后升级到 2.9.2 后 swagger 的 file 框消失了

2.9.2 出错页面如图所示

后来经过排查原来把 @ApiImplicitParam 的 dataType 改下就好了变成了

  @PostMapping("/fileUpload")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType="file", name = "file",
            value = "材料文件啊", required = false)})
    public String fileUpload(MeterialUploadForm form) {
        return form.getFile() == null ? "file is null" : form.getFile().getOriginalFilename();
    }

2.9.2 修复后页面如果所示

原来是 swagger 升级的时候做了些调整
将其中的 "file" 改成了 "__file"

2.6.1 的 Types

public class Types {
  private Types() {
    throw new UnsupportedOperationException();
  }

  private static final Set<String> baseTypes = newHashSet(
      "int",
      "date",
      "string",
      "double",
      "float",
      "boolean",
      "byte",
      "object",
      "long",
      "date-time",
      "file",
      "biginteger",
      "bigdecimal");
  private static final Map<Type, String> typeNameLookup = ImmutableMap.<Type, String>builder()
      .put(Long.TYPE, "long")
      .put(Short.TYPE, "int")
      .put(Integer.TYPE, "int")
      .put(Double.TYPE, "double")
      .put(Float.TYPE, "float")
      .put(Byte.TYPE, "byte")
      .put(Boolean.TYPE, "boolean")
      .put(Character.TYPE, "string")

      .put(Date.class, "date-time")
      .put(java.sql.Date.class, "date")
      .put(String.class, "string")
      .put(Object.class, "object")
      .put(Long.class, "long")
      .put(Integer.class, "int")
      .put(Short.class, "int")
      .put(Double.class, "double")
      .put(Float.class, "float")
      .put(Boolean.class, "boolean")
      .put(Byte.class, "byte")
      .put(BigDecimal.class, "bigdecimal")
      .put(BigInteger.class, "biginteger")
      .put(Currency.class, "string")
      .put(UUID.class, "string")
      .put(MultipartFile.class, "file")
      .build();

  public static String typeNameFor(Type type) {
    return typeNameLookup.get(type);
  }

  public static boolean isBaseType(String typeName) {
    return baseTypes.contains(typeName);
  }

  public static boolean isBaseType(ResolvedType type) {
    return baseTypes.contains(typeNameFor(type.getErasedType()));
  }

  public static boolean isVoid(ResolvedType returnType) {
    return Void.class.equals(returnType.getErasedType()) || Void.TYPE.equals(returnType.getErasedType());
  }
}

2.9.2Types

public class Types {
  private Types() {
    throw new UnsupportedOperationException();
  }

  private static final Set<String> baseTypes = newHashSet(
      "int",
      "date",
      "string",
      "double",
      "float",
      "boolean",
      "byte",
      "object",
      "long",
      "date-time",
      "__file",
      "biginteger",
      "bigdecimal",
      "uuid");
  private static final Map<Type, String> typeNameLookup = ImmutableMap.<Type, String>builder()
      .put(Long.TYPE, "long")
      .put(Short.TYPE, "int")
      .put(Integer.TYPE, "int")
      .put(Double.TYPE, "double")
      .put(Float.TYPE, "float")
      .put(Byte.TYPE, "byte")
      .put(Boolean.TYPE, "boolean")
      .put(Character.TYPE, "string")

      .put(Date.class, "date-time")
      .put(java.sql.Date.class, "date")
      .put(String.class, "string")
      .put(Object.class, "object")
      .put(Long.class, "long")
      .put(Integer.class, "int")
      .put(Short.class, "int")
      .put(Double.class, "double")
      .put(Float.class, "float")
      .put(Boolean.class, "boolean")
      .put(Byte.class, "byte")
      .put(BigDecimal.class, "bigdecimal")
      .put(BigInteger.class, "biginteger")
      .put(Currency.class, "string")
      .put(UUID.class, "uuid")
      .put(MultipartFile.class, "__file")
      .build();

  public static String typeNameFor(Type type) {
    return typeNameLookup.get(type);
  }

  public static boolean isBaseType(String typeName) {
    return baseTypes.contains(typeName);
  }

  public static boolean isBaseType(ResolvedType type) {
    return baseTypes.contains(typeNameFor(type.getErasedType()));
  }

  public static boolean isVoid(ResolvedType returnType) {
    return Void.class.equals(returnType.getErasedType()) || Void.TYPE.equals(returnType.getErasedType());
  }
}

询问 AI


原网址: 访问
创建于: 2024-12-11 18:17:39
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论