swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

How to custom format for some type?

Open liudaomanbu opened this issue 7 years ago • 1 comments

My swagger version is 2.9.2 My project is a Spring Cloud project. And i config custom format for some type, such as Enum,java8 time.

The result class as follows.

@Data
public class SearchHealthRecordDTO extends BaseDTO {

  /**
   * 对外唯一主键
   */
  @Length(max = 24)
  String id;

  /**
   * 创建时间
   */
  LocalDateTime gmtCreated;

  /**
   * 修改时间
   */
  LocalDateTime gmtModified;

  /**
   * 待办类型
   */
  @Length(max = 20)
  String todoType;

  /**
   * 医生id
   */
  Long doctorId;

  /**
   * 用户姓名
   */
  @Length(max = 32)
  String userRealName;

  /**
   * 用户身份证号
   */
  @Length(max = 18)
  String userIdcard;

  /**
   * 用户出生证号
   */
  @Length(max = 9)
  String userBirthNumber;

  /**
   * 用户手机号
   */
  @Length(max = 11)
  String userPhone;

  /**
   * 用户性别
   */
  GenderEnum userGender;

  /**
   * 用户年龄
   */
  Integer userAge;

  /**
   * 签约状态
   */
  BooleanEnum signState;

  /**
   * 医生姓名
   */
  @Length(max = 32)
  String doctorName;

  /**
   * 是否高血压:0无;1有
   */
  BooleanEnum isHypertension;

  /**
   * 是否糖尿病:0无;1有
   */
  BooleanEnum isDiabetes;

  /**
   * 是否严重精神障碍:0无;1有
   */
  BooleanEnum isMentalDisorder;

  /**
   * 是否肺结核:0无;1有
   */
  BooleanEnum isTuberculosis;

  /**
   * 是否残疾:0无;1有
   */
  BooleanEnum isDisability;

  /**
   * 是否贫困户:0无;1有
   */
  BooleanEnum isPoor;

  /**
   * 用户所属人群:1普通人群;2儿童(0-6岁);3孕妇;4老年人
   */
  CrowdTypeEnum userCrowdType;

  /**
   * 用户家人表id
   */
  Long userFid;

  /**
   * 签约时间
   */
  LocalDateTime signTime;

  /**
   * 邀请状态:0未邀请;1已邀请;2待确认:3已完成
   */
  Integer healthRecordState;
}

The param class as follows.

@Data
public class SearchHealthRecordReq extends PageQuery {

  /**
   * 用户姓名
   */
  @Length(max = 32)
  String userRealName;

  /**
   * 用户手机号
   */
  @Length(max = 11)
  String userPhone;

  /**
   * 用户身份证号
   */
  @Length(max = 18)
  String userIdcard;

  /**
   * 用户出生证号
   */
  @Length(max = 9)
  String userBirthNumber;

  /**
   * 签约状态
   */
  BooleanEnum signState;

  /**
   * 是否高血压:0无;1有
   */
  BooleanEnum isHypertension;

  /**
   * 是否糖尿病:0无;1有
   */
  BooleanEnum isDiabetes;

  /**
   * 是否严重精神障碍:0无;1有
   */
  BooleanEnum isMentalDisorder;

  /**
   * 是否肺结核:0无;1有
   */
  BooleanEnum isTuberculosis;

  /**
   * 是否残疾:0无;1有
   */
  BooleanEnum isDisability;

  /**
   * 是否贫困户:0无;1有
   */
  BooleanEnum isPoor;

  /**
   * 用户所属人群:1普通人群;2儿童(0-6岁);3孕妇;4老年人
   */
  CrowdTypeEnum userCrowdType;

  LocalDateTime testTime;
}

Actually,now the format as follows. image

Now,in global configuration. The Serialize format and Deserialize format of LocalDateTime are "yyyy-MM-dd HH:mm:ss". The Serialize format and Deserialize format of Enum are Integer.

But in Swagger. image image

How to custom format for some type?

liudaomanbu avatar Oct 26 '18 18:10 liudaomanbu

I have the same Issue: swagger does not show the correct format for LocalDateTime

Class Expected Format Swagger Format
java.time.Instant ISO_INSTANT the correct format ✅
java.time.LocalDateTime ISO_LOCAL_DATE_TIME ISO_INSTANT ❌

The difference is that LocalDateTime should not have the Z or an offset or a zone id at the end. That's why it's Local.

TheBestPessimist avatar Feb 21 '22 09:02 TheBestPessimist