Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger - timestamp weird representation

I have Spring Boot controller and for easy API reference used Swagger. Question about timestamp. Why in generated documentation example looks like:

"timestamp": { "date": 0, "day": 0, "hours": 0, "minutes": 0, "month": 0, "nanos": 0, "seconds": 0, "time": 0, "timezoneOffset": 0, "year": 0 }

So why swagger have so strange example for timestamp no just simple numeric value, which standard describes this representation?

like image 456
Sonique Avatar asked Sep 08 '17 14:09

Sonique


1 Answers

check open specification of Swagger (https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md).

If you want to solve the issue, try this sample spring boot application

import java.io.IOException;
import java.sql.Timestamp;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@RestController
@EnableSwagger2
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @RequestMapping(value = "/api", method = RequestMethod.POST)
    public Demo messages(HttpServletRequest request, HttpServletResponse response) throws IOException {
        java.util.Date date = new java.util.Date();
        return new Demo(new Timestamp(date.getTime()));
    }

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage(getClass().getPackage().getName())).paths(PathSelectors.any())
                .build().apiInfo(generateApiInfo()).directModelSubstitute(Timestamp.class, Long.class);
    }

    private ApiInfo generateApiInfo() {
        return new ApiInfo("demo", "demo.", "Version 1.0", "urn:tos", "test", "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0");
    }
}

Demo Object

import java.sql.Timestamp;

public class Demo {
private Timestamp time;

public Timestamp getTime() {
    return time;
}

public void setTime(Timestamp time) {
    this.time = time;
}

public Demo(Timestamp time) {
    super();
    this.time = time;
}

public Demo() {
    super();
}

}

check below image or open http://localhost:8080/swagger-ui.html

enter image description here

.directModelSubstitute(Timestamp.class, Long.class) is responsible of mapping between timestamp type and numeric type

like image 182
Safwan Hijazi Avatar answered Oct 19 '22 20:10

Safwan Hijazi