我正在使用swagger工具记录我基于泽西岛的REST API(我使用的swaggerui于2014年6月下载,不知道此问题是否已在更高版本中得到解决,但是由于我对其代码进行了大量自定义,所以我如果没有花费大量时间来重新自定义它,则无法选择下载最新版本)。
到目前为止,到目前为止,我所有的传输对象都具有一级深度属性(没有嵌入式pojos)。但是,现在我添加了一些返回更复杂对象(两个深度级别)的休息路径,我发现SwaggerUI在具有嵌入式对象时不会扩展JSON模型架构。
这是swagger文档的重要部分:
...
{
"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"
},...
}
}
...
嵌入的对象是User和Address,它们的模型已正确创建,如json响应中所示。
但是当打开SwaggerUI时,我只能看到:
{
"user": "UserDto",
"address": "AddressDto"
}
但是我应该看到类似的东西:
{
"user": {
"userId": "integer",
"name": "string",...
},
"address": {
"addressId": "integer",
"street": "string",...
}
}
扩展内部属性的代码可能出了一些问题,javascript控制台未显示任何错误,因此我认为这是一个错误。
我找到了解决方案,需要修改一行代码以使其正常工作:
在swagger.js文件中,有一个getSampleValue函数,该函数有条件地检查未定义:
SwaggerModelProperty.prototype.getSampleValue = function(modelsToIgnore) {
var result;
if ((this.refModel != null) && (modelsToIgnore[this.refModel.name] === 'undefined'))
...
我将相等性检查更新为(删除引号):
modelsToIgnore[this.refModel.name] === undefined
之后,SwaggerUI可以显示嵌入式模型。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句