我一直在为同样的问题而苦苦挣扎,但意识到目前不支持此功能。您基本上不能使用模型 POST 或 PUT 数据。此功能正在不断变化且正在开发中,所以我猜它在待办事项列表中。
如果查看源代码,您将看到ResourcesResponse数据协定中不Models
支持任何属性:
[DataContract]
public class ResourcesResponse
{
[DataMember(Name = "swaggerVersion")]
public string SwaggerVersion { get; set; }
[DataMember(Name = "apiVersion")]
public string ApiVersion { get; set; }
[DataMember(Name = "basePath")]
public string BasePath { get; set; }
[DataMember(Name = "apis")]
public List<RestService> Apis { get; set; }
}
如果您将此与 Wordnik 上的 Petstore 示例进行比较,您会发现模型作为根节点包含在内:
{
"apiVersion":"0.2",
"swaggerVersion":"1.1",
"basePath":"http://petstore.swagger.wordnik.com/api",
"resourcePath":"/pet",
"apis":[
{
"path":"/pet.{format}",
"description":"Operations about pets",
"operations":[
{
"httpMethod":"POST",
"summary":"Add a new pet to the store",
"responseClass":"void",
"nickname":"addPet",
"parameters":[
{
"description":"Pet object that needs to be added to the store",
"paramType":"body",
"required":true,
"allowMultiple":false,
"dataType":"Pet"
}
],
"errorResponses":[
{
"code":405,
"reason":"Invalid input"
}
]
}
]
}
],
"models":{
"Category":{
"id":"Category",
"properties":{
"id":{
"type":"long"
},
"name":{
"type":"string"
}
}
},
"Pet":{
"id":"Pet",
"properties":{
"tags":{
"items":{
"$ref":"Tag"
},
"type":"Array"
},
"id":{
"type":"long"
},
"category":{
"type":"Category"
},
"status":{
"allowableValues":{
"valueType":"LIST",
"values":[
"available",
"pending",
"sold"
],
"valueType":"LIST"
},
"description":"pet status in the store",
"type":"string"
},
"name":{
"type":"string"
},
"photoUrls":{
"items":{
"type":"string"
},
"type":"Array"
}
}
},
"Tag":{
"id":"Tag",
"properties":{
"id":{
"type":"long"
},
"name":{
"type":"string"
}
}
}
}
}
我认为解决这个问题的唯一方法是自己发布整个对象。拥有一个接受整个对象的请求对象,例如 Pet。设置ParameterType
tobody
和DataType
to Pet
。在 Swagger 界面中,您将看到一个文本区域,您必须将实际的 JSON 对象粘贴到其中。您的请求将如下所示:
[Api("The Thing Service")]
[Route("/thing", "POST", Summary = @"POST a new thing", Notes = "Send a thing here")]
public class ThingRequest
{
[DataMember]
[ApiMember(Name = "Thing", Description = "The thing", ParameterType = "body", DataType = "Thing", IsRequired = false)]
public ThingDto Thing { get; set; }
}
你的服务是这样的:
/// <summary>
/// Summary description for ThingService
/// </summary>
public class ThingService : Service
{
public IThingRepository ThingRepository { get; set; }
public object Post(ThingRequest request)
{
var thing = Thing.Map(request);
ThingRepository.Save(thing);
return new ThingResponse();
}
}
将呈现以下内容:
像这样输入对象,请求将被正确解析: