Search code examples
javaswaggerswagger-codegenswagger-editor

swagger.yaml :: How to generate properties with "Long" data type and "Timestamp" datatype?


I am using swagger-codegen-maven-plugin to generate Java classes based on swagger.yaml spec.

I have added a property phoneNum in the swagger.yaml as below:

phoneNum:
    type: number

I am expecting it generate the property with Integer or Long datatype.
But it got generated as BigDecimal phoneNum;

As per the swagger docs, it was mentioned to use format: int64

  phoneNum:
    type: number
    format: int64

But still, it ends up generating private BigDecimal phoneNum;

If anyone of you have faced similar issue and have a workaround, please share it here.

Thank you.


Solution

  • In order to generate a Long property you have to use integer as type and int64 as format

    phoneNum:
        type: integer
        format: int64
    

    Looking at AbstractJavaCodegen I think there is no way to generate a Timestamp property.
    The only available types are:

    • org.threeten.bp.LocalDate
    • org.threeten.bp.OffsetDateTime
    • org.joda.time.LocalDate
    • org.joda.time.DateTime
    • java.time.LocalDate
    • java.time.LocalDateTime
    • java.time.OffsetDateTime
    • java.time.Instant (added on Feb 2019)
    • java.util.Date

    Here's a way to convert OffsetDateTime to Timestamp

    OffsetDateTime dateTime = OffsetDateTime.now();
    Timestamp timestamp = Timestamp.valueOf(dateTime.atZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime());