ServiceStack对象参数未通过Swagger-UI传递给服务

时间:2016-11-18 18:43:42

标签: c# servicestack swagger swagger-ui

我的服务模式:

[Route("/customer/{CustomerId}/creditcardtoken/{CanLookup}", "POST")]
public class CreditCardToken : IReturn<CreditCardTokenResponse>
{
    [ApiMember(ParameterType = "path", DataType = "string", IsRequired = true, Verb = "POST")]
    public string CustomerId { get; set; }
    [ApiMember(ParameterType = "path", DataType = "boolean", IsRequired = true, Verb = "POST")]
    public bool CanLookup { get; set; }
    [ApiMember(ParameterType = "body", DataType = "CreditCard", IsRequired = true, Verb = "POST")]
    public CreditCard CreditCard { get; set; }
}

public class CreditCard
{
    public short CreditCardTypeId { get; set; }
    public string NameOnCard { get; set; }
    public string CardNumber { get; set; }
    public string Expiration { get; set; }
    public string PostalCode { get; set; }
}

服务接口没有任何内容(我在返回null上设置了一个断点):

public class CreditCardTokenService : Service
{
    public CreditCardTokenResponse Post(CreditCardToken request)
    {
        return null;
    }
}

在撰写此问题(不是.Net Core)时使用最新版本的servicestack for .Net 4.6.2。

当我填充Swagger-UI中的字段时,CreditCard对象不会通过,它只是null。不确定我缺少什么,CustomerId和CanLookup确实可以通过。我尝试在CreditCard类上设置DataContract / DataMember。

enter image description here

1 个答案:

答案 0 :(得分:3)

您的请求DTO与声明的​​Swagger架构不匹配,CreditCard不是主体,它只是一个具有复杂类型值的参数。整个CreditCardToken请求DTO应该作为HTTP POST请求的JSON主体进行POST,而此定义仅发布CreditCard JSON正文。其他参数正在通过,因为它们是在请求的路径信息上发送的,即它们不会在正文中发送。