I am trying to get swagger spec(yaml) from Java code using Swagger Annotations in a spring boot application. I annotate the models and then run the springboot app and then get the spec from http://localhost:8080/v2/api-docs.
My Model Looks like below :
package com.indiana.core;
import java.time.LocalDateTime;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
@lombok.ToString
@lombok.Getter
@lombok.Setter
public class SampleClass{
@JsonProperty(value = "birth_date_time")
private LocalDateTime birthDateTime;
....
}'
I want to create correct swagger yaml speck for this. I expect the below when I access swagger UI which provides the yaml automatically .
Note that dateOfBirth is a Java 8 LocalDateTime class . But as per swagger spec(https://swagger.io/specification/ look for Data Types Sections ) the Date in yaml should be type : 'string' and format: date-time.
'SampleClass:
properties:
dateOfBirth:
type: string
format: date-time'
What I am getting now :
It looks like swagger is building a detailed definition LocalDateTime, which is not I am looking for .
'"definitions": {
"LocalDateTime": {
"type": "object",
"properties": {
"chronology": {
"$ref": "#/definitions/Chronology"
},
"dayOfMonth": {
"type": "integer",
"format": "int32"
},
"dayOfWeek": {
"type": "string",
"enum": ["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"]
},
"dayOfYear": {
"type": "integer",
"format": "int32"
},
"hour": {
"type": "integer",
"format": "int32"
},
"minute": {
"type": "integer",
"format": "int32"
},
"month": {
"type": "string",
"enum": ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"]
},
"monthValue": {
"type": "integer",
"format": "int32"
},
"nano": {
"type": "integer",
"format": "int32"
},
"second": {
"type": "integer",
"format": "int32"
},
"year": {
"type": "integer",
"format": "int32"
}
}
},
I have tried with
'A. @ApiModelProperty(dataType = "java.lang.String", example = "17-03-2019 22:18:59", notes = "Birthdaytime desc")
B. @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM-dd-yyyy hh:mm:ss")'
Out these for A , the element is defined as string only in the yaml , the 'format: date-time' is missing like below .
SampleClass:
properties:
dateOfBirth:
type: string
What tweaking is needed to create aperfect swegger for this ? I need this
SampleClass:
properties:
dateOfBirth:
type: string
format: date-time .
Just put below annotation on your LocalDateTime field to format datetime in swagger definition:
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", shape = Shape.STRING)
@ApiModelProperty(required = true, example = "2021-08-20T00:00:00")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With