swagger date field vs date-time field
Asked Answered
S

2

20

I am using swagger to test my rest api, one of the property of my entity class is a date field for which I need the date in yyyy-mm-dd format , but swagger model schema is showing this field as date-time instead of date field, therefore it gives date with time and zone. How can I convert this date-time into date field ?

I have a java entity class TimeEntry.java one of its property is Date, it looks like this.

@ApiModelProperty(required = true)
@JsonFormat(pattern = DATE_FORMAT)
private Date date;

for this field, on the swagger UI model schema, the field date displays as "date": "2016-01-08T22:34:22.337Z" but I need this as "date":"2016-01-08" .

I have tried the following:

1.

@ApiModelProperty(required = true, dataType="date")  
@JsonFormat(pattern = DATE_FORMAT)   
private Date date;

2.Tried to follow along this code (override OverrideConvertor class) but could not find swagger-core 1.3 version mvn repo. Only available is 1.5 version https://github.com/swagger-api/swagger-core/wiki/overriding-models

  1. Apparently from 1.5 version they have removed the OverrideConvertor class https://groups.google.com/forum/#!topic/swagger-swaggersocket/ChiknyHZiP4

Please help.

Suppression answered 11/1, 2016 at 16:9 Comment(2)
From looking at the javadocs I believe dataType is a java type, as in java.lang.String or int, not the "swagger type".Sclar
@ApiModelProperty is the swagger annotation , this annotation has a method dataType which takes the fully qualified path of the class of the type we need , in this case it is org.joda.time.LocalDate. In the Answer section says the same. Check the overriding property data type section 5.3 here: springfox.github.io/springfox/docs/snapshot/…Suppression
O
16

The problem (one of the problems actually) with java.util.Date is that it's really a date-time, and swagger correctly detects it as such. I do understand that the @JsonFormat is a workaround for this as well--swagger does not support that annotation during it's type detection.

You have three options to properly handle date types.

1) Use Joda's LocalDate as the datatype. If you declared private LocalDate date, it would appear correctly.

2) Use java8's LocalDate, same as above.

3) Tell swagger to use either of the above when detecting the type in the annotation, but keep the property as type java.util.Date:

@ApiModelProperty(required = true, dataType = "org.joda.time.LocalDate")

Then, when scanning, swagger will detect this as a date formatted string.

Onetime answered 12/1, 2016 at 17:50 Comment(4)
Thank you very much for responding to my question. I tried all the above options but the format I am getting for the date field on swagger (for all the three options) is like this. "date": { "chronology": { "calendarType": "string", "id": "string" }, "dayOfMonth": 0, "dayOfWeek": "MONDAY", "dayOfYear": 0, "era": { "value": 0 }, "leapYear": true, "month": "JANUARY", "monthValue": 0, "year": 0 }Suppression
Can you please confirm the version of swagger-core that you're using? 1.5.6 is latest.Onetime
We are using swagger-core 1.5.3 version, also I should mention that we are using springfox-swager2 dependency . The Dependency in my pom file is : <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${spring.fox.version}</version> </dependency>Suppression
Springfox uses the core swagger annotations, I suggest you open an issue in their repository to see that they update to 1.5.6, and perhaps add tests for this situation.Onetime
S
13

My team mate has found the fix. We needed to upgrade the springfox version to 2.3.0 , previously we were using springfox 2.2.2 version. In that old version swagger's @ApiModelPreporty has attribute called "example" which was not doing anything. From the version 2.3.0 version this "example" started working. So after we upgraded the springfox version to 2.3.0 , all we had to do is as shown below.

@ApiModelProperty(required = true,example = "2016-01-01")
@JsonFormat(pattern = DATE_FORMAT)
private LocalDate date; 

Below is the link from where we found this information:

https://github.com/springfox/springfox/issues/998

Suppression answered 25/1, 2016 at 20:59 Comment(1)
Tnx ,@ApiModelProperty helped me.Vernacularize

© 2022 - 2024 — McMap. All rights reserved.