在swagger-core版本1.3.x中,我们能够使用此代码控制DateTime的呈现,如此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);
但是这个解决方案在swagger 2.0中不起作用,因为我无法在swagger 2.0中找到OverrideConvertor。
目前,我的代码中的日期时间呈现如下:
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)
答案 0 :(得分:1)
我不确定如何将自定义转换器添加到swagger 1.5。但这些是您的用例的解决方法:
您可以创建Model
并在生成的swagger对象中替换它,然后再将其转换为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);
这将用字符串类型模型替换LocalDateTime
的定义。
另一种方法是迭代定义,无论你在哪里看到LocalDateTime的引用类型,用字符串类型替换它:
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"));
}
}
}
到目前为止,我还没有意识到任何其他更清洁的方法来实现这一目标。
<强>更新强>
Swagger有DateTime
而非LocalDateTime
的映射。因此,如果适用于您,您还可以考虑将类型移至DateTime
。