我正在尝试接收POCO的POST列表,但通过电汇提交时,无法通过Swagger API或Postman反序列化。我已经序列化了该对象以确认它应该如何序列化,但是当将其返回到表单主体时,request对象的参数为null。
public class NameValuePair
{
public string Name { get; set; }
public string Value { get; set; }
}
public class NameValueList
{
private List<NameValuePair> valuesToProcess = null;
public List<NameValuePair> ValuesToProcess { get { return valuesToProcess; } set { valuesToProcess = value; } }
public NameValueList()
{
valuesToProcess = new List<NameValuePair>();
}
}
[Api("A description of what the API does")]
[Tag("Enriching Requests")]
[ApiResponse(HttpStatusCode.BadRequest, "Your request was not understood")]
[ApiResponse(HttpStatusCode.InternalServerError, "Oops, something broke")]
[Route("/EnrichValueList", "POST", Summary = "Summary of the Web Get method", Notes = "Longer information about what this service does")]
public class EnrichValueList : IPost, IReturn<EnrichValueListResponse>
{
[ApiMember(Name = "NameValues", Description = "A string that represents a value with which to understand more information about", ParameterType = "body", DataType = "NameValueList", IsRequired = true)]
public NameValueList NameValues
{
get;
set;
}
[ApiMember(Name = "AssociatedActivities", Description = "A comma seperated string that links Enrichment Activities with this value", ParameterType = "body", DataType = "string", IsRequired = false)]
public string AssociatedActivities
{
get;
set;
}
}
在这种情况下,request.NameValues为null(不会引发错误):
public async Task<EnrichValueListResponse> Post(EnrichValueList request)
{
EnrichValueListResponse enrichValueListResponse = new EnrichValueListResponse();
return enrichValueListResponse;
}
我已经有其他方法来接收字符串化对象的字符串,然后使用ServiceStack.Text中的JsonSerializer.DeserializeFromString方法,用这种方法可以很好地解决问题。我试图在原始请求中使用类型更强的对象(这可能是不可能的,或者我做错了)。
Param:NameValues,值{“ valuesToProcess”:[{“ name”:“ bob”,“ value”:“ Not Bob”}]}
并尝试我能想到的所有其他排列。有趣的是,当更改为纯字符串参数并发布时,内置的swagger API返回反序列化错误,但Postman很好。
响应正文为文本
{
"responseStatus": {
"errorCode": "SerializationException",
"message": "Type definitions should start with a '{', expecting serialized type 'EnrichValueList', got string starting with: \"two\"",
"stackTrace": " at ServiceStack.Text.Common.DeserializeTypeRefJson.StringToType(ReadOnlySpan`1 strType, TypeConfig typeConfig, EmptyCtorDelegate ctorFn, KeyValuePair`2[] typeAccessors)\r\n at ServiceStack.Text.Common.DeserializeType`1.StringToTypeContext.DeserializeJson(ReadOnlySpan`1 value)\r\n at ServiceStack.Text.Json.JsonReader.<>c__DisplayClass3_0.<GetParseSpanFn>b__0(ReadOnlySpan`1 v)\r\n at ServiceStack.Text.JsonSerializer.DeserializeFromSpan(Type type, ReadOnlySpan`1 value)\r\n at ServiceStack.Memory.NetCoreMemory.Deserialize(MemoryStream memoryStream, Boolean fromPool, Type type, DeserializeStringSpanDelegate deserializer)\r\n at ServiceStack.Memory.NetCoreMemory.DeserializeAsync(Stream stream, Type type, DeserializeStringSpanDelegate deserializer)\r\n at ServiceStack.Host.RestHandler.CreateRequestAsync(IRequest httpReq, IRestPath restPath, Dictionary`2 requestParams)\r\n at ServiceStack.Host.RestHandler.CreateRequestAsync(IRequest httpReq, IRestPath restPath)\r\n at ServiceStack.Host.RestHandler.ProcessRequestAsync(IRequest req, IResponse httpRes, String operationName)"
}
}
更新
按照的建议,我从装饰器中删除了ParameterType和DataType,并且能够表现出一些稍有不同的行为。
使用示例类:
public class NameValues
{
public string Name { get; set; }
public List<string> Values { get; set; }
public NameValues()
{
Values = new List<string>();
Name = string.Empty;
}
}
public class NameValuesList
{
public List<NameValues> ValuesToProcess { get; set; }
public NameValuesList()
{
ValuesToProcess = new List<NameValues>();
}
}
并将DTO参数设置为此
[ApiMember(Name = "NameValues", Description = "A string that represents a value with which to understand more information about", IsRequired = true)]
public NameValuesList NameValues
{
get;
set;
}
当我传入应该反序列化的JSON结构时,会导致相同的反序列化错误。但是,如果传入一些变形的字符串,它不会引发任何错误,并且会一直运行到IPost处理程序方法,该属性为null。
如果我将API参数改回字符串,并使用此示例通过ServiceStack.Text库显示序列化和反序列化,则通过Swagger和Postman都像超级按钮一样工作。
public async Task<EnrichValueListResponse> Post(EnrichValueList request)
{
EnrichValueListResponse enrichValueListResponse = new EnrichValueListResponse();
// Create a dummy object to serialize as JSON to return as an example back to the caller
NameValuesList stuff = new NameValuesList();
stuff.ValuesToProcess.Add(new NameValues { Name = "first", Values = { "second", "third" } });
enrichValueListResponse.BatchId = await Task.Run(() => stuff.ToJson().IndentJson());
// Deserialize the inbound string
NameValuesList betterStuff = JsonSerializer.DeserializeFromString<NameValuesList>(request.NameValues);
return enrichValueListResponse;
}
提交了JSON(尽管我确实尝试了许多不同的编码和结构变体)。
{
"valuesToProcess": [
{
"name": "first",
"values": [
"second",
"third"
]
}
]
}
我已经设置了调试模式,但是在解析期间看不到SeriLog数据库的任何异常。现在,我将使用字符串参数运行并在事实之后进行反序列化。我可以在Fluent Validator中进行一些预检查