OData / WebApi

OData Web API: A server library built upon ODataLib and WebApi
https://docs.microsoft.com/odata
Other
855 stars 473 forks source link

Failed to deserialize special character(#) in property name of open type #1758

Open dh97243 opened 5 years ago

dh97243 commented 5 years ago

Deserialization of IDictionary<string, object> will be failed if one of the name/value pairs has hash character(#) in name. Wondering how to workaround this? Any escapes needed?

Assemblies affected

Microsoft.AspNet.OData 7.1.0

Reproduce steps

Suppose we have following model.

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ProductProperties Properties { get; set; }
}

public class ProductProperties
{
    public IDictionary<string, object> Properties { get; set; }
}

Valid Payload(POST request to create a Product entity):

{
    "@odata.type" : "#odatatest.Models.Product",
    "Name": "test",
    "Properties" : {"prop 1":"a"}
}

Invalid Payload(POST request to create a Product entity):

{
    "@odata.type" : "#odatatest.Models.Product",
    "Name": "test",
    "Properties" : {"prop #1":"a"}
}

Expected result

Dictionary would be deserialized successfully

Actual result

Deserialization failed with error :

product : A metadata reference property was found in a JSON Light request payload. Metadata reference properties are only supported in responses.

Additional detail

Optional, details of the root cause if known. Delete this section if you have no additional details to add.

xuzhg commented 5 years ago

@dh97243 The property name string should follow up:

_15.2 Simple Identifier A simple identifier is a Unicode character sequence with the following restrictions:

It consists of at least one and at most 128 Unicode characters. The first character MUST be the underscore character (U+005F) or any character in the Unicode category “Letter (L)” or “Letter number (Nl)”. The remaining characters MUST be the underscore character (U+005F) or any character in the Unicode category “Letter (L)”, “Letter number (Nl)”, “Decimal number (Nd)”, “Non-spacing mark (Mn)”, “Combining spacing mark (Mc)”, “Connector punctuation (Pc)”, and “Other, format (Cf)”. Non-normatively speaking it starts with a letter or underscore, followed by at most 127 letters, underscores or digits._

sinadell commented 5 years ago

@dh97243 @xuzhg I'm running into same issue. How can we workaround this issue?