使用ServiceStack的Swagger插件,如何使用预设值列表实现字符串字段

时间:2013-02-07 21:35:16

标签: api servicestack swagger

我正在使用ServiceStack的新Swagger插件实现Swagger API文档,并尝试确定如何使用“容器”数据类型。我需要显示一个字符串字段,其中包含预定值列表和其他参数,这些参数是子对象列表。

除非我遗漏了一些东西,否则我认为招摇只能采取一个文本字段,您输入JSON作为子对象列表。我相信这段代码可以解决问题。

[ApiMember(Name = "Connections", Description = "insert JSON sample here", ParameterType = "body", DataType = "container", IsRequired = false, Verb = "Post")]

我不知道(并希望有人可以帮助我)是否可以使用来自预设值列表的字符串字段。在Swagger中,这段代码片段说明了如何执行此操作。

"Pet":{
    "id":"Pet",
    "properties":{
    ...
      "status":{
        "type":"String",
        "description":"pet status in the store",
        "allowableValues":{
          "valueType":"LIST",
          "values":[
            "available",
            "pending",
            "sold"
          ]
        }
      },
      "happiness": {
        "type": "Int",
        "description": "how happy the Pet appears to be, where 10 is 'extremely happy'",
        "allowableValues": {
          "valueType": "RANGE",
          "min": 1,
          "max": 10
        }
      },
      ...

有谁知道如何使用ServiceStack.Api.Swagger完成这项工作?

1 个答案:

答案 0 :(得分:3)

我一直在努力解决同样的问题,但已经意识到此功能目前尚未得到支持。您基本上不能使用模型POST或PUT数据。这个功能正在变化,正在开发中,所以我猜它在todo列表中。

如果您查看源代码,您会发现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设置为body,将DataType设置为Pet。在Swagger界面中,您将看到一个textarea,您必须在其中粘贴实际的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();
    }
}

将呈现以下内容:

Swagger output

像这样输入对象,并正确解析请求:

enter image description here