使用ServiceStack的扬鞭插件,如何实现与preSET值的列表的字符串场 [英] Using ServiceStack's Swagger Plugin, how to implement a string field with a list of preset values

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

问题描述

我实现使用ServiceStack新扬鞭插件扬鞭API文档,我试图确定如何使用容器数据类型。我需要显示具有是子对象的列表predetermined值和其他参数的列表的字符串字段

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.

除非我失去了一些东西,我相信招摇只能拿一个文本字段,你输入JSON你子对象的列表。我相信这code应该做的伎俩。

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")]

我不知道什么(我希望有人在那里可以帮助我)是,如果有可能有一个字符串字段是从值preSET列表。在此扬鞭code片段说明如何做到这一点。

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?

推荐答案

我一直在挣扎了同样的问题,但已经意识到这个功能目前不支持。你基本上不能发布或使用模型将数据。此功能在不断变化和正在开发的,所以我想这是待办事项列表。

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.

如果您查看源代码code,你会看到,有没有模式属性在<支持href=\"https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.Api.Swagger/SwaggerResourcesService.cs#L19\"相对=nofollow> 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"
            }
         }
      }
   }
}

我认为,解决这个问题的唯一方法是自己张贴整个对象。有需要整个对象,如宠物一个请求对象。在参数类型数据类型设置为宠物。在扬鞭界面,你会看到一个textarea,在其中您必须粘贴一个实际的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的扬鞭插件,如何实现与preSET值的列表的字符串场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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