我正在使用swagger工具来记录我的基于Jersey的REST API(我使用的swaggerui是在2014年6月下载的,不知道这个问题是否已在以后的版本中得到修复,但我对其进行了大量的自定义代码,所以我没有选择下载最新的,没有花很多时间再次定制它。)
到目前为止,我所有的传输对象都有一个级别的深层属性(没有嵌入的pojos)。但是现在我添加了一些返回更复杂对象(两个深度级别)的休息路径,我发现SwaggerUI在嵌入对象时没有扩展JSON模型模式。
这是swagger doc的重要部分:
...
{
"path": "/user/combo",
"operations": [{
"method": "POST",
"summary": "Inserts a combo (user, address)",
"notes": "Will insert a new user and a address definition in a single step",
"type": "UserAndAddressWithIdSwaggerDto",
"nickname": "insertCombo",
"consumes": ["application/json"],
"parameters": [{
"name": "body",
"description": "New user and address combo",
"required": true,
"type": "UserAndAddressWithIdSwaggerDto",
"paramType": "body",
"allowMultiple": false
}],
"responseMessages": [{
"code": 200,
"message": "OK",
"responseModel": "UserAndAddressWithIdSwaggerDto"
}]
}]
}
...
"models": {
"UserAndAddressWithIdSwaggerDto": {
"id": "UserAndAddressWithIdSwaggerDto",
"description": "",
"required": ["user",
"address"],
"properties": {
"user": {
"$ref": "UserDto",
"description": "User"
},
"address": {
"$ref": "AddressDto",
"description": "Address"
}
}
},
"UserDto": {
"id": "UserDto",
"properties": {
"userId": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},...
},
"AddressDto": {
"id": "AddressDto",
"properties": {
"addressId": {
"type": "integer",
"format": "int64"
},
"street": {
"type": "string"
},...
}
}
...
嵌入的对象是用户和地址,正如json响应中所示正确创建了它们的模型。
但是当打开SwaggerUI时我只能看到:
{
"user": "UserDto",
"address": "AddressDto"
}
但我应该看到类似的东西:
{
"user": {
"userId": "integer",
"name": "string",...
},
"address": {
"addressId": "integer",
"street": "string",...
}
}
扩展内部属性的代码可能有问题,javascript控制台没有显示任何错误,因此我认为这是一个错误。
答案 0 :(得分:1)
我找到了解决方案,需要修改一行代码才能使其正常工作:
在swagger.js文件中有一个getSampleValue函数,带有对undefined的条件检查:
SwaggerModelProperty.prototype.getSampleValue = function(modelsToIgnore) {
var result;
if ((this.refModel != null) && (modelsToIgnore[this.refModel.name] === 'undefined'))
...
我将等式检查更新为(删除引号):
modelsToIgnore[this.refModel.name] === undefined
之后,SwaggerUI能够显示嵌入式模型。