Reputation: 25
In swagger-core version 1.3.x, we were able to control the rendering of DateTime by using this code as also mentioned in this link :
import com.wordnik.swagger.converter.*;
String jsonString = "{" +
" \"id\": \"Date\"," +
" \"properties\": {" +
" \"value\": {" +
" \"required\": true," +
" \"description\": \"Date in ISO-8601 format\"," +
" \"notes\": \"Add any notes you like here\"," +
" \"type\": \"string\"," +
" \"format\": \"date-time\"" +
" }" +
" }" +
"}";
OverrideConverter converter = new OverrideConverter();
converter.add("java.util.Date", jsonString);
ModelConverters.addConverter(converter, true);
But this solution doesn't work in swagger 2.0 as I am not able to find OverrideConvertor in swagger 2.0.
At present the date Time from my code is being rendered as below :
LocalDateTime {
chronology (Chronology, optional),
weekOfWeekyear (integer, optional),
weekyear (integer, optional),
monthOfYear (integer, optional),
yearOfEra (integer, optional),
yearOfCentury (integer, optional),
centuryOfEra (integer, optional),
millisOfSecond (integer, optional),
millisOfDay (integer, optional),
secondOfMinute (integer, optional),
minuteOfHour (integer, optional),
hourOfDay (integer, optional),
year (integer, optional),
dayOfMonth (integer, optional),
dayOfWeek (integer, optional),
era (integer, optional),
dayOfYear (integer, optional),
fields (Array[DateTimeField], optional),
fieldTypes (Array[DateTimeFieldType], optional),
values (Array[integer], optional)
Upvotes: 1
Views: 1318
Reputation: 2843
I am not sure how to add your custom converters to swagger 1.5. But these are workarounds for your use case:
You can create a Model
and replace it in the generated swagger object, before converting it to Json.
ModelImpl m = new ModelImpl()
.description("LocalDateTime in ISO format")
.format("ISO DateTime")
.name("DateTime")
.type("string");
Response response = getListingJsonResponse(app, context, sc, headers, uriInfo);
Swagger swagger = (Swagger) response.getEntity();
swagger.getDefinitions().put("LocalDateTime", m);
That will replace the definition of LocalDateTime
with a string type model.
The other way will be to iterate through the definitions, and wherever you see the reference type to LocalDateTime, replace that with a string type:
Map<String, Model> definitions = swagger.getDefinitions();
for(Map.Entry<String, Model> e : definitions.entrySet()){
Map<String, Property> propertyMap = e.getValue().getProperties();
for(String key : propertyMap.keySet()){
Property value = propertyMap.get(key);
if(value.getType().equals("ref") && ((RefProperty) value).getSimpleRef().equals("LocalDateTime")){
propertyMap.put(key, new StringProperty("LocalDateTime in ISO format")
.example("dd-mm-yyyy")
.pattern("pattern")
.description("ISO format string"));
}
}
}
As of now I am unaware of any other cleaner way to achieve this.
UPDATE
Swagger has a mapping for DateTime
and not LocalDateTime
. So you can also consider moving your types to DateTime
if that works for you.
Upvotes: 1