使用ServiceStack的Swagger不会在POST时向服务器发送元素

时间:2013-03-20 20:15:47

标签: servicestack swagger

我有一个简单的会话对象,看起来像这样

[Route("/Session", Summary = "Creates a security session", Notes = "Some session related notes here")]
public class Session : IReturn<SessionResponse>
{
    [ApiMember(Name = "DomainName", Description = "The Security Domain", ParameterType = "path", DataType = "string", IsRequired = true)]
    public string DomainName { get; set; }

    [ApiMember(Name = "UserName", Description = "The User Name", ParameterType = "path", DataType = "string", IsRequired = true)]
    public string UserName { get; set; }

    [ApiMember(Name = "Password", Description = "The password", ParameterType = "path", DataType = "string", IsRequired = true)]
    public string Password { get; set; }
}

当我去swagger UI时,我可以看到元素

但是,当我输入元素并按下立即尝试时,我看到请求内容未发送到服务器。

我是否使用parameterType =“path”正确配置了我的poco,还是应该在这里做其他事情?请指教。

4 个答案:

答案 0 :(得分:1)

我是否已使用parameterType =“path”正确配置了我的poco,还是应该在此处执行其他操作?

我不这么认为。将每个会话属性配置为ParameterType='path'表明您希望每个属性都是带有路径/ url(文档here)的变量/字段。所以如果你想使用'path',你会希望你的ServiceStack路由看起来像这样。

[Route("/Session/{DomainName}/{UserName}/{Password}"]

对我而言,'查询'或'正文'将是更好的选择。另外,This might contain useful information as well

答案 1 :(得分:1)

我让这个工作,这是我必须做的事情

[Route("/Session", "POST", Summary = "Creates a security session", Notes = "Some session related notes here")]
public class Session : IReturn<SessionResponse>
{
    [ApiMember(Name = "SessionData", Description = "The Session Data", ParameterType = "body", DataType = "SessionData", IsRequired = true)]
    public string DomainName { get; set; }

    //[ApiMember(Name = "UserName", Description = "The User Name", ParameterType = "path", DataType = "string", IsRequired = true)]
    public string UserName { get; set; }

    //[ApiMember(Name = "Password", Description = "The password", ParameterType = "path", DataType = "string", IsRequired = true)]
    public string Password { get; set; }
}

所以现在我得到一个文本区域框,我需要输入整个会话对象的JSON,以便转移DomainName,UserName和Password,但不知何故这似乎不对。

答案 2 :(得分:1)

为了支持在ServiceStack中使用Swagger的当前实现在请求体中发送数据,您应该只有一个ApiMemberParameterType = "body"(您仍然可以将其他ApiMember属性与其他其他ParameterType值一起使用,如果你的URL中有变量,例如)。

ApiMember ParameterType = "body"代表您的请求正文的全部内容。使用此属性装饰您的请求DTO的哪个属性并不重要。我们这样做是为了在Swagger UI中明确表示应该使用为此textarea生成的ApiMember来填充整个请求正文:

[ApiMember(Name = "RequestBody", ParameterType = "body", IsRequired = true,
    Description = SomeLongStringConstantThatDescribesTheEntireDTO]

在Swagger UI中,这将显示textarea,您需要在此文本区域中输入整个DTO作为JSON。在你的情况下,它会是这样的:

{"DomainName": "...", "UserName": "...", "Password": "..."}

答案 3 :(得分:1)

在没有在body参数定义中看到路径参数的情况下,是否有任何关于让它工作的更新?

[Route("/users/{UserId}/races", "POST", Summary = "Associates a race with a user")]
public class AddUserRace : IReturnVoid
{
    [ApiMember(Description = "User Id to associate a race with to", ParameterType = "path", Verb="POST", DataType = "int", IsRequired = true)]
    public int UserId { get; set; }
    [Description("Race Id of race to associate with user")]
    public int RaceId { get; set; }
}

Swagger UI Result

理想情况下,由于UserId是路径参数,我希望它隐藏在DTO主体中。