/// <inheritdoc/> public override Task <object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) { if (type == null) { throw Error.ArgumentNull("type"); } if (readStream == null) { throw Error.ArgumentNull("readStream"); } object result = null; HttpContentHeaders contentHeaders = content == null ? null : content.Headers; // If content length is 0 then return default value for this type if (contentHeaders != null && contentHeaders.ContentLength == 0) { result = GetDefaultValueForType(type); } else { bool isPatchMode = TryGetInnerTypeForDelta(ref type); ODataDeserializer deserializer = ODataDeserializerProvider.GetODataDeserializer(type); if (deserializer == null) { throw Error.Argument("type", SRResources.FormatterReadIsNotSupportedForType, type.FullName, GetType().FullName); } ODataMessageReader oDataMessageReader = null; ODataMessageReaderSettings oDataReaderSettings = new ODataMessageReaderSettings { DisableMessageStreamDisposal = true }; try { IODataRequestMessage oDataRequestMessage = new ODataMessageWrapper(readStream, contentHeaders); oDataMessageReader = new ODataMessageReader(oDataRequestMessage, oDataReaderSettings, ODataDeserializerProvider.EdmModel); ODataDeserializerContext readContext = new ODataDeserializerContext { IsPatchMode = isPatchMode, PatchKeyMode = PatchKeyMode }; result = deserializer.Read(oDataMessageReader, readContext); } finally { if (oDataMessageReader != null) { oDataMessageReader.Dispose(); } } } return(TaskHelpers.FromResult(result)); }
/// <summary> /// Read and deserialize incoming object from HTTP request stream. /// </summary> /// <param name="type">incoming request body object type.</param> /// <param name="defaultValue">default value for this type.</param> /// <param name="model">Edm model to reference when validating.</param> /// <param name="baseAddress">Base address of request.</param> /// <param name="internalRequest">HTTP request method that contains ODataPath.</param> /// <param name="getODataRequestMessage">Function to obtain customized ODataMessageWrapper.</param> /// <param name="getEdmTypeDeserializer">Function to obtain appropriate edm deserializer.</param> /// <param name="getODataPayloadDeserializer">Function to obtain appropriate deserialize for function payloads.</param> /// <param name="getODataDeserializerContext">Context for Deserializer.</param> /// <param name="registerForDisposeAction">Registration function for disposables.</param> /// <returns>Deserialized object.</returns> internal object ReadFromStream( Type type, object defaultValue, IEdmModel model, Uri baseAddress, HttpRequest internalRequest, Func <IODataRequestMessage> getODataRequestMessage, Func <IEdmTypeReference, ODataDeserializer> getEdmTypeDeserializer, Func <Type, ODataDeserializer> getODataPayloadDeserializer, Func <ODataDeserializerContext> getODataDeserializerContext, Action <IDisposable> registerForDisposeAction, Action <Exception> logErrorAction) { object result; IEdmTypeReference expectedPayloadType; ODataPath path = internalRequest.ODataFeature().Path; ODataDeserializer deserializer = GetDeserializer(type, path, model, getEdmTypeDeserializer, getODataPayloadDeserializer, out expectedPayloadType); if (deserializer == null) { throw new ArgumentException("type", "Formatter does not support reading type " + type.FullName); } try { ODataMessageReaderSettings oDataReaderSettings = internalRequest.GetReaderSettings(); oDataReaderSettings.BaseUri = baseAddress; oDataReaderSettings.Validations = oDataReaderSettings.Validations & ~ValidationKinds.ThrowOnUndeclaredPropertyForNonOpenType; IODataRequestMessage oDataRequestMessage = getODataRequestMessage(); ODataMessageReader oDataMessageReader = new ODataMessageReader(oDataRequestMessage, oDataReaderSettings, model); registerForDisposeAction(oDataMessageReader); ODataDeserializerContext readContext = getODataDeserializerContext(); readContext.Path = path; readContext.Model = model; readContext.ResourceType = type; readContext.ResourceEdmType = expectedPayloadType; result = deserializer.Read(oDataMessageReader, type, readContext); } catch (Exception e) { logErrorAction(e); result = defaultValue; } return(result); }
internal static object ReadFromStream(Type type, object defaultValue, Uri baseAddress, HttpRequest request, IList <IDisposable> disposes) { object result; IEdmModel model = request.GetModel(); IEdmTypeReference expectedPayloadType; ODataDeserializer deserializer = GetDeserializer(request, type, out expectedPayloadType); if (deserializer == null) { throw Error.Argument("type", SRResources.FormatterReadIsNotSupportedForType, type.FullName, typeof(ODataInputFormatter).FullName); } try { ODataMessageReaderSettings oDataReaderSettings = request.GetReaderSettings(); oDataReaderSettings.BaseUri = baseAddress; oDataReaderSettings.Validations = oDataReaderSettings.Validations & ~ValidationKinds.ThrowOnUndeclaredPropertyForNonOpenType; IODataRequestMessage oDataRequestMessage = ODataMessageWrapperHelper.Create(request.Body, request.Headers, request.GetODataContentIdMapping(), request.GetSubServiceProvider()); ODataMessageReader oDataMessageReader = new ODataMessageReader(oDataRequestMessage, oDataReaderSettings, model); disposes.Add(oDataMessageReader); ODataPath path = request.ODataFeature().Path; ODataDeserializerContext readContext = new ODataDeserializerContext { Request = request, }; readContext.Path = path; readContext.Model = model; readContext.ResourceType = type; readContext.ResourceEdmType = expectedPayloadType; result = deserializer.Read(oDataMessageReader, type, readContext); } catch (Exception ex) { LoggerError(request.HttpContext, ex); result = defaultValue; } return(result); }
private object ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) { object result; HttpContentHeaders contentHeaders = content == null ? null : content.Headers; // If content length is 0 then return default value for this type if (contentHeaders == null || contentHeaders.ContentLength == 0) { result = GetDefaultValueForType(type); } else { IEdmModel model = Request.ODataProperties().Model; if (model == null) { throw Error.InvalidOperation(SRResources.RequestMustHaveModel); } IEdmTypeReference expectedPayloadType; ODataDeserializer deserializer = GetDeserializer(type, Request.ODataProperties().Path, model, _deserializerProvider, out expectedPayloadType); if (deserializer == null) { throw Error.Argument("type", SRResources.FormatterReadIsNotSupportedForType, type.FullName, GetType().FullName); } try { ODataMessageReaderSettings oDataReaderSettings = new ODataMessageReaderSettings(MessageReaderSettings); oDataReaderSettings.BaseUri = GetBaseAddress(Request); IODataRequestMessage oDataRequestMessage = new ODataMessageWrapper(readStream, contentHeaders, Request.GetODataContentIdMapping()); ODataMessageReader oDataMessageReader = new ODataMessageReader(oDataRequestMessage, oDataReaderSettings, model); Request.RegisterForDispose(oDataMessageReader); ODataPath path = Request.ODataProperties().Path; ODataDeserializerContext readContext = new ODataDeserializerContext { Path = path, Model = model, Request = Request, ResourceType = type, ResourceEdmType = expectedPayloadType, RequestContext = Request.GetRequestContext(), }; result = deserializer.Read(oDataMessageReader, type, readContext); } catch (Exception e) { if (formatterLogger == null) { throw; } formatterLogger.LogError(String.Empty, e); result = GetDefaultValueForType(type); } } return(result); }
internal static object ReadFromStream( #endif Type type, object defaultValue, IEdmModel model, ODataVersion version, Uri baseAddress, IWebApiRequestMessage internalRequest, Func <IODataRequestMessage> getODataRequestMessage, Func <IEdmTypeReference, ODataDeserializer> getEdmTypeDeserializer, Func <Type, ODataDeserializer> getODataPayloadDeserializer, Func <ODataDeserializerContext> getODataDeserializerContext, Action <IDisposable> registerForDisposeAction, Action <Exception> logErrorAction) { object result; IEdmTypeReference expectedPayloadType; ODataDeserializer deserializer = GetDeserializer(type, internalRequest.Context.Path, model, getEdmTypeDeserializer, getODataPayloadDeserializer, out expectedPayloadType); if (deserializer == null) { throw Error.Argument("type", SRResources.FormatterReadIsNotSupportedForType, type.FullName, typeof(ODataInputFormatterHelper).FullName); } try { ODataMessageReaderSettings oDataReaderSettings = internalRequest.ReaderSettings; oDataReaderSettings.BaseUri = baseAddress; oDataReaderSettings.Validations = oDataReaderSettings.Validations & ~ValidationKinds.ThrowOnUndeclaredPropertyForNonOpenType; oDataReaderSettings.Version = version; IODataRequestMessage oDataRequestMessage = getODataRequestMessage(); string preferHeader = RequestPreferenceHelpers.GetRequestPreferHeader(internalRequest.Headers); string annotationFilter = null; if (!String.IsNullOrEmpty(preferHeader)) { oDataRequestMessage.SetHeader(RequestPreferenceHelpers.PreferHeaderName, preferHeader); annotationFilter = oDataRequestMessage.PreferHeader().AnnotationFilter; } if (annotationFilter != null) { oDataReaderSettings.ShouldIncludeAnnotation = ODataUtils.CreateAnnotationFilter(annotationFilter); } ODataMessageReader oDataMessageReader = new ODataMessageReader(oDataRequestMessage, oDataReaderSettings, model); registerForDisposeAction(oDataMessageReader); ODataPath path = internalRequest.Context.Path; ODataDeserializerContext readContext = getODataDeserializerContext(); readContext.Path = path; readContext.Model = model; readContext.ResourceType = type; readContext.ResourceEdmType = expectedPayloadType; #if NETCORE result = await deserializer.ReadAsync(oDataMessageReader, type, readContext); #else result = deserializer.Read(oDataMessageReader, type, readContext); #endif } catch (Exception e) { logErrorAction(e); result = defaultValue; } return(result); }
public override Task <object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) { if (type == null) { throw Error.ArgumentNull("type"); } if (readStream == null) { throw Error.ArgumentNull("readStream"); } if (_request == null) { throw Error.InvalidOperation(SRResources.ReadFromStreamAsyncMustHaveRequest); } return(TaskHelpers.RunSynchronously <object>(() => { object result; HttpContentHeaders contentHeaders = content == null ? null : content.Headers; // If content length is 0 then return default value for this type if (contentHeaders == null || contentHeaders.ContentLength == 0) { result = GetDefaultValueForType(type); } else { IEdmModel model = _request.GetEdmModel(); if (model == null) { throw Error.InvalidOperation(SRResources.RequestMustHaveModel); } Type originalType = type; bool isPatchMode = TryGetInnerTypeForDelta(ref type); ODataDeserializer deserializer = _deserializerProvider.GetODataDeserializer(model, type); if (deserializer == null) { throw Error.Argument("type", SRResources.FormatterReadIsNotSupportedForType, type.FullName, GetType().FullName); } ODataMessageReader oDataMessageReader = null; ODataMessageReaderSettings oDataReaderSettings = new ODataMessageReaderSettings { DisableMessageStreamDisposal = true, MessageQuotas = MessageReaderQuotas, BaseUri = GetBaseAddress(_request) }; try { IODataRequestMessage oDataRequestMessage = new ODataMessageWrapper(readStream, contentHeaders); oDataMessageReader = new ODataMessageReader(oDataRequestMessage, oDataReaderSettings, model); _request.RegisterForDispose(oDataMessageReader); ODataPath path = _request.GetODataPath(); ODataDeserializerContext readContext = new ODataDeserializerContext { IsPatchMode = isPatchMode, Path = path, Model = model, Request = _request }; if (isPatchMode) { readContext.PatchEntityType = originalType; } result = deserializer.Read(oDataMessageReader, readContext); } catch (Exception e) { if (formatterLogger == null) { throw; } formatterLogger.LogError(String.Empty, e); result = GetDefaultValueForType(type); } } return result; })); }