private static object CovertResourceId(object source, ODataResource resource, IEdmEntityTypeReference entityTypeReference, ODataDeserializerContext readContext) { Contract.Assert(resource != null); Contract.Assert(source != null); if (resource.Id == null || resource.Properties.Any()) { return(source); } IWebApiRequestMessage request = readContext.InternalRequest; IWebApiUrlHelper urlHelper = readContext.InternalUrlHelper; DefaultODataPathHandler pathHandler = new DefaultODataPathHandler(); string serviceRoot = urlHelper.CreateODataLink( request.Context.RouteName, request.PathHandler, new List <ODataPathSegment>()); IEnumerable <KeyValuePair <string, object> > keyValues = GetKeys(pathHandler, serviceRoot, resource.Id, request.RequestContainer); IList <IEdmStructuralProperty> keys = entityTypeReference.Key().ToList(); if (keys.Count == 1 && keyValues.Count() == 1) { // TODO: make sure the enum key works object propertyValue = keyValues.First().Value; DeserializationHelpers.SetDeclaredProperty(source, EdmTypeKind.Primitive, keys[0].Name, propertyValue, keys[0], readContext); return(source); } IDictionary <string, object> keyValuesDic = keyValues.ToDictionary(e => e.Key, e => e.Value); foreach (IEdmStructuralProperty key in keys) { object value; if (keyValuesDic.TryGetValue(key.Name, out value)) { // TODO: make sure the enum key works DeserializationHelpers.SetDeclaredProperty(source, EdmTypeKind.Primitive, key.Name, value, key, readContext); } } return(source); }
private static object CovertResourceId(object source, ODataResourceBase resource, IEdmEntityTypeReference entityTypeReference, ODataDeserializerContext readContext) { Contract.Assert(resource != null); Contract.Assert(source != null); if (resource.Id == null || resource.Properties.Any()) { return(source); } HttpRequest request = readContext.Request; string serviceRoot = request.CreateODataLink(); IEnumerable <KeyValuePair <string, object> > keyValues = GetKeys(request, serviceRoot, resource.Id, request.GetSubServiceProvider()); IList <IEdmStructuralProperty> keys = entityTypeReference.Key().ToList(); if (keys.Count == 1 && keyValues.Count() == 1) { // TODO: make sure the enum key works object propertyValue = keyValues.First().Value; DeserializationHelpers.SetDeclaredProperty(source, EdmTypeKind.Primitive, keys[0].Name, propertyValue, keys[0], readContext); return(source); } IDictionary <string, object> keyValuesDic = keyValues.ToDictionary(e => e.Key, e => e.Value); foreach (IEdmStructuralProperty key in keys) { object value; if (keyValuesDic.TryGetValue(key.Name, out value)) { // TODO: make sure the enum key works DeserializationHelpers.SetDeclaredProperty(source, EdmTypeKind.Primitive, key.Name, value, key, readContext); } } return(source); }
internal static object CovertEntityId(object source, ODataEntry entry, IEdmEntityTypeReference entityTypeReference, ODataDeserializerContext readContext) { Contract.Assert(entry != null); Contract.Assert(source != null); if (entry.Id == null || entry.Properties.Any()) { return(source); } HttpRequestMessage request = readContext.Request; IEdmModel edmModel = readContext.Model; DefaultODataPathHandler pathHandler = new DefaultODataPathHandler(); string serviceRoot = GetServiceRoot(request); IDictionary <string, string> keyValues = GetKeys(pathHandler, edmModel, serviceRoot, entry.Id); IList <IEdmStructuralProperty> keys = entityTypeReference.Key().ToList(); if (keys.Count == 1 && keyValues.Count == 1) { object propertyValue = ODataUriUtils.ConvertFromUriLiteral(keyValues.First().Value, ODataVersion.V4, edmModel, keys[0].Type); DeserializationHelpers.SetDeclaredProperty(source, EdmTypeKind.Primitive, keys[0].Name, propertyValue, keys[0], readContext); return(source); } foreach (IEdmStructuralProperty key in keys) { string value; if (keyValues.TryGetValue(key.Name, out value)) { object propertyValue = ODataUriUtils.ConvertFromUriLiteral(value, ODataVersion.V4, edmModel, key.Type); DeserializationHelpers.SetDeclaredProperty(source, EdmTypeKind.Primitive, key.Name, propertyValue, key, readContext); } } return(source); }