使用 ServiceStack 的 Swagger 插件,如何实现带有预设值列表的字符串字段 [英] Using ServiceStack's Swagger Plugin, how to implement a string field with a list of preset values

查看:20
本文介绍了使用 ServiceStack 的 Swagger 插件,如何实现带有预设值列表的字符串字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am implementing Swagger API documentation using ServiceStack's new Swagger plugin and am trying to determine how to use the "container" data type. I need to display a string field that has a list of predetermined values and other parameters that are lists of sub-objects.

除非我遗漏了一些东西,否则我相信 swagger 只能采用一个文本字段,您可以为子对象列表输入 JSON.我相信这段代码应该可以解决问题.

Unless I am missing something I believe swagger can only take a text field that you input the JSON for you list of sub-objects. I believe this code should do the trick.

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

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

What I do not know ( and am hoping someone out there can help me) is if it is possible to have a string field that is from a preset list of values. In Swagger this code snippet illustrates how to do this.

"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 实现的吗?

Does anyone know how this is accomplished using ServiceStack.Api.Swagger?

推荐答案

我一直在为同样的问题苦苦挣扎,但意识到目前不支持此功能.您基本上不能使用模型 POST 或 PUT 数据.这个功能还在不断变化中,正在开发中,所以我想它在待办事项列表中.

I've been struggling with the same issue, but have realised that this feature is currently unsupported. You basically cannot POST or PUT data using Models. This feature is in flux and under development so I guess it is on the todo list.

如果您查看源代码,您将看到 ResourcesResponse 数据契约:

If you view the source code, you will see that there is no Models property supported in the ResourcesResponse data contract:

[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 示例进行比较,您会发现模型作为根节点包含在内:

If you compare this to the Petstore example on Wordnik, you'll find that the models are included as a root node:

{
   "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 界面中,您将看到一个文本区域,您必须在其中粘贴实际的 JSON 对象.您的请求将如下所示:

I think that the only way around this is to post the entire object yourself. Have a request object that takes an entire object, such as Pet. Set the ParameterType to body and the DataType to Pet. In the Swagger interface you'll see a textarea, into which you have to paste an actual JSON object. You request will look like this:

[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();
    }
}

将呈现以下内容:

像这样输入对象,请求就会被正确解析:

Enter the object like so, and the request will be correctly parsed:

这篇关于使用 ServiceStack 的 Swagger 插件,如何实现带有预设值列表的字符串字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆