Web API/JsonMediaTypeFormatter 接受无效的 JSON 并将空参数传递给操作

JTe*_*ech 5 json.net asp.net-web-api

我有以下模型:

public class Resource
{
    [DataMember(IsRequired = true)]
    [Required]
    public bool IsPublic { get; set; }

    [DataMember(IsRequired = true)]
    [Required]
    public ResourceKey ResourceKey { get; set; }
}

public class ResourceKey
{
    [StringLength(50, MinimumLength = 1)]
    [Required]
    public string SystemId { get; set; }

    [StringLength(50, MinimumLength = 1)]
    [Required]
    public string SystemDataIdType { get; set; }

    [StringLength(50, MinimumLength = 1)]
    [Required]
    public string SystemEntityType { get; set; }

    [StringLength(50, MinimumLength = 1)]
    [Required]
    public string SystemDataId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有以下操作方法签名:

public HttpResponseMessage PostResource(Resource resource)
Run Code Online (Sandbox Code Playgroud)

我在正文中发送以下带有 JSON 的请求(属性“IsPublic”的故意无效值):

Request Method:POST
Host: localhost:63307
Connection: keep-alive
Content-Length: 477
User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

{
    "IsPublic": invalidvalue,   
    "ResourceKey":{     
        "SystemId": "asdf",
        "SystemDataIdType": "int",
        "SystemDataId": "Lorem ipsum",
        "SystemEntityType":"EntityType"
    },    
}
Run Code Online (Sandbox Code Playgroud)

这是无效的 JSON - 通过 JSONLint 运行它,它会告诉你:

第 2 行解析错误:

{“IsPublic”:无效值,

.......^ 期待 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

ModelState.IsValid 属性为 'true' - 为什么???

此外,格式化程序似乎没有抛出验证错误,而是放弃了反序列化,只是将“资源”参数作为 null 传递给 action 方法!

请注意,如果我为其他属性输入无效值,也会发生这种情况,例如替换:

"SystemId": notAnObjectOrLiteralOrArray
Run Code Online (Sandbox Code Playgroud)

但是,如果我为“SystemId”属性发送带有特殊未定义值的以下 JSON :

{
    "IsPublic": true,   
    ResourceKey:{       
        "SystemId": undefined,
        "SystemDataIdType": "int",
        "SystemDataId": "Lorem ipsum",
        "SystemEntityType":"EntityType"
    },    
}
Run Code Online (Sandbox Code Playgroud)

然后我得到以下合理的异常抛出:

Exception Type: Newtonsoft.Json.JsonReaderException
Message: "Error reading string. Unexpected token: Undefined. Path 'ResourceKey.SystemId', line 4, position 24."
Stack Trace: " at Newtonsoft.Json.JsonReader.ReadAsStringInternal() 
at Newtonsoft.Json.JsonTextReader.ReadAsString() 
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter) 
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)"
Run Code Online (Sandbox Code Playgroud)

SO:Newtonsoft.Json 库中发生了什么,导致看起来像部分 JSON 验证???

PS:可以将 JSON 名称/值对发布到 Web API,而无需将名称括在引号中...

{
    IsPublic: true, 
    ResourceKey:{       
        SystemId: "123",
        SystemDataIdType: "int",
        SystemDataId: "Lorem ipsum",
        SystemEntityType:"EntityType"
    },    
}
Run Code Online (Sandbox Code Playgroud)

这也是无效的 JSON!

JTe*_*ech 3

好的 - 看来问题的一部分是由我自己造成的。

我的控制器上有两个过滤器:

  1. 检查是否有任何空操作参数传递给操作方法,如果有,则返回“400 Bad Request”响应,规定该参数不能为空。
  2. ModelState 检查过滤器检查 ModelState 的错误,如果发现任何错误,则在“400 Bad Request”响应中返回它们。

我犯的错误是将空参数过滤器放在模型状态检查过滤器之前。

模型绑定后,第一个 JSON 示例的序列化将正确失败,并将相关序列化异常放入 ModelState 中,并且操作参数将保持为空,这是正确的。

然而,由于第一个过滤器检查空参数,然后返回“404 Bad Request”响应,因此 ModelState 过滤器从未启动......

因此,验证似乎没有发生,而事实上确实如此,但结果被忽略了!

重要提示:模型绑定期间发生的序列化异常被放置在 ModelState KeyValue 对 Value 的“Exception”属性中...而不是放在 ErrorMessage 属性中!

为了帮助其他人区分这一点,这是我的 ModelValidationFilterAttribute:

public class ModelValidationFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (actionContext.ModelState.IsValid) return;

        // Return the validation errors in the response body.
        var errors = new Dictionary<string, IEnumerable<string>>();
        foreach (KeyValuePair<string, ModelState> keyValue in actionContext.ModelState)
        {
            var modelErrors = keyValue.Value.Errors.Where(e => e.ErrorMessage != string.Empty).Select(e => e.ErrorMessage).ToList();
            if (modelErrors.Count > 0)
                errors[keyValue.Key] = modelErrors;

            // Add details of any Serialization exceptions as well
            var modelExceptions = keyValue.Value.Errors.Where(e => e.Exception != null).Select(e => e.Exception.Message).ToList();
            if (modelExceptions.Count > 0)
                errors[keyValue.Key + "_exception"] = modelExceptions;
        }
        actionContext.Response =
            actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, errors);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是操作方法,其中过滤器的顺序正确:

    [ModelValidationFilter]
    [ActionArgNotNullFilter]
    public HttpResponseMessage PostResource(Resource resource)
Run Code Online (Sandbox Code Playgroud)

现在,以下 JSON 结果为:

{
    "IsPublic": invalidvalue,   
    "ResourceKey":{     
        "SystemId": "asdf",
        "SystemDataIdType": "int",
        "SystemDataId": "Lorem ipsum",
        "SystemEntityType":"EntityType"
    },    
} 

{
    "resource.IsPublic_exception": [(2)
    "Unexpected character encountered while parsing value: i. Path 'IsPublic', line 2, position 21.",
    "Unexpected character encountered while parsing value: i. Path 'IsPublic', line 2, position 21."
    ]-
}
Run Code Online (Sandbox Code Playgroud)

然而,所有这些并不能解释为什么无效的 JSON 仍然由 JsonMediaTypeFormatter 解析,例如它不要求名称是字符串。